@datagrok/curves 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,17 +2,19 @@ import * as DG from 'datagrok-api/dg';
2
2
  import * as ui from 'datagrok-api/ui';
3
3
 
4
4
  import {CellRenderViewer} from './cell-render-viewer';
5
- import {FitChartCellRenderer, mergeSeries} from './fit-renderer';
5
+ import {FitChartCellRenderer, mergeChartOptions, mergeSeries} from './fit-renderer';
6
6
  import {getChartData, mergeProperties} from './fit-renderer';
7
- import {FIT_SEM_TYPE, FitChartData, fitChartDataProperties, IFitChartData} from '@datagrok-libraries/statistics/src/fit/fit-curve';
7
+ import {FIT_SEM_TYPE, FitChartData, fitChartDataProperties, IFitChartData, IFitChartOptions} from '@datagrok-libraries/statistics/src/fit/fit-curve';
8
8
 
9
9
  import {debounce} from 'rxjs/operators';
10
10
  import {interval, merge} from 'rxjs';
11
+ import {FitConstants} from './const';
11
12
 
12
13
 
13
14
  const ERROR_CLASS = 'd4-viewer-error';
14
15
 
15
16
  export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
17
+ [index: string]: any;
16
18
  curvesColumnNames?: string[] = [];
17
19
  showSelectedRowsCurves: boolean = false;
18
20
  showCurrentRowCurve: boolean = true;
@@ -20,11 +22,16 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
20
22
  mergeColumnSeries: boolean = false;
21
23
  rows: number[] = [];
22
24
  data: IFitChartData = new FitChartData();
25
+ logX?: boolean;
26
+ logY?: boolean;
27
+ allowXZeroes?: boolean;
28
+ mergeCellSeries?: boolean;
29
+ showColumnLabel?: boolean;
23
30
 
24
31
  constructor() {
25
32
  super(new FitChartCellRenderer());
26
33
 
27
- this.curvesColumnNames = this.addProperty('curvesColumnNames', DG.TYPE.COLUMN_LIST);
34
+ this.curvesColumnNames = this.addProperty('curvesColumnNames', DG.TYPE.COLUMN_LIST, [], {semType: FIT_SEM_TYPE});
28
35
 
29
36
  this.showSelectedRowsCurves = this.bool('showSelectedRowsCurves', false, { description: 'Adds curves from the selected rows'});
30
37
  this.showCurrentRowCurve = this.bool('showCurrentRowCurve', true);
@@ -46,29 +53,57 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
46
53
  mergeProperties(fitChartDataProperties, this, this.data.chartOptions);
47
54
  }
48
55
 
56
+ mergeViewerChartOptions(): void {
57
+ for (const p of fitChartDataProperties) {
58
+ const isDefaultValueChanged = ['logX', 'logY', 'allowXZeroes', 'mergeCellSeries', 'showColumnLabel'].includes(p.name === 'mergeSeries' ?
59
+ 'mergeCellSeries' : p.name) ? this[p.name === 'mergeSeries' ? 'mergeCellSeries' : p.name] !== undefined :
60
+ this.props.get(p.name) !== undefined && this.props.get(p.name) !== p.defaultValue;
61
+ if (isDefaultValueChanged) {
62
+ if (['title', 'xAxisName', 'yAxisName'].includes(p.name) && this.props.get(p.name) as unknown as string === '')
63
+ continue;
64
+ else if (['logX', 'logY', 'allowXZeroes', 'mergeCellSeries', 'showColumnLabel'].includes(p.name === 'mergeSeries' ? 'mergeCellSeries' : p.name))
65
+ this.data.chartOptions![p.name as keyof IFitChartOptions] = this[p.name];
66
+ else
67
+ this.data.chartOptions![p.name as keyof IFitChartOptions] = this.props.get(p.name) as unknown as any;
68
+ }
69
+ }
70
+ }
71
+
49
72
  createChartData(): void {
50
73
  if (this.curvesColumnNames?.length === 0)
51
74
  return;
52
75
  this.rows.length = 0;
76
+ let selectionStart = -1;
53
77
  if (this.showCurrentRowCurve && this.dataFrame.currentRowIdx !== -1)
54
78
  this.rows.push(this.dataFrame.currentRowIdx);
55
79
  if (this.showMouseOverRowCurve && this.dataFrame.mouseOverRowIdx !== -1)
56
80
  this.rows.push(this.dataFrame.mouseOverRowIdx);
57
- if (this.showSelectedRowsCurves && this.dataFrame.mouseOverRowIdx !== -1)
81
+ if (this.showSelectedRowsCurves && this.dataFrame.mouseOverRowIdx !== -1) {
82
+ selectionStart = this.rows.length;
58
83
  this.rows.push(...this.dataFrame.selection.getSelectedIndexes());
84
+ }
59
85
 
60
86
  this.data = new FitChartData();
61
87
  const grid = this.tableView?.grid!;
62
- this.data.chartOptions!.showColumnLabel = this.props.get('showColumnLabel') as unknown as boolean;
63
88
  const mergeCellSeries = this.props.get('mergeCellSeries') as unknown as boolean;
89
+ const chartOptions: IFitChartOptions[] = [];
64
90
  for (const colName of this.curvesColumnNames!) {
65
91
  const series = [];
66
92
  for (const i of new Set(this.rows)) {
67
93
  const gridCell = grid.cell(colName, grid.tableRowToGrid(i));
94
+ if (gridCell.cell.value === '')
95
+ continue;
68
96
  const cellCurves = getChartData(gridCell);
69
97
  cellCurves.series?.forEach((series) => series.columnName = gridCell.cell.column.name);
70
- if (mergeCellSeries)
71
- cellCurves.series = [mergeSeries(cellCurves.series!)!];
98
+ const currentChartOptions = cellCurves.chartOptions;
99
+ if (currentChartOptions !== undefined && currentChartOptions !== null)
100
+ chartOptions[chartOptions.length] = currentChartOptions;
101
+ if (mergeCellSeries) {
102
+ const mergedSeries = mergeSeries(cellCurves.series!)!;
103
+ if (currentChartOptions?.title !== undefined && currentChartOptions?.title !== '')
104
+ mergedSeries.name = currentChartOptions?.title;
105
+ cellCurves.series = [mergedSeries];
106
+ }
72
107
  series.push(...cellCurves.series!);
73
108
  }
74
109
  if (this.mergeColumnSeries)
@@ -76,6 +111,8 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
76
111
  else
77
112
  this.data.series?.push(...series);
78
113
  }
114
+ this.data.chartOptions = mergeChartOptions(chartOptions);
115
+ this.mergeViewerChartOptions();
79
116
  this.data.series?.forEach((series, i) => {
80
117
  series.pointColor = DG.Color.toHtml(DG.Color.getCategoricalColor(this.data.series?.length! > 20 ? 0 : i));
81
118
  series.fitLineColor = DG.Color.toHtml(DG.Color.getCategoricalColor(this.data.series?.length! > 20 ? 0 : i));
@@ -85,12 +122,20 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
85
122
  series.showPoints = '';
86
123
  series.lineStyle = 'solid';
87
124
  }
125
+ if (this.data.series?.length! > 10 && this.data.series?.length! < 100 && i >= selectionStart) {
126
+ const color = DG.Color.fromHtml(series.fitLineColor);
127
+ series.fitLineColor = `rgba(${DG.Color.r(color)}, ${DG.Color.g(color)}, ${DG.Color.b(color)}, 0.2)`;
128
+ }
88
129
  });
89
130
  }
90
131
 
91
132
  onPropertyChanged(property: DG.Property | null): void {
92
- if (property?.name === 'curvesColumnNames')
133
+ if (property?.name === 'curvesColumnNames') {
93
134
  this.props.set('showColumnLabel', this.curvesColumnNames?.length! > 1 as unknown as object);
135
+ this.showColumnLabel = this.curvesColumnNames?.length! > 1;
136
+ }
137
+ if (['logX', 'logY', 'allowXZeroes', 'mergeCellSeries', 'showColumnLabel'].includes(property?.name === 'mergeSeries' ? 'mergeCellSeries' : property?.name ?? ''))
138
+ this[property?.name === 'mergeSeries' ? 'mergeCellSeries' : property?.name ?? ''] = this[property?.name ?? ''];
94
139
  this.applyViewerProperties();
95
140
  this.createChartData();
96
141
  this.render();
@@ -129,6 +174,6 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
129
174
  return;
130
175
  }
131
176
  this._removeErrorMessage();
132
- this.renderer.renderCurves(g, 0, 0, this.canvas.width, this.canvas.height, this.data);
177
+ this.renderer.renderCurves(g, new DG.Rect(0, 0, this.canvas.width, this.canvas.height).inflate(FitConstants.INFLATE_SIZE, FitConstants.INFLATE_SIZE), this.data);
133
178
  }
134
179
  }
@@ -0,0 +1,476 @@
1
+ import * as DG from 'datagrok-api/dg';
2
+ import {
3
+ CONFIDENCE_INTERVAL_FILL_COLOR,
4
+ CONFIDENCE_INTERVAL_STROKE_COLOR,
5
+ CURVE_CONFIDENCE_INTERVAL_BOUNDS,
6
+ FitConfidenceIntervals,
7
+ FitFunction,
8
+ IFitChartData,
9
+ IFitSeries,
10
+ statisticsProperties
11
+ } from "@datagrok-libraries/statistics/src/fit/fit-curve";
12
+ import {
13
+ getSeriesConfidenceInterval,
14
+ getSeriesStatistics,
15
+ LogOptions
16
+ } from "@datagrok-libraries/statistics/src/fit/fit-data";
17
+ import {Viewport} from "@datagrok-libraries/utils/src/transform";
18
+ import {FitConstants} from "./const";
19
+ import {BoxPlotStatistics, calculateBoxPlotStatistics} from "@datagrok-libraries/statistics/src/box-plot-statistics";
20
+ import {StringUtils} from "@datagrok-libraries/utils/src/string-utils";
21
+
22
+
23
+ interface FitRenderOptions {
24
+ viewport: Viewport;
25
+ ratio?: number;
26
+ }
27
+
28
+ interface FitPointRenderOptions extends FitRenderOptions {
29
+ x: number;
30
+ color: string;
31
+ boxPlotStats: BoxPlotStatistics;
32
+ }
33
+
34
+ interface FitLineRenderOptions extends FitRenderOptions {
35
+ logOptions: LogOptions;
36
+ showAxes: boolean;
37
+ showAxesLabels: boolean;
38
+ screenBounds: DG.Rect;
39
+ curveFunc?: (x: number) => number;
40
+ }
41
+
42
+ interface FitConfidenceIntervalRenderOptions extends FitLineRenderOptions {
43
+ fitFunc?: FitFunction;
44
+ userParamsFlag?: boolean;
45
+ confidenceIntervals?: FitConfidenceIntervals;
46
+ confidenceType?: string;
47
+ }
48
+
49
+ interface FitDroplineRenderOptions extends FitRenderOptions {
50
+ showDroplines?: boolean;
51
+ xValue: number;
52
+ dataBounds: DG.Rect;
53
+ curveFunc: (x: number) => number;
54
+ logOptions: LogOptions;
55
+ }
56
+
57
+ interface FitStatisticsRenderOptions {
58
+ statistics?: string[];
59
+ fitFunc: FitFunction;
60
+ logOptions: LogOptions;
61
+ dataBox: DG.Rect;
62
+ }
63
+
64
+ interface FitTitleRenderOptions {
65
+ showTitle: boolean;
66
+ title?: string;
67
+ dataBox: DG.Rect;
68
+ screenBounds: DG.Rect;
69
+ }
70
+
71
+ interface FitAxesLabelsRenderOptions extends FitTitleRenderOptions {
72
+ showAxesLabels: boolean;
73
+ xAxisName?: string;
74
+ yAxisName?: string;
75
+ }
76
+
77
+ interface FitLegendRenderOptions {
78
+ showLegend: boolean;
79
+ dataBox: DG.Rect;
80
+ ratio: number;
81
+ }
82
+
83
+
84
+ export function assignSeriesColors(series: IFitSeries, seriesIdx: number): void {
85
+ const pointColor = series.pointColor ? DG.Color.fromHtml(series.pointColor) ? series.pointColor :
86
+ DG.Color.toHtml(DG.Color.getCategoricalColor(seriesIdx)) : DG.Color.toHtml(DG.Color.getCategoricalColor(seriesIdx));
87
+ const outlierColor = series.outlierColor ? DG.Color.fromHtml(series.outlierColor) ?
88
+ series.outlierColor : DG.Color.toHtml(DG.Color.red) : DG.Color.toHtml(DG.Color.red);
89
+ const fitLineColor = series.fitLineColor ? DG.Color.fromHtml(series.fitLineColor) ? series.fitLineColor :
90
+ DG.Color.toHtml(DG.Color.getCategoricalColor(seriesIdx)) : DG.Color.toHtml(DG.Color.getCategoricalColor(seriesIdx));
91
+ series.pointColor = pointColor;
92
+ series.outlierColor = outlierColor;
93
+ series.fitLineColor = fitLineColor;
94
+ }
95
+
96
+ export function renderConnectDots(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitRenderOptions): void {
97
+ if (series.connectDots ?? false) {
98
+ g.strokeStyle = series.pointColor!;
99
+ g.lineWidth = 2 * renderOptions.ratio!;
100
+ g.beginPath();
101
+ for (let i = 0; i < series.points.length; i++) {
102
+ const x = series.points[i].x;
103
+ const y = series.points[i].y;
104
+ const screenX = renderOptions.viewport.xToScreen(x);
105
+ const screenY = renderOptions.viewport.yToScreen(y);
106
+ if (i === 0)
107
+ g.moveTo(screenX, screenY);
108
+ else
109
+ g.lineTo(screenX, screenY);
110
+ }
111
+ g.stroke();
112
+ }
113
+ }
114
+
115
+ export function renderPoints(g: CanvasRenderingContext2D, series: IFitSeries, options: FitRenderOptions) {
116
+ if (series.showPoints ?? 'points') {
117
+ g.strokeStyle = series.pointColor!;
118
+ if ((series.connectDots && series.showPoints !== '') || series.showPoints === 'points')
119
+ drawPoints(g, series, options);
120
+ else if (['candlesticks', 'both'].includes(series.showPoints!))
121
+ drawCandles(g, series, options);
122
+ }
123
+ }
124
+
125
+ /** Performs points drawing */
126
+ function drawPoints(g: CanvasRenderingContext2D, series: IFitSeries, options: FitRenderOptions): void {
127
+ for (let i = 0; i < series.points.length!; i++) {
128
+ const p = series.points[i];
129
+ const color = series.connectDots ? series.pointColor! :
130
+ p.outlier ? (p.outlierColor ? DG.Color.fromHtml(p.outlierColor) ? p.outlierColor : series.outlierColor! : series.outlierColor!) :
131
+ (p.color ? DG.Color.fromHtml(p.color) ? p.color : series.pointColor! : series.pointColor!);
132
+ const marker = p.marker ? p.marker as DG.MARKER_TYPE : series.markerType as DG.MARKER_TYPE;
133
+ const size = !series.connectDots ? p.outlier ? FitConstants.OUTLIER_PX_SIZE * options.ratio! :
134
+ p.size ? p.size : FitConstants.POINT_PX_SIZE * options.ratio! : FitConstants.POINT_PX_SIZE * options.ratio!;
135
+ DG.Paint.marker(g, !series.connectDots ? p.outlier ? DG.MARKER_TYPE.OUTLIER : marker : marker,
136
+ options.viewport.xToScreen(p.x), options.viewport.yToScreen(p.y), color, size);
137
+ if (p.stdev && !p.outlier) {
138
+ g.strokeStyle = color;
139
+ g.beginPath();
140
+ g.moveTo(options.viewport.xToScreen(p.x), options.viewport.yToScreen(p.y + p.stdev));
141
+ g.lineTo(options.viewport.xToScreen(p.x), options.viewport.yToScreen(p.y - p.stdev));
142
+ g.stroke();
143
+ }
144
+ }
145
+ }
146
+
147
+ /** Performs candles drawing */
148
+ function drawCandles(g: CanvasRenderingContext2D, series: IFitSeries, options: FitRenderOptions) : void {
149
+ for (let i = 0, candleStart = null; i < series.points.length!; i++) {
150
+ const p = series.points[i];
151
+ if (p.outlier)
152
+ continue;
153
+ const nextSame = i + 1 < series.points.length && series.points[i + 1].x === p.x;
154
+ if (!candleStart && nextSame)
155
+ candleStart = i;
156
+ else if (candleStart !== null && !nextSame) {
157
+ const values: number[] = [];
158
+ for (let j = candleStart, ind = 0; j <= i; j++, ind++)
159
+ values[ind] = series.points[j].y;
160
+ const boxPlotStats = calculateBoxPlotStatistics(values);
161
+
162
+ g.beginPath();
163
+ drawCandlestick(g, {viewport: options.viewport, ratio: options.ratio!, x: p.x,
164
+ boxPlotStats: boxPlotStats, color: series.pointColor!});
165
+ g.stroke();
166
+
167
+ if (series.showPoints === 'both') {
168
+ for (let ind = 0; ind < values.length; ind++) {
169
+ if (values[ind] < boxPlotStats.lowerAdjacentValue || values[ind] > boxPlotStats.upperAdjacentValue) {
170
+ DG.Paint.marker(g, DG.MARKER_TYPE.OUTLIER,
171
+ options.viewport.xToScreen(p.x), options.viewport.yToScreen(values[ind]),
172
+ series.pointColor!, FitConstants.CANDLESTICK_OUTLIER_PX_SIZE * options.ratio!);
173
+ }
174
+ }
175
+ }
176
+
177
+ candleStart = null;
178
+ }
179
+ }
180
+ }
181
+
182
+ /** Performs candlestick drawing */
183
+ function drawCandlestick(g: CanvasRenderingContext2D, renderOptions: FitPointRenderOptions): void {
184
+ drawCandlestickBorder(g, renderOptions.x, renderOptions.boxPlotStats.lowerAdjacentValue, renderOptions.viewport);
185
+ g.moveTo(renderOptions.viewport.xToScreen(renderOptions.x), renderOptions.viewport.yToScreen(renderOptions.boxPlotStats.lowerAdjacentValue));
186
+ g.lineTo(renderOptions.viewport.xToScreen(renderOptions.x), renderOptions.viewport.yToScreen(renderOptions.boxPlotStats.upperAdjacentValue));
187
+ drawCandlestickBorder(g, renderOptions.x, renderOptions.boxPlotStats.upperAdjacentValue, renderOptions.viewport);
188
+ DG.Paint.marker(g, DG.MARKER_TYPE.CIRCLE, renderOptions.viewport.xToScreen(renderOptions.x),
189
+ renderOptions.viewport.yToScreen(renderOptions.boxPlotStats.q2), renderOptions.color,
190
+ FitConstants.CANDLESTICK_MEDIAN_PX_SIZE * renderOptions.ratio!);
191
+ }
192
+
193
+ /** Performs candlestick border drawing */
194
+ function drawCandlestickBorder(g: CanvasRenderingContext2D, x: number, adjacentValue: number, transform: Viewport): void {
195
+ g.moveTo(transform.xToScreen(x) - (FitConstants.CANDLESTICK_BORDER_PX_SIZE / 2), transform.yToScreen(adjacentValue));
196
+ g.lineTo(transform.xToScreen(x) + (FitConstants.CANDLESTICK_BORDER_PX_SIZE / 2), transform.yToScreen(adjacentValue));
197
+ }
198
+
199
+ export function renderFitLine(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitLineRenderOptions): void {
200
+ if (series.showFitLine ?? true) {
201
+ g.save();
202
+ g.strokeStyle = series.fitLineColor!;
203
+ g.lineWidth = 2 * renderOptions.ratio!;
204
+ g.beginPath();
205
+ if (series.lineStyle)
206
+ g.setLineDash(FitConstants.LINE_STYLES[series.lineStyle]);
207
+
208
+ const axesLeftPxMargin = renderOptions.showAxes ? renderOptions.showAxesLabels ?
209
+ FitConstants.AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS : FitConstants.AXES_LEFT_PX_MARGIN : 0;
210
+ const axesRightPxMargin = renderOptions.showAxes ? FitConstants.AXES_RIGHT_PX_MARGIN : 0;
211
+ for (let j = axesLeftPxMargin; j <= renderOptions.screenBounds.width - axesRightPxMargin; j++) {
212
+ const x = renderOptions.screenBounds.x + j;
213
+ const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) :
214
+ renderOptions.viewport.xToWorld(x);
215
+ const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(Math.pow(10, renderOptions.curveFunc!(xForY))) :
216
+ renderOptions.viewport.yToScreen(renderOptions.curveFunc!(xForY));
217
+ if (j === axesLeftPxMargin)
218
+ g.moveTo(x, y);
219
+ else
220
+ g.lineTo(x, y);
221
+ }
222
+ g.stroke();
223
+ g.restore();
224
+ }
225
+ }
226
+
227
+ export function renderConfidenceIntervals(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitConfidenceIntervalRenderOptions): void {
228
+ if ((series.showFitLine ?? true) && (series.showCurveConfidenceInterval ?? false)) {
229
+ // TODO: improve confidence intervals colors
230
+ g.strokeStyle = series.confidenceIntervalColor ?? CONFIDENCE_INTERVAL_STROKE_COLOR;
231
+ g.fillStyle = series.confidenceIntervalColor ?? CONFIDENCE_INTERVAL_FILL_COLOR;
232
+
233
+ const confidenceIntervals = getSeriesConfidenceInterval(series, renderOptions.fitFunc!,
234
+ renderOptions.userParamsFlag!, renderOptions.logOptions);
235
+ drawConfidenceInterval(g, {viewport: renderOptions.viewport, logOptions: renderOptions.logOptions,
236
+ showAxes: renderOptions.showAxes, showAxesLabels: renderOptions.showAxesLabels, screenBounds: renderOptions.screenBounds,
237
+ confidenceIntervals: confidenceIntervals, confidenceType: CURVE_CONFIDENCE_INTERVAL_BOUNDS.TOP});
238
+ drawConfidenceInterval(g, {viewport: renderOptions.viewport, logOptions: renderOptions.logOptions,
239
+ showAxes: renderOptions.showAxes, showAxesLabels: renderOptions.showAxesLabels, screenBounds: renderOptions.screenBounds,
240
+ confidenceIntervals: confidenceIntervals, confidenceType: CURVE_CONFIDENCE_INTERVAL_BOUNDS.BOTTOM});
241
+ fillConfidenceInterval(g, {viewport: renderOptions.viewport, logOptions: renderOptions.logOptions,
242
+ showAxes: renderOptions.showAxes, showAxesLabels: renderOptions.showAxesLabels, screenBounds: renderOptions.screenBounds,
243
+ confidenceIntervals: confidenceIntervals});
244
+ }
245
+ }
246
+
247
+ /** Performs a curve confidence interval drawing */
248
+ function drawConfidenceInterval(g: CanvasRenderingContext2D, renderOptions: FitConfidenceIntervalRenderOptions): void {
249
+ g.beginPath();
250
+ const axesLeftPxMargin = renderOptions.showAxes ? renderOptions.showAxesLabels ?
251
+ FitConstants.AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS : FitConstants.AXES_LEFT_PX_MARGIN : 0;
252
+ const axesRightPxMargin = renderOptions.showAxes ? FitConstants.AXES_RIGHT_PX_MARGIN : 0;
253
+ const confIntervalFunc = renderOptions.confidenceType === CURVE_CONFIDENCE_INTERVAL_BOUNDS.TOP ?
254
+ renderOptions.confidenceIntervals!.confidenceTop : renderOptions.confidenceIntervals!.confidenceBottom;
255
+ for (let i = axesLeftPxMargin; i <= renderOptions.screenBounds.width - axesRightPxMargin; i++) {
256
+ const x = renderOptions.screenBounds.x + i;
257
+ const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) : renderOptions.viewport.xToWorld(x);
258
+ const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(Math.pow(10, confIntervalFunc(xForY))) :
259
+ renderOptions.viewport.yToScreen(confIntervalFunc(xForY));
260
+ if (i === axesLeftPxMargin)
261
+ g.moveTo(x, y);
262
+ else
263
+ g.lineTo(x, y);
264
+ }
265
+ g.stroke();
266
+ }
267
+
268
+ /** Performs a curve confidence interval color filling */
269
+ function fillConfidenceInterval(g: CanvasRenderingContext2D, renderOptions: FitConfidenceIntervalRenderOptions): void {
270
+ g.beginPath();
271
+ const axesLeftPxMargin = renderOptions.showAxes ? renderOptions.showAxesLabels ?
272
+ FitConstants.AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS : FitConstants.AXES_LEFT_PX_MARGIN : 0;
273
+ const axesRightPxMargin = renderOptions.showAxes ? FitConstants.AXES_RIGHT_PX_MARGIN : 0;
274
+ for (let i = axesLeftPxMargin; i <= renderOptions.screenBounds.width - axesRightPxMargin; i++) {
275
+ const x = renderOptions.screenBounds.x + i;
276
+ const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) :
277
+ renderOptions.viewport.xToWorld(x);
278
+ const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(
279
+ Math.pow(10, renderOptions.confidenceIntervals!.confidenceTop(xForY))) :
280
+ renderOptions.viewport.yToScreen(renderOptions.confidenceIntervals!.confidenceTop(xForY));
281
+ if (i === axesLeftPxMargin)
282
+ g.moveTo(x, y);
283
+ else
284
+ g.lineTo(x, y);
285
+ }
286
+
287
+ // reverse traverse to make a shape of confidence interval to fill it
288
+ for (let i = renderOptions.screenBounds.width - axesRightPxMargin; i >= axesLeftPxMargin; i--) {
289
+ const x = renderOptions.screenBounds.x + i;
290
+ const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) :
291
+ renderOptions.viewport.xToWorld(x);
292
+ const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(
293
+ Math.pow(10, renderOptions.confidenceIntervals!.confidenceBottom(xForY))) :
294
+ renderOptions.viewport.yToScreen(renderOptions.confidenceIntervals!.confidenceBottom(xForY));
295
+ g.lineTo(x, y);
296
+ }
297
+ g.closePath();
298
+ g.fill();
299
+ }
300
+
301
+ export function renderDroplines(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitDroplineRenderOptions): void {
302
+ if ((series.showFitLine ?? true) && series.droplines && renderOptions.showDroplines!) {
303
+ g.save();
304
+ g.strokeStyle = 'blue';
305
+ g.lineWidth = renderOptions.ratio!;
306
+ g.beginPath();
307
+ g.setLineDash([5, 5]);
308
+ for (let j = 0; j < series.droplines.length; j++) {
309
+ const droplineName = series.droplines[j];
310
+ if (droplineName === 'IC50')
311
+ drawDropline(g, {viewport: renderOptions.viewport, xValue: series.parameters![2],
312
+ dataBounds: renderOptions.dataBounds, curveFunc: renderOptions.curveFunc, logOptions: renderOptions.logOptions});
313
+ }
314
+ g.stroke();
315
+ g.restore();
316
+ }
317
+ }
318
+
319
+ /** Performs a dropline drawing */
320
+ function drawDropline(g: CanvasRenderingContext2D, renderOptions: FitDroplineRenderOptions): void {
321
+ if (renderOptions.logOptions.logX)
322
+ renderOptions.xValue = Math.pow(10, renderOptions.xValue);
323
+ const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.xValue) : renderOptions.xValue;
324
+ const y = renderOptions.logOptions.logY ? Math.pow(10, renderOptions.curveFunc(xForY)) : renderOptions.curveFunc(xForY);
325
+ g.moveTo(renderOptions.viewport.xToScreen(renderOptions.dataBounds.minX), renderOptions.viewport.yToScreen(y));
326
+ g.lineTo(renderOptions.viewport.xToScreen(renderOptions.xValue), renderOptions.viewport.yToScreen(y));
327
+ g.lineTo(renderOptions.viewport.xToScreen(renderOptions.xValue), renderOptions.viewport.yToScreen(renderOptions.dataBounds.minY));
328
+ }
329
+
330
+ export function renderStatistics(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitStatisticsRenderOptions): void {
331
+ if ((series.showFitLine ?? true) && renderOptions.statistics) {
332
+ const statistics = getSeriesStatistics(series, renderOptions.fitFunc, renderOptions.logOptions);
333
+ for (let i = 0; i < renderOptions.statistics.length; i++) {
334
+ const statName = renderOptions.statistics[i];
335
+ const prop = statisticsProperties.find((p) => p.name === statName);
336
+ if (prop) {
337
+ const s = StringUtils.formatNumber(prop.get(statistics));
338
+ g.fillStyle = series.fitLineColor!;
339
+ g.textAlign = 'left';
340
+ g.fillText(prop.name + ': ' + s, renderOptions.dataBox.x + 5, renderOptions.dataBox.y + 20 + 20 * i);
341
+ }
342
+ }
343
+ }
344
+ }
345
+
346
+ export function renderTitle(g: CanvasRenderingContext2D, renderOptions: FitTitleRenderOptions): void {
347
+ if (renderOptions.showTitle) {
348
+ g.font = '12px Roboto, "Roboto Local"';
349
+ g.textAlign = 'center';
350
+ g.fillStyle = 'black';
351
+ g.fillText(renderOptions.title!, renderOptions.dataBox.midX - 5, renderOptions.screenBounds.y + 15);
352
+ }
353
+ }
354
+
355
+ export function renderAxesLabels(g: CanvasRenderingContext2D, renderOptions: FitAxesLabelsRenderOptions): void {
356
+ if (renderOptions.showAxesLabels) {
357
+ g.font = '11px Roboto, "Roboto Local"';
358
+ g.textAlign = 'center';
359
+ g.fillStyle = 'black';
360
+ g.fillText(renderOptions.xAxisName!, renderOptions.dataBox.midX - 5,
361
+ renderOptions.screenBounds.maxY - FitConstants.X_AXIS_LABEL_BOTTOM_PX_MARGIN);
362
+ g.translate(renderOptions.screenBounds.x, renderOptions.screenBounds.y);
363
+ g.rotate(-Math.PI / 2);
364
+ const axesTopPxMargin = renderOptions.showTitle ? FitConstants.AXES_TOP_PX_MARGIN_WITH_TITLE : FitConstants.AXES_TOP_PX_MARGIN;
365
+ g.fillText(renderOptions.yAxisName!, -(renderOptions.dataBox.height / 2 + axesTopPxMargin + 15), 15);
366
+ g.restore();
367
+ }
368
+ }
369
+
370
+ export function renderLegend(g: CanvasRenderingContext2D, data: IFitChartData, renderOptions: FitLegendRenderOptions): void {
371
+ if (renderOptions.showLegend) {
372
+ g.font = '11px Roboto, "Roboto Local"';
373
+ const columnNames = [...new Set(data.series?.map((series) => series.columnName))]
374
+ .filter((colName) => colName !== null && colName !== undefined);
375
+ let drawnCurvesInLegend = 0;
376
+ for (let i = 0; i < columnNames.length; i++) {
377
+ const colName = columnNames[i];
378
+
379
+ if (data.chartOptions?.showColumnLabel) {
380
+ g.beginPath();
381
+ g.fillStyle = 'black';
382
+ const colNameWidth = g.measureText(colName!).width;
383
+ g.fillText(colName!, renderOptions.dataBox.maxX - colNameWidth, renderOptions.dataBox.y +
384
+ FitConstants.LEGEND_TOP_PX_MARGIN + (i * FitConstants.LEGEND_RECORD_PX_HEIGHT + FitConstants.LEGEND_RECORD_PX_HEIGHT * drawnCurvesInLegend));
385
+ }
386
+
387
+ const series = data.series?.filter((series) => series.columnName === colName);
388
+ for (let j = 0; j < series?.length!; j++) {
389
+ const currentSeries = series![j];
390
+ if (currentSeries.name === '' || currentSeries.name === null || currentSeries.name === undefined)
391
+ continue;
392
+
393
+ g.beginPath();
394
+ g.strokeStyle = currentSeries.fitLineColor!;
395
+ g.lineWidth = 2 * renderOptions.ratio;
396
+ const textWidth = g.measureText(currentSeries.name).width;
397
+ g.moveTo(renderOptions.dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_PX_WIDTH - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN,
398
+ renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN - FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN +
399
+ (data.chartOptions?.showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT * (i + 1) : 0) + FitConstants.LEGEND_RECORD_PX_HEIGHT * drawnCurvesInLegend);
400
+ g.lineTo(renderOptions.dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN,
401
+ renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN - FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN +
402
+ (data.chartOptions?.showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT * (i + 1) : 0) + FitConstants.LEGEND_RECORD_PX_HEIGHT * drawnCurvesInLegend);
403
+ const marker = currentSeries.markerType ? currentSeries.markerType as DG.MARKER_TYPE : DG.MARKER_TYPE.CIRCLE;
404
+ DG.Paint.marker(g, marker, renderOptions.dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN - FitConstants.LEGEND_RECORD_LINE_PX_WIDTH / 2,
405
+ renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN - FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN +
406
+ (data.chartOptions?.showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT * (i + 1) : 0) + FitConstants.LEGEND_RECORD_PX_HEIGHT * drawnCurvesInLegend,
407
+ currentSeries.pointColor!, FitConstants.POINT_PX_SIZE * renderOptions.ratio);
408
+ g.fillStyle = currentSeries.fitLineColor!;
409
+ g.fillText(currentSeries.name, renderOptions.dataBox.maxX - textWidth,
410
+ renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN +
411
+ (data.chartOptions?.showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT * (i + 1) : 0) + FitConstants.LEGEND_RECORD_PX_HEIGHT * drawnCurvesInLegend);
412
+ g.stroke();
413
+ drawnCurvesInLegend++;
414
+ }
415
+ }
416
+ g.restore();
417
+
418
+
419
+
420
+
421
+ // let drawnCurvesInLegend = 0;
422
+ // const columnNames = new Set(data.series?.map((series) => series.columnName));
423
+ // [...columnNames].forEach((columnName, columnNameIndex) => {
424
+ // const series = data.series?.filter((series) => series.columnName === columnName);
425
+ // const getOffset = () => data.chartOptions?.showColumnLabel ? 20 : 0;
426
+ // if (data.chartOptions?.showColumnLabel) {
427
+ // g.beginPath();
428
+ // g.fillStyle = 'black';
429
+ // const colNameWidth = g.measureText(columnName ?? '').width;
430
+ // g.fillText(columnName ?? '', renderOptions.dataBox.maxX - colNameWidth,
431
+ // renderOptions.dataBox.y + 20 + (20 * columnNameIndex + 20 * drawnCurvesInLegend));
432
+ // }
433
+ // for (let i = 0; i < series?.length!; i++) {
434
+ // g.beginPath();
435
+ // const currentSeries = series![i];
436
+ // g.strokeStyle = currentSeries.fitLineColor!;
437
+ // g.lineWidth = 2 * renderOptions.ratio;
438
+ // const text = `${currentSeries.name ?? ''}`;
439
+ // const textWidth = g.measureText(text).width;
440
+ // g.moveTo(renderOptions.dataBox.maxX - textWidth - 25,
441
+ // renderOptions.dataBox.y + 20 - 4 + (20 * columnNameIndex + getOffset() + 20 * drawnCurvesInLegend));
442
+ // g.lineTo(renderOptions.dataBox.maxX - textWidth - 5,
443
+ // renderOptions.dataBox.y + 20 - 4 + (20 * columnNameIndex + getOffset() + 20 * drawnCurvesInLegend));
444
+ // const marker = currentSeries.markerType ? currentSeries.markerType as DG.MARKER_TYPE : DG.MARKER_TYPE.CIRCLE;
445
+ // DG.Paint.marker(g, marker, renderOptions.dataBox.maxX - textWidth - 25 + (20 / 2),
446
+ // renderOptions.dataBox.y + 20 - 4 + (20 * columnNameIndex + getOffset() + 20 * drawnCurvesInLegend),
447
+ // currentSeries.pointColor!, FitConstants.POINT_PX_SIZE * renderOptions.ratio);
448
+ // g.fillStyle = currentSeries.fitLineColor!;
449
+ // g.fillText(text, renderOptions.dataBox.maxX - textWidth,
450
+ // renderOptions.dataBox.y + 20 + (20 * columnNameIndex + getOffset() + 20 * drawnCurvesInLegend));
451
+ // g.stroke();
452
+ // drawnCurvesInLegend++;
453
+ // }
454
+ // });
455
+
456
+ }
457
+ }
458
+
459
+ // function renderLegendSeries(g: CanvasRenderingContext2D, series: IFitSeries): void {
460
+ // g.beginPath();
461
+ // g.strokeStyle = series.fitLineColor!;
462
+ // g.lineWidth = 2 * renderOptions.ratio;
463
+ // const textWidth = g.measureText(series.name!).width;
464
+ // g.moveTo(renderOptions.dataBox.maxX - textWidth - 25,
465
+ // renderOptions.dataBox.y + 20 - 4 + (20 * columnNameIndex + getOffset() + 20 * drawnCurvesInLegend));
466
+ // g.lineTo(renderOptions.dataBox.maxX - textWidth - 5,
467
+ // renderOptions.dataBox.y + 20 - 4 + (20 * columnNameIndex + getOffset() + 20 * drawnCurvesInLegend));
468
+ // const marker = series.markerType ? currentSeries.markerType as DG.MARKER_TYPE : DG.MARKER_TYPE.CIRCLE;
469
+ // DG.Paint.marker(g, marker, renderOptions.dataBox.maxX - textWidth - 25 + (20 / 2),
470
+ // renderOptions.dataBox.y + 20 - 4 + (20 * columnNameIndex + getOffset() + 20 * drawnCurvesInLegend),
471
+ // series.pointColor!, FitConstants.POINT_PX_SIZE * renderOptions.ratio);
472
+ // g.fillStyle = series.fitLineColor!;
473
+ // g.fillText(text, renderOptions.dataBox.maxX - textWidth,
474
+ // renderOptions.dataBox.y + 20 + (20 * columnNameIndex + getOffset() + 20 * drawnCurvesInLegend));
475
+ // g.stroke();
476
+ // }
package/src/package.ts CHANGED
@@ -2,12 +2,13 @@
2
2
  import * as DG from 'datagrok-api/dg';
3
3
 
4
4
  import {FitGridCellHandler, calculateSeriesStats, getChartDataAggrStats} from './fit/fit-grid-cell-handler';
5
- import {FitChartCellRenderer, TAG_FIT_CHART_FORMAT, TAG_FIT_CHART_FORMAT_3DX, getChartData, substituteZeroes} from './fit/fit-renderer';
5
+ import {FitChartCellRenderer, getChartData, substituteZeroes} from './fit/fit-renderer';
6
6
  import {MultiCurveViewer} from './fit/multi-curve-viewer';
7
7
  import {curveDemo} from './fit/fit-demo';
8
8
  import {convertXMLToIFitChartData} from './fit/fit-parser';
9
9
  import {LogOptions} from '@datagrok-libraries/statistics/src/fit/fit-data';
10
10
  import {FitStatistics} from '@datagrok-libraries/statistics/src/fit/fit-curve';
11
+ import {FitConstants} from './fit/const';
11
12
 
12
13
 
13
14
  export const _package = new DG.Package();
@@ -68,7 +69,7 @@ export function addStatisticsColumn(df: DG.DataFrame, colName: string, propName:
68
69
  const gridCell = DG.GridCell.fromColumnRow(grid, colName, grid.tableRowToGrid(i));
69
70
  if (gridCell.cell.value === '')
70
71
  return null;
71
- const chartData = gridCell.cell.column.getTag(TAG_FIT_CHART_FORMAT) === TAG_FIT_CHART_FORMAT_3DX ?
72
+ const chartData = gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
72
73
  convertXMLToIFitChartData(gridCell.cell.value) : getChartData(gridCell);
73
74
  if (chartData.series![seriesNumber] === undefined || chartData.series![seriesNumber].points.every((p) => p.outlier))
74
75
  return null;
@@ -101,7 +102,7 @@ export function addAggrStatisticsColumn(df: DG.DataFrame, colName: string, propN
101
102
  const gridCell = DG.GridCell.fromColumnRow(grid, colName, grid.tableRowToGrid(i));
102
103
  if (gridCell.cell.value === '')
103
104
  return null;
104
- const chartData = gridCell.cell.column.getTag(TAG_FIT_CHART_FORMAT) === TAG_FIT_CHART_FORMAT_3DX ?
105
+ const chartData = gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
105
106
  convertXMLToIFitChartData(gridCell.cell.value) : getChartData(gridCell);
106
107
  if (chartData.series?.every((series) => series.points.every((p) => p.outlier)))
107
108
  return null;