@datagrok/curves 1.6.0 → 1.8.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.
@@ -7,7 +7,7 @@ import {
7
7
  IFitSeries,
8
8
  FitStatistics,
9
9
  fitChartDataProperties,
10
- fitSeriesProperties, IFitChartOptions, IFitPoint,
10
+ fitSeriesProperties, IFitChartOptions, IFitPoint, FitCurve, FitFunction,
11
11
  } from '@datagrok-libraries/statistics/src/fit/fit-curve';
12
12
  import {Viewport} from '@datagrok-libraries/utils/src/transform';
13
13
 
@@ -17,22 +17,29 @@ import {
17
17
  getChartBounds,
18
18
  getSeriesFitFunction,
19
19
  getCurve,
20
- LogOptions,
20
+ LogOptions, getDataPoints,
21
21
  } from '@datagrok-libraries/statistics/src/fit/fit-data';
22
22
 
23
23
  import {convertXMLToIFitChartData} from './fit-parser';
24
- import {CellRenderViewer} from './cell-render-viewer';
25
24
  import { calculateSeriesStats, getChartDataAggrStats } from './fit-grid-cell-handler';
26
25
  import {FitConstants} from './const';
27
26
  import {
28
- assignSeriesColors, renderAxesLabels,
27
+ renderAxesLabels,
29
28
  renderConfidenceIntervals, renderConnectDots,
30
29
  renderDroplines,
31
30
  renderFitLine, renderLegend,
32
31
  renderPoints, renderStatistics, renderTitle
33
32
  } from './render-utils';
33
+ import {merge} from 'rxjs';
34
34
 
35
35
 
36
+ export interface FitCellOutlierToggleArgs {
37
+ gridCell: DG.GridCell;
38
+ series: IFitSeries;
39
+ seriesIdx: number;
40
+ pointIdx: number;
41
+ }
42
+
36
43
  function clamp(value: number, min: number, max: number): number {
37
44
  return Math.min(Math.max(value, min), max);
38
45
  }
@@ -135,17 +142,27 @@ export function mergeSeries(series: IFitSeries[]): IFitSeries | null {
135
142
  return mergedSeries;
136
143
  }
137
144
 
145
+ /** Returns either cached or constructed chart data for the specified grid cell. */
146
+ export function getOrCreateParsedChartData(gridCell: DG.GridCell, useCache = true): IFitChartData {
147
+ const column = gridCell?.cell?.column;
148
+ return (useCache && column && gridCell?.cell) ?
149
+ FitChartCellRenderer.parsedCurves.getOrCreate(`tableId: ${column.dataFrame.id} || tableName: ${column.dataFrame.name} || colName: ${column.name} || colVersion: ${column.version} || rowIdx: ${gridCell?.tableRowIndex}`, () => {
150
+ return getChartData(gridCell);
151
+ }) : getChartData(gridCell);
152
+ }
153
+
138
154
  /** Constructs {@link IFitChartData} from the grid cell, taking into account
139
155
  * chart and fit settings potentially defined on the dataframe and column level. */
140
- export function getChartData(gridCell: DG.GridCell): IFitChartData {
156
+ function getChartData(gridCell: DG.GridCell): IFitChartData {
141
157
  // removing '|' from JSON (how did it get here?)
142
158
  let cellValue = gridCell.cell.value as string;
143
159
  if (cellValue.includes('|'))
144
160
  cellValue = cellValue.replaceAll('|', '');
145
- const cellChartData: IFitChartData = gridCell.cell?.column?.type === DG.TYPE.STRING ?
146
- (gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
147
- convertXMLToIFitChartData(cellValue) :
148
- JSON.parse(cellValue ?? '{}') ?? {}) : createDefaultChartData();
161
+ const column = gridCell.cell.column;
162
+ const cellChartData: IFitChartData = column ? (column.type === DG.TYPE.STRING ?
163
+ (column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
164
+ convertXMLToIFitChartData(cellValue) : JSON.parse(cellValue ?? '{}') ?? {}) :
165
+ createDefaultChartData()) : JSON.parse(cellValue ?? '{}') ?? {};
149
166
 
150
167
  const columnChartOptions = gridCell.cell.column ? getColumnChartOptions(gridCell.cell.column) : {};
151
168
  const dfChartOptions = gridCell.cell.column ? getDataFrameChartOptions(gridCell.cell.dataFrame) : {};
@@ -165,6 +182,28 @@ export function getChartData(gridCell: DG.GridCell): IFitChartData {
165
182
  return cellChartData;
166
183
  }
167
184
 
185
+ /** Returns existing, or fits curve for the specified grid cell and series. */
186
+ export function getOrCreateCachedFitCurve(series: IFitSeries, seriesIdx: number, fitFunc: FitFunction,
187
+ chartLogOptions: LogOptions, gridCell?: DG.GridCell, useCache = true): FitCurve {
188
+ const dataPoints = getOrCreateCachedCurvesDataPoints(series, seriesIdx, chartLogOptions, false, gridCell, useCache);
189
+ // don't refit when just rerender - using LruCache with key `cellValue_colName_colVersion`
190
+ const column = gridCell?.cell?.column;
191
+ return (useCache && column && gridCell?.cell) ?
192
+ FitChartCellRenderer.fittedCurves.getOrCreate(`tableId: ${column.dataFrame.id} || tableName: ${column.dataFrame.name} || colName: ${column.name} || colVersion: ${column.version} || rowIdx: ${gridCell?.tableRowIndex}`, () => {
193
+ return fitSeries(series, fitFunc, dataPoints, chartLogOptions);
194
+ }) : fitSeries(series, fitFunc, dataPoints, chartLogOptions);
195
+ }
196
+
197
+ /** Returns existing, or maps new data points for the specified series. */
198
+ export function getOrCreateCachedCurvesDataPoints(series: IFitSeries, idx: number, logOptions?: LogOptions,
199
+ userParamsFlag?: boolean, gridCell?: DG.GridCell, useCache = true): {x: number[], y: number[]} {
200
+ const column = gridCell?.cell?.column;
201
+ return (useCache && column && gridCell?.cell) ?
202
+ FitChartCellRenderer.curvesDataPoints.getOrCreate(`tableId: ${column.dataFrame.id} || tableName: ${column.dataFrame.name} || colName: ${column.name} || colVersion: ${column.version} || rowIdx: ${gridCell?.tableRowIndex} || idx: ${idx} || userParamsFlag: ${userParamsFlag}`, () => {
203
+ return getDataPoints(series, logOptions, userParamsFlag);
204
+ }) : getDataPoints(series, logOptions, userParamsFlag);
205
+ }
206
+
168
207
  /** Returns existing, or creates new dataframe default chart options. */
169
208
  export function getDataFrameChartOptions(df: DG.DataFrame): IFitChartData {
170
209
  return JSON.parse(df.tags[FitConstants.TAG_FIT] ??= JSON.stringify(createDefaultChartData()));
@@ -224,6 +263,57 @@ export function substituteZeroes(data: IFitChartData): void {
224
263
  }
225
264
  }
226
265
 
266
+ export function setOutlier(gridCell: DG.GridCell, p: IFitPoint, seriesIdx: number, pointIdx: number, data?: IFitChartData) {
267
+ data ??= gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
268
+ convertXMLToIFitChartData(gridCell.cell.value) : getOrCreateParsedChartData(gridCell);
269
+ p.outlier = !p.outlier;
270
+ // temporarily works only for JSON structure
271
+ if (gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) !== FitConstants.TAG_FIT_CHART_FORMAT_3DX) {
272
+ const gridCellValue = JSON.parse(gridCell.cell.value) as IFitChartData;
273
+ gridCellValue.series![seriesIdx].points[pointIdx].outlier = p.outlier;
274
+ gridCell.cell.column.set(gridCell.cell.rowIndex, JSON.stringify(gridCellValue), false);
275
+ grok.events.fireCustomEvent('fit-cell-outlier-toggle', {
276
+ gridCell: gridCell,
277
+ series: gridCellValue.series![seriesIdx],
278
+ seriesIdx: seriesIdx,
279
+ pointIdx: pointIdx,
280
+ });
281
+ const g = gridCell.grid.canvas.getContext('2d')!;
282
+ gridCell.render({context: g, bounds: gridCell.bounds});
283
+ }
284
+ const columns = gridCell.grid.dataFrame.columns.byTags({'.sourceColumn': gridCell.cell.column.name});
285
+ if (columns) {
286
+ for (const column of columns) {
287
+ const chartLogOptions: LogOptions = {logX: data.chartOptions?.logX, logY: data.chartOptions?.logY};
288
+ const stats = column.tags['.seriesAggregation'] !== null ?
289
+ getChartDataAggrStats(data, column.tags['.seriesAggregation'], gridCell) :
290
+ column.tags['.seriesNumber'] === seriesIdx ? calculateSeriesStats(data.series![seriesIdx], seriesIdx, chartLogOptions, gridCell) : null;
291
+ if (stats === null)
292
+ continue;
293
+ column.set(gridCell.cell.rowIndex, stats[column.tags['.statistics'] as keyof FitStatistics]);
294
+ }
295
+ }
296
+ }
297
+
298
+ export function inspectCurve(gridCell: DG.GridCell, size?: Partial<DG.Size>, rerenderOnChange: boolean = false): void {
299
+ if (!gridCell.cell.value)
300
+ return;
301
+
302
+ const gridCellWidget = DG.GridCellWidget.fromGridCell(gridCell);
303
+ gridCellWidget.root.style.removeProperty('aspectRatio');
304
+ gridCellWidget.root.style.height = '100%';
305
+ gridCellWidget.canvas.style.removeProperty('height');
306
+ gridCellWidget.canvas.style.removeProperty('width');
307
+ gridCellWidget.canvas.style.left = '18px';
308
+ gridCellWidget.canvas.style.top = '18px';
309
+
310
+ const dlg = ui.dialog({title: 'Edit chart'})
311
+ .add(gridCellWidget.root)
312
+ .show({resizable: true, width: size?.width ?? 350, height: size?.height ?? 300});
313
+ if (rerenderOnChange)
314
+ dlg.sub(merge(gridCell.grid.dataFrame.onDataChanged, gridCell.grid.dataFrame.onMetadataChanged).subscribe(() => gridCellWidget.render()));
315
+ }
316
+
227
317
  @grok.decorators.cellRenderer({
228
318
  name: 'Fit',
229
319
  cellType: 'fit',
@@ -234,16 +324,29 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
234
324
 
235
325
  get cellType() { return FitConstants.FIT_CELL_TYPE; }
236
326
 
327
+ get defaultHeight() { return FitConstants.CELL_DEFAULT_HEIGHT; }
328
+
329
+ get defaultWidth() { return FitConstants.CELL_DEFAULT_WIDTH; }
330
+
237
331
  getDefaultSize(gridColumn: DG.GridColumn): {width?: number | null, height?: number | null} {
238
332
  return {width: FitConstants.CELL_DEFAULT_WIDTH, height: FitConstants.CELL_DEFAULT_HEIGHT};
239
333
  }
240
334
 
335
+ static fittedCurves: DG.LruCache<string, FitCurve> = new DG.LruCache<string, FitCurve>(1000);
336
+ static parsedCurves: DG.LruCache<string, IFitChartData> = new DG.LruCache<string, IFitChartData>(1000);
337
+ static curvesDataPoints: DG.LruCache<string, {x: number[], y: number[]}> = new DG.LruCache<string, {x: number[], y: number[]}>(2000);
338
+
339
+ static inflateScreenBounds(rect: DG.Rect): DG.Rect {
340
+ return rect.inflate(rect.width < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH ? FitConstants.MIN_INFLATE_SIZE : FitConstants.INFLATE_SIZE,
341
+ rect.height < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT ? FitConstants.MIN_INFLATE_SIZE : FitConstants.INFLATE_SIZE);
342
+ }
343
+
241
344
  onClick(gridCell: DG.GridCell, e: MouseEvent): void {
242
345
  if (!gridCell.cell.value)
243
346
  return;
244
347
 
245
348
  const data = gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
246
- convertXMLToIFitChartData(gridCell.cell.value) : getChartData(gridCell);
349
+ convertXMLToIFitChartData(gridCell.cell.value) : getOrCreateParsedChartData(gridCell);
247
350
 
248
351
  for (const [message, condition] of Object.entries(FitConstants.CONDITION_MAP)) {
249
352
  if (condition(data.series)) {
@@ -254,7 +357,7 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
254
357
 
255
358
  grok.shell.o = gridCell;
256
359
 
257
- const screenBounds = gridCell.bounds.inflate(FitConstants.INFLATE_SIZE, FitConstants.INFLATE_SIZE);
360
+ const screenBounds = FitChartCellRenderer.inflateScreenBounds(gridCell.bounds);
258
361
  const dataBox = layoutChart(screenBounds, this.areAxesLabelsShown(screenBounds, data),
259
362
  this.isTitleShown(screenBounds, data))[0];
260
363
  const dataBounds = getChartBounds(data);
@@ -267,26 +370,7 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
267
370
  for (let j = 0; j < data.series![i].points.length!; j++) {
268
371
  const p = data.series![i].points[j];
269
372
  if (this.hitTest(e, p, viewport)) {
270
- p.outlier = !p.outlier;
271
- const columns = gridCell.grid.dataFrame.columns.byTags({'.sourceColumn': gridCell.cell.column.name});
272
- if (columns) {
273
- for (const column of columns) {
274
- const chartLogOptions: LogOptions = {logX: data.chartOptions?.logX, logY: data.chartOptions?.logY};
275
- const stats = column.tags['.seriesAggregation'] !== null ?
276
- getChartDataAggrStats(data, column.tags['.seriesAggregation']) :
277
- column.tags['.seriesNumber'] === i ? calculateSeriesStats(data.series![i], chartLogOptions) : null;
278
- if (stats === null)
279
- continue;
280
- column.set(gridCell.cell.rowIndex, stats[column.tags['.statistics'] as keyof FitStatistics]);
281
- }
282
- }
283
-
284
- // temporarily works only for JSON structure
285
- if (gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) !== FitConstants.TAG_FIT_CHART_FORMAT_3DX) {
286
- const gridCellValue = JSON.parse(gridCell.cell.value) as IFitChartData;
287
- gridCellValue.series![i].points[j].outlier = p.outlier;
288
- gridCell.cell.value = JSON.stringify(gridCellValue);
289
- }
373
+ setOutlier(gridCell, p, i, j, data);
290
374
  return;
291
375
  }
292
376
  }
@@ -294,29 +378,7 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
294
378
  }
295
379
 
296
380
  onDoubleClick(gridCell: DG.GridCell, e: MouseEvent): void {
297
- if (!gridCell.cell.value)
298
- return;
299
-
300
- const cellRenderViewer = CellRenderViewer.fromGridCell(gridCell);
301
- const dlg = ui.dialog({title: 'Edit chart'})
302
- .add(cellRenderViewer.root)
303
- .show({resizable: true});
304
-
305
- // canvas is created as (300, 150), so we change its size to the dialog contents box size
306
- const dlgContentsBox = dlg.root.getElementsByClassName('d4-dialog-contents dlg-edit-chart')[0].firstChild as HTMLElement;
307
- cellRenderViewer.canvas.width = dlgContentsBox.clientWidth;
308
- cellRenderViewer.canvas.height = dlgContentsBox.clientHeight;
309
- cellRenderViewer.render();
310
-
311
- // contents ui-box isn't resizable by default
312
- dlgContentsBox.style.width = '100%';
313
- dlgContentsBox.style.height = '100%';
314
-
315
- ui.tools.handleResize(dlgContentsBox, (w: number, h: number) => {
316
- cellRenderViewer.canvas.width = w;
317
- cellRenderViewer.canvas.height = h;
318
- cellRenderViewer.render();
319
- });
381
+ inspectCurve(gridCell);
320
382
  }
321
383
 
322
384
  areAxesShown(screenBounds: DG.Rect): boolean {
@@ -341,12 +403,14 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
341
403
  return screenBounds.width >= FitConstants.MIN_DROPLINES_VISIBILITY_PX_WIDTH && screenBounds.height >= FitConstants.MIN_DROPLINES_VISIBILITY_PX_HEIGHT;
342
404
  }
343
405
 
344
- renderCurves(g: CanvasRenderingContext2D, screenBounds: DG.Rect, data: IFitChartData): void {
406
+ renderCurves(g: CanvasRenderingContext2D, screenBounds: DG.Rect, data: IFitChartData, gridCell?: DG.GridCell): void {
345
407
  g.save();
346
408
  g.beginPath();
347
409
  g.rect(screenBounds.x, screenBounds.y, screenBounds.width, screenBounds.height);
348
410
  g.clip();
349
411
 
412
+ const isRenderedOnGrid = gridCell?.grid && gridCell?.grid.dart && g.canvas === gridCell?.grid?.canvas; // only use cache for grid, because there is no guarantee that rowIdx will be correct for other places
413
+
350
414
  if (data.chartOptions?.allowXZeroes && data.chartOptions?.logX &&
351
415
  data.series?.some((series) => series.points.some((p) => p.x === 0)))
352
416
  substituteZeroes(data);
@@ -371,14 +435,9 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
371
435
 
372
436
  for (let i = 0; i < data.series?.length!; i++) {
373
437
  const series = data.series![i];
438
+ const containsParams = series.parameters && series.parameters.length > 0;
374
439
  if (series.points.some((point) => point.x === undefined || point.y === undefined))
375
440
  continue;
376
- if (screenBounds.width < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH ||
377
- screenBounds.height < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT) {
378
- series.showPoints = '';
379
- if (data.chartOptions)
380
- data.chartOptions.showStatistics = [];
381
- }
382
441
  series.points.sort((a, b) => a.x - b.x);
383
442
 
384
443
  let userParamsFlag = true;
@@ -387,14 +446,13 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
387
446
  if (!(series.connectDots && !series.showFitLine)) {
388
447
  if (series.parameters) {
389
448
  if (data.chartOptions?.logX) {
390
- if (series.parameters[2] > 0)
449
+ if (series.parameters[2] > 0) // check if sigmoid, then log
391
450
  series.parameters[2] = Math.log10(series.parameters[2]);
392
451
  }
393
452
  curve = getCurve(series, fitFunc);
394
453
  }
395
454
  else {
396
- // TODO: Stop refitting when we just rerender the same curve - use LruMap
397
- const fitResult = fitSeries(series, fitFunc, chartLogOptions);
455
+ const fitResult = getOrCreateCachedFitCurve(series, i, fitFunc, chartLogOptions, gridCell, isRenderedOnGrid);
398
456
  curve = fitResult.fittedCurve;
399
457
  const params = [...fitResult.parameters]
400
458
  series.parameters = params;
@@ -402,18 +460,20 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
402
460
  }
403
461
  }
404
462
 
405
- assignSeriesColors(series, i);
406
- renderConnectDots(g, series, {viewport, ratio});
407
- renderPoints(g, series, {viewport, ratio});
408
463
  renderFitLine(g, series, {viewport, ratio, logOptions: chartLogOptions, showAxes: this.areAxesShown(screenBounds),
409
- showAxesLabels: this.areAxesLabelsShown(screenBounds, data), screenBounds, curveFunc: curve!});
464
+ showAxesLabels: this.areAxesLabelsShown(screenBounds, data), screenBounds, curveFunc: curve!, seriesIdx: i});
465
+ renderConnectDots(g, series, {viewport, ratio, seriesIdx: i});
466
+ renderPoints(g, series, {viewport, ratio, screenBounds, seriesIdx: i});
410
467
  renderConfidenceIntervals(g, series, {viewport, logOptions: chartLogOptions, showAxes: this.areAxesShown(screenBounds),
411
- showAxesLabels: this.areAxesLabelsShown(screenBounds, data), screenBounds, fitFunc, userParamsFlag});
468
+ showAxesLabels: this.areAxesLabelsShown(screenBounds, data), screenBounds, fitFunc, userParamsFlag,
469
+ dataPoints: getOrCreateCachedCurvesDataPoints(series, i, chartLogOptions, userParamsFlag, gridCell, isRenderedOnGrid)});
412
470
  if (series.parameters)
413
471
  renderDroplines(g, series, {viewport, ratio, showDroplines: this.areDroplinesShown(screenBounds),
414
472
  xValue: series.parameters![2], dataBounds, curveFunc: curve!, logOptions: chartLogOptions});
415
473
  renderStatistics(g, series, {statistics: data.chartOptions?.showStatistics, fitFunc,
416
- logOptions: chartLogOptions, dataBox});
474
+ logOptions: chartLogOptions, dataBox, screenBounds, seriesIdx: i});
475
+ if (!containsParams)
476
+ delete series.parameters;
417
477
  }
418
478
 
419
479
  renderTitle(g, {showTitle: this.isTitleShown(screenBounds, data), title: data.chartOptions?.title, dataBox, screenBounds});
@@ -435,9 +495,10 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
435
495
  if (w < FitConstants.MIN_CELL_RENDERER_PX_WIDTH || h < FitConstants.MIN_CELL_RENDERER_PX_HEIGHT)
436
496
  return;
437
497
 
498
+ const isRenderedOnGrid = gridCell?.grid && gridCell?.grid.dart && g.canvas === gridCell?.grid?.canvas; // only use cache for grid, because there is no guarantee that rowIdx will be correct for other places
438
499
  const data = gridCell.cell.column?.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX ?
439
- convertXMLToIFitChartData(gridCell.cell.value) : getChartData(gridCell);
440
- const screenBounds = new DG.Rect(x, y, w, h).inflate(FitConstants.INFLATE_SIZE, FitConstants.INFLATE_SIZE);
500
+ convertXMLToIFitChartData(gridCell.cell.value) : getOrCreateParsedChartData(gridCell, isRenderedOnGrid);
501
+ const screenBounds = FitChartCellRenderer.inflateScreenBounds(new DG.Rect(x, y, w, h));
441
502
 
442
503
  for (const [message, condition] of Object.entries(FitConstants.CONDITION_MAP)) {
443
504
  if (condition(data.series)) {
@@ -446,11 +507,13 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
446
507
  }
447
508
  }
448
509
 
449
- data.series?.forEach((series) => series.columnName = gridCell.cell.column.name);
510
+ if (gridCell.cell.column?.name)
511
+ data.series?.forEach((series) => series.columnName = gridCell.cell.column.name);
450
512
  if (data.chartOptions?.mergeSeries)
451
513
  data.series = [mergeSeries(data.series!)!];
452
514
 
453
- this.renderCurves(g, screenBounds, data);
515
+ g.clearRect(screenBounds.x, screenBounds.y, screenBounds.width, screenBounds.height);
516
+ this.renderCurves(g, screenBounds, data, gridCell);
454
517
  }
455
518
 
456
519
  hitTest(e: MouseEvent, point: IFitPoint, viewport: Viewport): boolean {
@@ -466,7 +529,7 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
466
529
  if (!gridCell.cell.value)
467
530
  return;
468
531
 
469
- const screenBounds = gridCell.bounds.inflate(FitConstants.INFLATE_SIZE, FitConstants.INFLATE_SIZE);
532
+ const screenBounds = FitChartCellRenderer.inflateScreenBounds(gridCell.bounds);
470
533
  if (screenBounds.width < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH ||
471
534
  screenBounds.height < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT) {
472
535
  const canvas = ui.canvas(300, 200);
@@ -476,9 +539,9 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
476
539
  }
477
540
 
478
541
  // TODO: add caching
479
- const data = gridCell.cell.column.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX
542
+ const data = gridCell.cell.column?.getTag(FitConstants.TAG_FIT_CHART_FORMAT) === FitConstants.TAG_FIT_CHART_FORMAT_3DX
480
543
  ? convertXMLToIFitChartData(gridCell.cell.value)
481
- : getChartData(gridCell);
544
+ : getOrCreateParsedChartData(gridCell);
482
545
 
483
546
  if (screenBounds.width >= FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH &&
484
547
  screenBounds.height >= FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT) {
@@ -492,8 +555,8 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
492
555
  for (let j = 0; j < data.series![i].points.length!; j++) {
493
556
  const p = data.series![i].points[j];
494
557
  if (this.hitTest(e, p, viewport)) {
495
- ui.tooltip.show(ui.divV([ui.divText(`x: ${DG.format(p.x, '#0.000')}`),
496
- ui.divText(`y: ${DG.format(p.y, '#0.000')}`)]), e.x + 16, e.y + 16);
558
+ ui.tooltip.show(ui.divV([ui.divText(`${data.chartOptions?.xAxisName ?? 'x'}: ${DG.format(p.x, !data.chartOptions?.logX ? '#0.000' : 'scientific')}`),
559
+ ui.divText(`${data.chartOptions?.yAxisName ?? 'y'}: ${DG.format(p.y, !data.chartOptions?.logY ? '#0.000' : 'scientific')}`)]), e.x + 16, e.y + 16);
497
560
  if (!data.series![i].connectDots && data.series![i].clickToToggle && screenBounds.width >= FitConstants.MIN_AXES_CELL_PX_WIDTH &&
498
561
  screenBounds.height >= FitConstants.MIN_AXES_CELL_PX_HEIGHT)
499
562
  document.body.style.cursor = 'pointer';
@@ -2,9 +2,8 @@ import * as DG from 'datagrok-api/dg';
2
2
  import * as ui from 'datagrok-api/ui';
3
3
  import * as grok from 'datagrok-api/grok';
4
4
 
5
- import {CellRenderViewer} from './cell-render-viewer';
6
- import {FitChartCellRenderer, mergeChartOptions, mergeSeries} from './fit-renderer';
7
- import {getChartData, mergeProperties} from './fit-renderer';
5
+ import {mergeChartOptions, mergeSeries} from './fit-renderer';
6
+ import {getOrCreateParsedChartData, mergeProperties} from './fit-renderer';
8
7
  import {FitChartData, fitChartDataProperties, IFitChartData, IFitChartOptions} from '@datagrok-libraries/statistics/src/fit/fit-curve';
9
8
 
10
9
  import {debounce} from 'rxjs/operators';
@@ -19,8 +18,10 @@ const ERROR_CLASS = 'd4-viewer-error';
19
18
  description: 'A viewer that superimposes multiple in-cell curves on one chart',
20
19
  icon: 'icons/multi-curve-viewer.png',
21
20
  })
22
- export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
21
+ export class MultiCurveViewer extends DG.JsViewer {
23
22
  [index: string]: any;
23
+ canvas: HTMLCanvasElement;
24
+ gridCellWidget: DG.GridCellWidget;
24
25
  curvesColumnNames?: string[] = [];
25
26
  showSelectedRowsCurves: boolean = false;
26
27
  showCurrentRowCurve: boolean = true;
@@ -35,7 +36,15 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
35
36
  showColumnLabel?: boolean;
36
37
 
37
38
  constructor() {
38
- super(new FitChartCellRenderer());
39
+ super();
40
+ this.gridCellWidget = DG.GridCellWidget.fromGridCell(this.createGridCell(''));
41
+ this.canvas = this.gridCellWidget.canvas;
42
+ this.root.append(this.gridCellWidget.canvas);
43
+ ui.tools.handleResize(this.root, (w: number, h: number) => {
44
+ this.canvas.width = w;
45
+ this.canvas.height = h;
46
+ this.render();
47
+ });
39
48
 
40
49
  this.curvesColumnNames = this.addProperty('curvesColumnNames', DG.TYPE.COLUMN_LIST, [], {semType: FitConstants.FIT_SEM_TYPE});
41
50
 
@@ -55,6 +64,12 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
55
64
  return viewer;
56
65
  }
57
66
 
67
+ createGridCell(value: string): DG.GridCell {
68
+ const gridCell = DG.GridCell.fromValue(value);
69
+ gridCell.cellType = FitConstants.FIT_CELL_TYPE;
70
+ return gridCell;
71
+ }
72
+
58
73
  applyViewerProperties(): void {
59
74
  mergeProperties(fitChartDataProperties, this, this.data.chartOptions);
60
75
  }
@@ -99,7 +114,7 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
99
114
  const gridCell = grid.cell(colName, grid.tableRowToGrid(i));
100
115
  if (gridCell.cell.value === '')
101
116
  continue;
102
- const cellCurves = getChartData(gridCell);
117
+ const cellCurves = getOrCreateParsedChartData(gridCell);
103
118
  cellCurves.series?.forEach((series) => series.columnName = gridCell.cell.column.name);
104
119
  const currentChartOptions = cellCurves.chartOptions;
105
120
  if (currentChartOptions !== undefined && currentChartOptions !== null)
@@ -115,7 +130,7 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
115
130
  if (this.mergeColumnSeries)
116
131
  this.data.series?.push(mergeSeries(series)!);
117
132
  else
118
- this.data.series?.push(...series);
133
+ this.data.series?.push(...JSON.parse(JSON.stringify(series)));
119
134
  }
120
135
  this.data.chartOptions = mergeChartOptions(chartOptions);
121
136
  this.mergeViewerChartOptions();
@@ -156,8 +171,10 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
156
171
  merge(this.dataFrame.onCurrentCellChanged, grid.onCellMouseEnter, this.dataFrame.onSelectionChanged)
157
172
  .pipe(debounce(_ => interval(50)))
158
173
  .subscribe(_ => {
159
- this.createChartData();
160
- this.render();
174
+ if (this.dataFrame) {
175
+ this.createChartData();
176
+ this.render();
177
+ }
161
178
  });
162
179
  }
163
180
 
@@ -174,12 +191,16 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
174
191
  render(): void {
175
192
  const g = this.canvas.getContext('2d')!
176
193
  g.clearRect(0, 0, this.canvas.width, this.canvas.height);
194
+ this._removeErrorMessage();
177
195
 
178
196
  if (this.curvesColumnNames?.length === 0) {
179
197
  this._showErrorMessage('The MultiCurveViewer viewer requires a minimum of 1 curves column.');
180
198
  return;
181
199
  }
182
- this._removeErrorMessage();
183
- this.renderer.renderCurves(g, new DG.Rect(0, 0, this.canvas.width, this.canvas.height).inflate(FitConstants.INFLATE_SIZE, FitConstants.INFLATE_SIZE), this.data);
200
+ if (this.data?.series?.length === 0) {
201
+ this._showErrorMessage('No data to show.');
202
+ return;
203
+ }
204
+ this.gridCellWidget.gridCell = this.createGridCell(JSON.stringify(this.data));
184
205
  }
185
206
  }