@datagrok/curves 1.3.0 → 1.4.0

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 {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: FitConstants.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();
@@ -98,7 +143,7 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
98
143
 
99
144
  onTableAttached(): void {
100
145
  const grid = this.tableView?.grid!;
101
- const fitCol = this.dataFrame.columns.bySemType(FIT_SEM_TYPE);
146
+ const fitCol = this.dataFrame.columns.bySemType(FitConstants.FIT_SEM_TYPE);
102
147
  if (fitCol !== null)
103
148
  this.curvesColumnNames = [fitCol.name];
104
149
 
@@ -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,430 @@
1
+ import * as DG from 'datagrok-api/dg';
2
+ import {
3
+ FitConfidenceIntervals,
4
+ FitFunction,
5
+ IFitChartData,
6
+ IFitSeries,
7
+ statisticsProperties
8
+ } from "@datagrok-libraries/statistics/src/fit/fit-curve";
9
+ import {
10
+ getSeriesConfidenceInterval,
11
+ getSeriesStatistics,
12
+ LogOptions
13
+ } from "@datagrok-libraries/statistics/src/fit/fit-data";
14
+ import {Viewport} from "@datagrok-libraries/utils/src/transform";
15
+ import {FitConstants} from "./const";
16
+ import {BoxPlotStatistics, calculateBoxPlotStatistics} from "@datagrok-libraries/statistics/src/box-plot-statistics";
17
+ import {StringUtils} from "@datagrok-libraries/utils/src/string-utils";
18
+
19
+
20
+ interface FitRenderOptions {
21
+ viewport: Viewport;
22
+ ratio?: number;
23
+ }
24
+
25
+ interface FitPointRenderOptions extends FitRenderOptions {
26
+ x: number;
27
+ color: string;
28
+ boxPlotStats: BoxPlotStatistics;
29
+ }
30
+
31
+ interface FitLineRenderOptions extends FitRenderOptions {
32
+ logOptions: LogOptions;
33
+ showAxes: boolean;
34
+ showAxesLabels: boolean;
35
+ screenBounds: DG.Rect;
36
+ curveFunc?: (x: number) => number;
37
+ }
38
+
39
+ interface FitConfidenceIntervalRenderOptions extends FitLineRenderOptions {
40
+ fitFunc?: FitFunction;
41
+ userParamsFlag?: boolean;
42
+ confidenceIntervals?: FitConfidenceIntervals;
43
+ confidenceType?: string;
44
+ }
45
+
46
+ interface FitDroplineRenderOptions extends FitRenderOptions {
47
+ showDroplines?: boolean;
48
+ xValue: number;
49
+ dataBounds: DG.Rect;
50
+ curveFunc: (x: number) => number;
51
+ logOptions: LogOptions;
52
+ }
53
+
54
+ interface FitStatisticsRenderOptions {
55
+ statistics?: string[];
56
+ fitFunc: FitFunction;
57
+ logOptions: LogOptions;
58
+ dataBox: DG.Rect;
59
+ }
60
+
61
+ interface FitTitleRenderOptions {
62
+ showTitle: boolean;
63
+ title?: string;
64
+ dataBox: DG.Rect;
65
+ screenBounds: DG.Rect;
66
+ }
67
+
68
+ interface FitAxesLabelsRenderOptions extends FitTitleRenderOptions {
69
+ showAxesLabels: boolean;
70
+ xAxisName?: string;
71
+ yAxisName?: string;
72
+ }
73
+
74
+ interface FitLegendRenderOptions {
75
+ showLegend?: boolean;
76
+ dataBox: DG.Rect;
77
+ ratio?: number;
78
+ }
79
+
80
+ interface FitLegendColumnlabelSeriesRenderOptions extends FitLegendRenderOptions {
81
+ columnIdx: number;
82
+ drawnCurvesInLegend: number;
83
+ showColumnLabel?: boolean;
84
+ }
85
+
86
+
87
+ export function assignSeriesColors(series: IFitSeries, seriesIdx: number): void {
88
+ const pointColor = series.pointColor ? DG.Color.fromHtml(series.pointColor) ? series.pointColor :
89
+ DG.Color.toHtml(DG.Color.getCategoricalColor(seriesIdx)) : DG.Color.toHtml(DG.Color.getCategoricalColor(seriesIdx));
90
+ const outlierColor = series.outlierColor ? DG.Color.fromHtml(series.outlierColor) ?
91
+ series.outlierColor : DG.Color.toHtml(DG.Color.red) : DG.Color.toHtml(DG.Color.red);
92
+ const fitLineColor = series.fitLineColor ? DG.Color.fromHtml(series.fitLineColor) ? series.fitLineColor :
93
+ DG.Color.toHtml(DG.Color.getCategoricalColor(seriesIdx)) : DG.Color.toHtml(DG.Color.getCategoricalColor(seriesIdx));
94
+ series.pointColor = pointColor;
95
+ series.outlierColor = outlierColor;
96
+ series.fitLineColor = fitLineColor;
97
+ }
98
+
99
+ export function renderConnectDots(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitRenderOptions): void {
100
+ if (series.connectDots ?? false) {
101
+ g.strokeStyle = series.pointColor!;
102
+ g.lineWidth = 2 * renderOptions.ratio!;
103
+ g.beginPath();
104
+ for (let i = 0; i < series.points.length; i++) {
105
+ const x = series.points[i].x;
106
+ const y = series.points[i].y;
107
+ const screenX = renderOptions.viewport.xToScreen(x);
108
+ const screenY = renderOptions.viewport.yToScreen(y);
109
+ if (i === 0)
110
+ g.moveTo(screenX, screenY);
111
+ else
112
+ g.lineTo(screenX, screenY);
113
+ }
114
+ g.stroke();
115
+ }
116
+ }
117
+
118
+ export function renderPoints(g: CanvasRenderingContext2D, series: IFitSeries, options: FitRenderOptions) {
119
+ if (series.showPoints ?? 'points') {
120
+ g.strokeStyle = series.pointColor!;
121
+ if ((series.connectDots && series.showPoints !== '') || series.showPoints === 'points')
122
+ drawPoints(g, series, options);
123
+ else if (['candlesticks', 'both'].includes(series.showPoints!))
124
+ drawCandles(g, series, options);
125
+ }
126
+ }
127
+
128
+ /** Performs points drawing */
129
+ function drawPoints(g: CanvasRenderingContext2D, series: IFitSeries, options: FitRenderOptions): void {
130
+ for (let i = 0; i < series.points.length!; i++) {
131
+ const p = series.points[i];
132
+ const color = series.connectDots ? series.pointColor! :
133
+ p.outlier ? (p.outlierColor ? DG.Color.fromHtml(p.outlierColor) ? p.outlierColor : series.outlierColor! : series.outlierColor!) :
134
+ (p.color ? DG.Color.fromHtml(p.color) ? p.color : series.pointColor! : series.pointColor!);
135
+ const marker = p.marker ? p.marker as DG.MARKER_TYPE : series.markerType as DG.MARKER_TYPE;
136
+ const size = !series.connectDots ? p.outlier ? FitConstants.OUTLIER_PX_SIZE * options.ratio! :
137
+ p.size ? p.size : FitConstants.POINT_PX_SIZE * options.ratio! : FitConstants.POINT_PX_SIZE * options.ratio!;
138
+ DG.Paint.marker(g, !series.connectDots ? p.outlier ? DG.MARKER_TYPE.OUTLIER : marker : marker,
139
+ options.viewport.xToScreen(p.x), options.viewport.yToScreen(p.y), color, size);
140
+ if (p.stdev && !p.outlier) {
141
+ g.strokeStyle = color;
142
+ g.beginPath();
143
+ g.moveTo(options.viewport.xToScreen(p.x), options.viewport.yToScreen(p.y + p.stdev));
144
+ g.lineTo(options.viewport.xToScreen(p.x), options.viewport.yToScreen(p.y - p.stdev));
145
+ g.stroke();
146
+ }
147
+ }
148
+ }
149
+
150
+ /** Performs candles drawing */
151
+ function drawCandles(g: CanvasRenderingContext2D, series: IFitSeries, options: FitRenderOptions) : void {
152
+ for (let i = 0, candleStart = null; i < series.points.length!; i++) {
153
+ const p = series.points[i];
154
+ if (p.outlier)
155
+ continue;
156
+ const nextSame = i + 1 < series.points.length && series.points[i + 1].x === p.x;
157
+ if (!candleStart && nextSame)
158
+ candleStart = i;
159
+ else if (candleStart !== null && !nextSame) {
160
+ const values: number[] = [];
161
+ for (let j = candleStart, ind = 0; j <= i; j++, ind++)
162
+ values[ind] = series.points[j].y;
163
+ const boxPlotStats = calculateBoxPlotStatistics(values);
164
+
165
+ g.beginPath();
166
+ drawCandlestick(g, {viewport: options.viewport, ratio: options.ratio!, x: p.x,
167
+ boxPlotStats: boxPlotStats, color: series.pointColor!});
168
+ g.stroke();
169
+
170
+ if (series.showPoints === 'both') {
171
+ for (let ind = 0; ind < values.length; ind++) {
172
+ if (values[ind] < boxPlotStats.lowerAdjacentValue || values[ind] > boxPlotStats.upperAdjacentValue) {
173
+ DG.Paint.marker(g, DG.MARKER_TYPE.OUTLIER,
174
+ options.viewport.xToScreen(p.x), options.viewport.yToScreen(values[ind]),
175
+ series.pointColor!, FitConstants.CANDLESTICK_OUTLIER_PX_SIZE * options.ratio!);
176
+ }
177
+ }
178
+ }
179
+
180
+ candleStart = null;
181
+ }
182
+ }
183
+ }
184
+
185
+ /** Performs candlestick drawing */
186
+ function drawCandlestick(g: CanvasRenderingContext2D, renderOptions: FitPointRenderOptions): void {
187
+ drawCandlestickBorder(g, renderOptions.x, renderOptions.boxPlotStats.lowerAdjacentValue, renderOptions.viewport);
188
+ g.moveTo(renderOptions.viewport.xToScreen(renderOptions.x), renderOptions.viewport.yToScreen(renderOptions.boxPlotStats.lowerAdjacentValue));
189
+ g.lineTo(renderOptions.viewport.xToScreen(renderOptions.x), renderOptions.viewport.yToScreen(renderOptions.boxPlotStats.upperAdjacentValue));
190
+ drawCandlestickBorder(g, renderOptions.x, renderOptions.boxPlotStats.upperAdjacentValue, renderOptions.viewport);
191
+ DG.Paint.marker(g, DG.MARKER_TYPE.CIRCLE, renderOptions.viewport.xToScreen(renderOptions.x),
192
+ renderOptions.viewport.yToScreen(renderOptions.boxPlotStats.q2), renderOptions.color,
193
+ FitConstants.CANDLESTICK_MEDIAN_PX_SIZE * renderOptions.ratio!);
194
+ }
195
+
196
+ /** Performs candlestick border drawing */
197
+ function drawCandlestickBorder(g: CanvasRenderingContext2D, x: number, adjacentValue: number, transform: Viewport): void {
198
+ g.moveTo(transform.xToScreen(x) - (FitConstants.CANDLESTICK_BORDER_PX_SIZE / 2), transform.yToScreen(adjacentValue));
199
+ g.lineTo(transform.xToScreen(x) + (FitConstants.CANDLESTICK_BORDER_PX_SIZE / 2), transform.yToScreen(adjacentValue));
200
+ }
201
+
202
+ export function renderFitLine(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitLineRenderOptions): void {
203
+ if (series.showFitLine ?? true) {
204
+ g.save();
205
+ g.strokeStyle = series.fitLineColor!;
206
+ g.lineWidth = 2 * renderOptions.ratio!;
207
+ g.beginPath();
208
+ if (series.lineStyle)
209
+ g.setLineDash(FitConstants.LINE_STYLES[series.lineStyle]);
210
+
211
+ const axesLeftPxMargin = renderOptions.showAxes ? renderOptions.showAxesLabels ?
212
+ FitConstants.AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS : FitConstants.AXES_LEFT_PX_MARGIN : 0;
213
+ const axesRightPxMargin = renderOptions.showAxes ? FitConstants.AXES_RIGHT_PX_MARGIN : 0;
214
+ for (let j = axesLeftPxMargin; j <= renderOptions.screenBounds.width - axesRightPxMargin; j++) {
215
+ const x = renderOptions.screenBounds.x + j;
216
+ const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) :
217
+ renderOptions.viewport.xToWorld(x);
218
+ const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(Math.pow(10, renderOptions.curveFunc!(xForY))) :
219
+ renderOptions.viewport.yToScreen(renderOptions.curveFunc!(xForY));
220
+ if (j === axesLeftPxMargin)
221
+ g.moveTo(x, y);
222
+ else
223
+ g.lineTo(x, y);
224
+ }
225
+ g.stroke();
226
+ g.restore();
227
+ }
228
+ }
229
+
230
+ export function renderConfidenceIntervals(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitConfidenceIntervalRenderOptions): void {
231
+ if ((series.showFitLine ?? true) && (series.showCurveConfidenceInterval ?? false)) {
232
+ // TODO: improve confidence intervals colors
233
+ g.strokeStyle = series.confidenceIntervalColor ?? FitConstants.CONFIDENCE_INTERVAL_STROKE_COLOR;
234
+ g.fillStyle = series.confidenceIntervalColor ?? FitConstants.CONFIDENCE_INTERVAL_FILL_COLOR;
235
+
236
+ const confidenceIntervals = getSeriesConfidenceInterval(series, renderOptions.fitFunc!,
237
+ renderOptions.userParamsFlag!, renderOptions.logOptions);
238
+ drawConfidenceInterval(g, {viewport: renderOptions.viewport, logOptions: renderOptions.logOptions,
239
+ showAxes: renderOptions.showAxes, showAxesLabels: renderOptions.showAxesLabels, screenBounds: renderOptions.screenBounds,
240
+ confidenceIntervals: confidenceIntervals, confidenceType: FitConstants.CURVE_CONFIDENCE_INTERVAL_BOUNDS.TOP});
241
+ drawConfidenceInterval(g, {viewport: renderOptions.viewport, logOptions: renderOptions.logOptions,
242
+ showAxes: renderOptions.showAxes, showAxesLabels: renderOptions.showAxesLabels, screenBounds: renderOptions.screenBounds,
243
+ confidenceIntervals: confidenceIntervals, confidenceType: FitConstants.CURVE_CONFIDENCE_INTERVAL_BOUNDS.BOTTOM});
244
+ fillConfidenceInterval(g, {viewport: renderOptions.viewport, logOptions: renderOptions.logOptions,
245
+ showAxes: renderOptions.showAxes, showAxesLabels: renderOptions.showAxesLabels, screenBounds: renderOptions.screenBounds,
246
+ confidenceIntervals: confidenceIntervals});
247
+ }
248
+ }
249
+
250
+ /** Performs a curve confidence interval drawing */
251
+ function drawConfidenceInterval(g: CanvasRenderingContext2D, renderOptions: FitConfidenceIntervalRenderOptions): void {
252
+ g.beginPath();
253
+ const axesLeftPxMargin = renderOptions.showAxes ? renderOptions.showAxesLabels ?
254
+ FitConstants.AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS : FitConstants.AXES_LEFT_PX_MARGIN : 0;
255
+ const axesRightPxMargin = renderOptions.showAxes ? FitConstants.AXES_RIGHT_PX_MARGIN : 0;
256
+ const confIntervalFunc = renderOptions.confidenceType === FitConstants.CURVE_CONFIDENCE_INTERVAL_BOUNDS.TOP ?
257
+ renderOptions.confidenceIntervals!.confidenceTop : renderOptions.confidenceIntervals!.confidenceBottom;
258
+ for (let i = axesLeftPxMargin; i <= renderOptions.screenBounds.width - axesRightPxMargin; i++) {
259
+ const x = renderOptions.screenBounds.x + i;
260
+ const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) : renderOptions.viewport.xToWorld(x);
261
+ const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(Math.pow(10, confIntervalFunc(xForY))) :
262
+ renderOptions.viewport.yToScreen(confIntervalFunc(xForY));
263
+ if (i === axesLeftPxMargin)
264
+ g.moveTo(x, y);
265
+ else
266
+ g.lineTo(x, y);
267
+ }
268
+ g.stroke();
269
+ }
270
+
271
+ /** Performs a curve confidence interval color filling */
272
+ function fillConfidenceInterval(g: CanvasRenderingContext2D, renderOptions: FitConfidenceIntervalRenderOptions): void {
273
+ g.beginPath();
274
+ const axesLeftPxMargin = renderOptions.showAxes ? renderOptions.showAxesLabels ?
275
+ FitConstants.AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS : FitConstants.AXES_LEFT_PX_MARGIN : 0;
276
+ const axesRightPxMargin = renderOptions.showAxes ? FitConstants.AXES_RIGHT_PX_MARGIN : 0;
277
+ for (let i = axesLeftPxMargin; i <= renderOptions.screenBounds.width - axesRightPxMargin; i++) {
278
+ const x = renderOptions.screenBounds.x + i;
279
+ const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) :
280
+ renderOptions.viewport.xToWorld(x);
281
+ const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(
282
+ Math.pow(10, renderOptions.confidenceIntervals!.confidenceTop(xForY))) :
283
+ renderOptions.viewport.yToScreen(renderOptions.confidenceIntervals!.confidenceTop(xForY));
284
+ if (i === axesLeftPxMargin)
285
+ g.moveTo(x, y);
286
+ else
287
+ g.lineTo(x, y);
288
+ }
289
+
290
+ // reverse traverse to make a shape of confidence interval to fill it
291
+ for (let i = renderOptions.screenBounds.width - axesRightPxMargin; i >= axesLeftPxMargin; i--) {
292
+ const x = renderOptions.screenBounds.x + i;
293
+ const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) :
294
+ renderOptions.viewport.xToWorld(x);
295
+ const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(
296
+ Math.pow(10, renderOptions.confidenceIntervals!.confidenceBottom(xForY))) :
297
+ renderOptions.viewport.yToScreen(renderOptions.confidenceIntervals!.confidenceBottom(xForY));
298
+ g.lineTo(x, y);
299
+ }
300
+ g.closePath();
301
+ g.fill();
302
+ }
303
+
304
+ export function renderDroplines(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitDroplineRenderOptions): void {
305
+ if ((series.showFitLine ?? true) && series.droplines && renderOptions.showDroplines!) {
306
+ g.save();
307
+ g.strokeStyle = 'blue';
308
+ g.lineWidth = renderOptions.ratio!;
309
+ g.beginPath();
310
+ g.setLineDash([5, 5]);
311
+ for (let j = 0; j < series.droplines.length; j++) {
312
+ const droplineName = series.droplines[j];
313
+ if (droplineName === 'IC50')
314
+ drawDropline(g, {viewport: renderOptions.viewport, xValue: series.parameters![2],
315
+ dataBounds: renderOptions.dataBounds, curveFunc: renderOptions.curveFunc, logOptions: renderOptions.logOptions});
316
+ }
317
+ g.stroke();
318
+ g.restore();
319
+ }
320
+ }
321
+
322
+ /** Performs a dropline drawing */
323
+ function drawDropline(g: CanvasRenderingContext2D, renderOptions: FitDroplineRenderOptions): void {
324
+ if (renderOptions.logOptions.logX)
325
+ renderOptions.xValue = Math.pow(10, renderOptions.xValue);
326
+ const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.xValue) : renderOptions.xValue;
327
+ const y = renderOptions.logOptions.logY ? Math.pow(10, renderOptions.curveFunc(xForY)) : renderOptions.curveFunc(xForY);
328
+ g.moveTo(renderOptions.viewport.xToScreen(renderOptions.dataBounds.minX), renderOptions.viewport.yToScreen(y));
329
+ g.lineTo(renderOptions.viewport.xToScreen(renderOptions.xValue), renderOptions.viewport.yToScreen(y));
330
+ g.lineTo(renderOptions.viewport.xToScreen(renderOptions.xValue), renderOptions.viewport.yToScreen(renderOptions.dataBounds.minY));
331
+ }
332
+
333
+ export function renderStatistics(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitStatisticsRenderOptions): void {
334
+ if ((series.showFitLine ?? true) && renderOptions.statistics) {
335
+ const statistics = getSeriesStatistics(series, renderOptions.fitFunc, renderOptions.logOptions);
336
+ for (let i = 0; i < renderOptions.statistics.length; i++) {
337
+ const statName = renderOptions.statistics[i];
338
+ const prop = statisticsProperties.find((p) => p.name === statName);
339
+ if (prop) {
340
+ const s = StringUtils.formatNumber(prop.get(statistics));
341
+ g.fillStyle = series.fitLineColor!;
342
+ g.textAlign = 'left';
343
+ g.fillText(prop.name + ': ' + s, renderOptions.dataBox.x + 5, renderOptions.dataBox.y + 20 + 20 * i);
344
+ }
345
+ }
346
+ }
347
+ }
348
+
349
+ export function renderTitle(g: CanvasRenderingContext2D, renderOptions: FitTitleRenderOptions): void {
350
+ if (renderOptions.showTitle) {
351
+ g.font = '12px Roboto, "Roboto Local"';
352
+ g.textAlign = 'center';
353
+ g.fillStyle = 'black';
354
+ g.fillText(renderOptions.title!, renderOptions.dataBox.midX - 5, renderOptions.screenBounds.y + 15);
355
+ }
356
+ }
357
+
358
+ export function renderAxesLabels(g: CanvasRenderingContext2D, renderOptions: FitAxesLabelsRenderOptions): void {
359
+ if (renderOptions.showAxesLabels) {
360
+ g.font = '11px Roboto, "Roboto Local"';
361
+ g.textAlign = 'center';
362
+ g.fillStyle = 'black';
363
+ g.fillText(renderOptions.xAxisName!, renderOptions.dataBox.midX - 5,
364
+ renderOptions.screenBounds.maxY - FitConstants.X_AXIS_LABEL_BOTTOM_PX_MARGIN);
365
+ g.translate(renderOptions.screenBounds.x, renderOptions.screenBounds.y);
366
+ g.rotate(-Math.PI / 2);
367
+ const axesTopPxMargin = renderOptions.showTitle ? FitConstants.AXES_TOP_PX_MARGIN_WITH_TITLE : FitConstants.AXES_TOP_PX_MARGIN;
368
+ g.fillText(renderOptions.yAxisName!, -(renderOptions.dataBox.height / 2 + axesTopPxMargin + 15), 15);
369
+ g.restore();
370
+ }
371
+ }
372
+
373
+ export function renderLegend(g: CanvasRenderingContext2D, data: IFitChartData, renderOptions: FitLegendRenderOptions): void {
374
+ if (renderOptions.showLegend) {
375
+ g.font = '11px Roboto, "Roboto Local"';
376
+ const columnNames = [...new Set(data.series?.map((series) => series.columnName))]
377
+ .filter((colName) => colName !== null && colName !== undefined);
378
+ let drawnCurvesInLegend = 0;
379
+ for (let i = 0; i < columnNames.length; i++) {
380
+ const colName = columnNames[i];
381
+ if (data.chartOptions?.showColumnLabel)
382
+ renderColumnLabel(g, colName!, {dataBox: renderOptions.dataBox, columnIdx: i, drawnCurvesInLegend});
383
+
384
+ const series = data.series?.filter((series) => series.columnName === colName);
385
+ for (let j = 0; j < series?.length!; j++) {
386
+ const currentSeries = series![j];
387
+ if (currentSeries.name === '' || currentSeries.name === null || currentSeries.name === undefined)
388
+ continue;
389
+ renderLegendSeries(g, currentSeries, {dataBox: renderOptions.dataBox, ratio: renderOptions.ratio,
390
+ columnIdx: i, drawnCurvesInLegend, showColumnLabel: data.chartOptions?.showColumnLabel});
391
+ drawnCurvesInLegend++;
392
+ }
393
+ }
394
+ g.restore();
395
+ }
396
+ }
397
+
398
+ function renderColumnLabel(g: CanvasRenderingContext2D, colName: string, renderOptions: FitLegendColumnlabelSeriesRenderOptions): void {
399
+ g.beginPath();
400
+ g.fillStyle = 'black';
401
+ const colNameWidth = g.measureText(colName!).width;
402
+ g.fillText(colName!, renderOptions.dataBox.maxX - colNameWidth, renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN +
403
+ (renderOptions.columnIdx * FitConstants.LEGEND_RECORD_PX_HEIGHT + FitConstants.LEGEND_RECORD_PX_HEIGHT * renderOptions.drawnCurvesInLegend));
404
+ }
405
+
406
+ function renderLegendSeries(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitLegendColumnlabelSeriesRenderOptions): void {
407
+ g.beginPath();
408
+ g.strokeStyle = series.fitLineColor!;
409
+ g.lineWidth = 2 * renderOptions.ratio!;
410
+ const textWidth = g.measureText(series.name!).width;
411
+ g.moveTo(renderOptions.dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_PX_WIDTH - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN,
412
+ renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN - FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN +
413
+ (renderOptions.showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT * (renderOptions.columnIdx + 1) : 0) +
414
+ FitConstants.LEGEND_RECORD_PX_HEIGHT * renderOptions.drawnCurvesInLegend);
415
+ g.lineTo(renderOptions.dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN,
416
+ renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN - FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN +
417
+ (renderOptions.showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT * (renderOptions.columnIdx + 1) : 0) +
418
+ FitConstants.LEGEND_RECORD_PX_HEIGHT * renderOptions.drawnCurvesInLegend);
419
+ const marker = series.markerType ? series.markerType as DG.MARKER_TYPE : DG.MARKER_TYPE.CIRCLE;
420
+ DG.Paint.marker(g, marker, renderOptions.dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN
421
+ - FitConstants.LEGEND_RECORD_LINE_PX_WIDTH / 2, renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN -
422
+ FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN + (renderOptions.showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT
423
+ * (renderOptions.columnIdx + 1) : 0) + FitConstants.LEGEND_RECORD_PX_HEIGHT * renderOptions.drawnCurvesInLegend,
424
+ series.pointColor!, FitConstants.POINT_PX_SIZE * renderOptions.ratio!);
425
+ g.fillStyle = series.fitLineColor!;
426
+ g.fillText(series.name!, renderOptions.dataBox.maxX - textWidth,
427
+ renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN + (renderOptions.showColumnLabel ?
428
+ FitConstants.LEGEND_RECORD_PX_HEIGHT * (renderOptions.columnIdx + 1) : 0) + FitConstants.LEGEND_RECORD_PX_HEIGHT * renderOptions.drawnCurvesInLegend);
429
+ g.stroke();
430
+ }
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;
@@ -7,6 +7,7 @@ import {createSigmoidPoints, createDemoDataFrame} from '../fit/fit-demo';
7
7
  import {category, test, delay} from '@datagrok-libraries/utils/src/test';
8
8
  import {FitChartCellRenderer} from '../fit/fit-renderer';
9
9
  import {IFitChartData} from '@datagrok-libraries/statistics/src/fit/fit-curve';
10
+ import {FitConstants} from '../fit/const';
10
11
 
11
12
 
12
13
  export function createIFitChartData(seriesLength: number): IFitChartData {
@@ -67,7 +68,7 @@ category('rendering', () => {
67
68
  const fitChartCellRenderer = new FitChartCellRenderer();
68
69
  const fitChartData: IFitChartData = createIFitChartData(DG.Test.isInBenchmark ? 50 : 15);
69
70
  for (let i = 1; i <= renderingTimesAmount; i++) {
70
- fitChartCellRenderer.renderCurves(g, canvas.clientLeft, canvas.clientTop, canvasWidth, canvasHeight, fitChartData);
71
+ fitChartCellRenderer.renderCurves(g, new DG.Rect(canvas.clientLeft, canvas.clientTop, canvasWidth, canvasHeight).inflate(FitConstants.INFLATE_SIZE, FitConstants.INFLATE_SIZE), fitChartData);
71
72
  g.clearRect(0, 0, canvasWidth, canvasHeight);
72
73
  }
73
74
  return `rendering performed ${renderingTimesAmount} times`;
@@ -85,7 +86,7 @@ category('rendering', () => {
85
86
  const fitChartCellRenderer = new FitChartCellRenderer();
86
87
  for (let i = 1; i <= renderingTimesAmount; i++) {
87
88
  const fitChartData = createIFitChartData(DG.Test.isInBenchmark ? 50 : 15);
88
- fitChartCellRenderer.renderCurves(g, canvas.clientLeft, canvas.clientTop, canvasWidth, canvasHeight, fitChartData);
89
+ fitChartCellRenderer.renderCurves(g, new DG.Rect(canvas.clientLeft, canvas.clientTop, canvasWidth, canvasHeight).inflate(FitConstants.INFLATE_SIZE, FitConstants.INFLATE_SIZE), fitChartData);
89
90
  g.clearRect(0, 0, canvasWidth, canvasHeight);
90
91
  }
91
92
  return `rendering performed ${renderingTimesAmount} times`;
@@ -1,9 +1,8 @@
1
1
  import * as DG from 'datagrok-api/dg';
2
2
  import * as grok from 'datagrok-api/grok';
3
3
 
4
- import {TAG_FIT_CHART_FORMAT, TAG_FIT_CHART_FORMAT_3DX} from '../fit/fit-renderer';
5
- import {FIT_SEM_TYPE} from '@datagrok-libraries/statistics/src/fit/fit-curve';
6
4
  import {category, expect, test} from '@datagrok-libraries/utils/src/test';
5
+ import {FitConstants} from '../fit/const';
7
6
 
8
7
 
9
8
  category('detectors', () => {
@@ -17,9 +16,9 @@ category('detectors', () => {
17
16
  const col3 = df.columns.byName('json chart 2');
18
17
  await grok.data.detectSemanticTypes(df);
19
18
 
20
- expect(col1.semType, FIT_SEM_TYPE);
21
- expect(col2.semType, FIT_SEM_TYPE);
22
- expect(col3.semType, FIT_SEM_TYPE);
19
+ expect(col1.semType, FitConstants.FIT_SEM_TYPE);
20
+ expect(col2.semType, FitConstants.FIT_SEM_TYPE);
21
+ expect(col3.semType, FitConstants.FIT_SEM_TYPE);
23
22
  });
24
23
 
25
24
  test('detectXMLCurveChart', async () => {
@@ -29,7 +28,7 @@ category('detectors', () => {
29
28
  const col = df.columns.byName('xmlCurveFitChart');
30
29
  await grok.data.detectSemanticTypes(df);
31
30
 
32
- expect(col.semType, FIT_SEM_TYPE);
33
- expect(col.tags[TAG_FIT_CHART_FORMAT], TAG_FIT_CHART_FORMAT_3DX);
31
+ expect(col.semType, FitConstants.FIT_SEM_TYPE);
32
+ expect(col.tags[FitConstants.TAG_FIT_CHART_FORMAT], FitConstants.TAG_FIT_CHART_FORMAT_3DX);
34
33
  });
35
34
  });