@datagrok/curves 1.2.16 → 1.3.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.
- package/CHANGELOG.md +25 -0
- package/README.md +6 -0
- package/dist/package-test.js +1 -1
- package/dist/package-test.js.map +1 -1
- package/dist/package.js +1 -1
- package/dist/package.js.map +1 -1
- package/icons/multi-curve-viewer.png +0 -0
- package/package.json +2 -2
- package/src/fit/fit-renderer.ts +178 -45
- package/src/fit/multi-curve-viewer.ts +53 -14
- package/src/package.ts +1 -0
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@datagrok/curves",
|
|
3
3
|
"friendlyName": "Curves",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"description": "Curves provides support for fitted curves (such as dose-response curves), including in-grid rendering, storing charts in cells, interactivity, and automatic fitting.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Dmytro Kovalyov",
|
|
8
8
|
"email": "dkovalyov@datagrok.ai"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@datagrok-libraries/statistics": "^1.2.
|
|
11
|
+
"@datagrok-libraries/statistics": "^1.2.13",
|
|
12
12
|
"@datagrok-libraries/utils": "^4.1.45",
|
|
13
13
|
"@types/wu": "^2.1.42",
|
|
14
14
|
"cash-dom": "^8.0.0",
|
package/src/fit/fit-renderer.ts
CHANGED
|
@@ -52,6 +52,8 @@ const MIN_TITLE_PX_WIDTH = 275;
|
|
|
52
52
|
const MIN_TITLE_PX_HEIGHT = 225;
|
|
53
53
|
const MIN_X_AXIS_NAME_VISIBILITY_PX_WIDTH = 180;
|
|
54
54
|
const MIN_Y_AXIS_NAME_VISIBILITY_PX_HEIGHT = 140;
|
|
55
|
+
const MIN_LEGEND_PX_WIDTH = 325;
|
|
56
|
+
const MIN_LEGEND_PX_HEIGHT = 275;
|
|
55
57
|
const MIN_DROPLINES_VISIBILITY_PX_WIDTH = 120;
|
|
56
58
|
const MIN_DROPLINES_VISIBILITY_PX_HEIGHT = 110;
|
|
57
59
|
const AXES_LEFT_PX_MARGIN = 38;
|
|
@@ -73,6 +75,15 @@ export const LINE_STYLES: {[key: string]: number[]} = {
|
|
|
73
75
|
};
|
|
74
76
|
|
|
75
77
|
|
|
78
|
+
function clamp(value: number, min: number, max: number): number {
|
|
79
|
+
return Math.min(Math.max(value, min), max);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function showIncorrectFitCell(g: CanvasRenderingContext2D, screenBounds: DG.Rect, message: string): void {
|
|
83
|
+
DG.Paint.marker(g, DG.MARKER_TYPE.OUTLIER, screenBounds.midX, screenBounds.midY, DG.Color.red,
|
|
84
|
+
clamp(Math.min(screenBounds.width, screenBounds.height) * 0.8,0, 30));
|
|
85
|
+
}
|
|
86
|
+
|
|
76
87
|
/** Merges properties of the two objects by iterating over the specified {@link properties}
|
|
77
88
|
* and assigning properties from {@link source} to {@link target} only when
|
|
78
89
|
* the property is not defined in target and is defined in source. */
|
|
@@ -86,6 +97,34 @@ export function mergeProperties(properties: DG.Property[], source: any, target:
|
|
|
86
97
|
}
|
|
87
98
|
}
|
|
88
99
|
|
|
100
|
+
export function mergeSeries(series: IFitSeries[]): IFitSeries | null {
|
|
101
|
+
if (series.length === 0)
|
|
102
|
+
return null;
|
|
103
|
+
const mergedSeries: IFitSeries = {
|
|
104
|
+
points: [],
|
|
105
|
+
name: series[0].name,
|
|
106
|
+
fitFunction: series[0].fitFunction,
|
|
107
|
+
markerType: series[0].markerType,
|
|
108
|
+
lineStyle: series[0].lineStyle,
|
|
109
|
+
pointColor: series[0].pointColor,
|
|
110
|
+
fitLineColor: series[0].fitLineColor,
|
|
111
|
+
confidenceIntervalColor: series[0].confidenceIntervalColor,
|
|
112
|
+
outlierColor: series[0].outlierColor,
|
|
113
|
+
connectDots: series[0].connectDots,
|
|
114
|
+
showFitLine: series[0].showFitLine,
|
|
115
|
+
showPoints: series[0].showPoints,
|
|
116
|
+
showCurveConfidenceInterval: series[0].showCurveConfidenceInterval,
|
|
117
|
+
errorModel: series[0].errorModel,
|
|
118
|
+
clickToToggle: series[0].clickToToggle,
|
|
119
|
+
labels: series[0].labels,
|
|
120
|
+
droplines: series[0].droplines,
|
|
121
|
+
columnName: series[0].columnName,
|
|
122
|
+
};
|
|
123
|
+
for (const s of series)
|
|
124
|
+
mergedSeries.points = [...mergedSeries.points, ...s.points];
|
|
125
|
+
return mergedSeries;
|
|
126
|
+
}
|
|
127
|
+
|
|
89
128
|
/** Constructs {@link IFitChartData} from the grid cell, taking into account
|
|
90
129
|
* chart and fit settings potentially defined on the dataframe and column level. */
|
|
91
130
|
export function getChartData(gridCell: DG.GridCell): IFitChartData {
|
|
@@ -158,13 +197,13 @@ function drawPoints(g: CanvasRenderingContext2D, series: IFitSeries,
|
|
|
158
197
|
transform: Viewport, ratio: number, logOptions: LogOptions, pointColor: number, outlierColor: number): void {
|
|
159
198
|
for (let i = 0; i < series.points.length!; i++) {
|
|
160
199
|
const p = series.points[i];
|
|
161
|
-
const color = p.outlier ? (p.outlierColor ? DG.Color.fromHtml(p.outlierColor) ?
|
|
200
|
+
const color = !series.connectDots ? p.outlier ? (p.outlierColor ? DG.Color.fromHtml(p.outlierColor) ?
|
|
162
201
|
DG.Color.fromHtml(p.outlierColor) : outlierColor : outlierColor) : p.color ? DG.Color.fromHtml(p.color) ?
|
|
163
|
-
DG.Color.fromHtml(p.color) : pointColor : pointColor;
|
|
202
|
+
DG.Color.fromHtml(p.color) : pointColor : pointColor : pointColor;
|
|
164
203
|
const marker = p.marker ? p.marker as DG.MARKER_TYPE : series.markerType as DG.MARKER_TYPE;
|
|
165
|
-
const size = p.outlier ? OUTLIER_PX_SIZE * ratio : p.size ? p.size : POINT_PX_SIZE * ratio;
|
|
204
|
+
const size = !series.connectDots ? p.outlier ? OUTLIER_PX_SIZE * ratio : p.size ? p.size : POINT_PX_SIZE * ratio : POINT_PX_SIZE * ratio;
|
|
166
205
|
DG.Paint.marker(g,
|
|
167
|
-
p.outlier ? DG.MARKER_TYPE.OUTLIER : marker,
|
|
206
|
+
!series.connectDots ? p.outlier ? DG.MARKER_TYPE.OUTLIER : marker : marker,
|
|
168
207
|
transform.xToScreen(p.x), transform.yToScreen(p.y), color, size);
|
|
169
208
|
if (p.stdev && !p.outlier) {
|
|
170
209
|
g.strokeStyle = DG.Color.toHtml(color);
|
|
@@ -319,8 +358,25 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
319
358
|
? convertXMLToIFitChartData(gridCell.cell.value)
|
|
320
359
|
: getChartData(gridCell);
|
|
321
360
|
|
|
322
|
-
if (data.series?.
|
|
361
|
+
if (data.series?.length === 0) {
|
|
362
|
+
grok.shell.o = ui.divText('No series to show', {style: {color: 'red'}});
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (data.series?.some((series) => series.points.length === 0)) {
|
|
367
|
+
grok.shell.o = ui.divText('There were series with no points', {style: {color: 'red'}});
|
|
323
368
|
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (data.series?.some((series) => series.points.every((point) => point.x === 0))) {
|
|
372
|
+
grok.shell.o = ui.divText('There were series with all x zeroes', {style: {color: 'red'}});
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (data.series?.some((series) => series.points.every((point) => point.y === 0))) {
|
|
377
|
+
grok.shell.o = ui.divText('There were series with all y zeroes', {style: {color: 'red'}});
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
324
380
|
|
|
325
381
|
grok.shell.o = gridCell;
|
|
326
382
|
|
|
@@ -332,8 +388,8 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
332
388
|
const viewport = new Viewport(dataBounds, dataBox, data.chartOptions?.logX ?? false, data.chartOptions?.logY ?? false);
|
|
333
389
|
|
|
334
390
|
for (let i = 0; i < data.series?.length!; i++) {
|
|
335
|
-
if (!data.series![i].clickToToggle || data.series![i].showPoints !== 'points' ||
|
|
336
|
-
screenBounds.height < MIN_AXES_CELL_PX_HEIGHT)
|
|
391
|
+
if (data.series![i].connectDots || !data.series![i].clickToToggle || data.series![i].showPoints !== 'points' ||
|
|
392
|
+
screenBounds.width < MIN_AXES_CELL_PX_WIDTH || screenBounds.height < MIN_AXES_CELL_PX_HEIGHT)
|
|
337
393
|
continue;
|
|
338
394
|
for (let j = 0; j < data.series![i].points.length!; j++) {
|
|
339
395
|
const p = data.series![i].points[j];
|
|
@@ -408,6 +464,7 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
408
464
|
const showAxesLabels = w >= MIN_X_AXIS_NAME_VISIBILITY_PX_WIDTH && h >= MIN_Y_AXIS_NAME_VISIBILITY_PX_HEIGHT
|
|
409
465
|
&& !!data.chartOptions?.xAxisName && !!data.chartOptions.yAxisName;
|
|
410
466
|
const showTitle = w >= MIN_TITLE_PX_WIDTH && h >= MIN_TITLE_PX_HEIGHT && !!data.chartOptions?.title;
|
|
467
|
+
const showLegend = w >= MIN_LEGEND_PX_WIDTH && h >= MIN_LEGEND_PX_HEIGHT;
|
|
411
468
|
const showDroplines = w >= MIN_DROPLINES_VISIBILITY_PX_WIDTH && h >= MIN_DROPLINES_VISIBILITY_PX_HEIGHT;
|
|
412
469
|
const [dataBox, xAxisBox, yAxisBox] = layoutChart(screenBounds, showAxesLabels, showTitle);
|
|
413
470
|
|
|
@@ -436,30 +493,53 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
436
493
|
data.chartOptions.showStatistics = [];
|
|
437
494
|
}
|
|
438
495
|
series.points.sort((a, b) => a.x - b.x);
|
|
496
|
+
|
|
497
|
+
if (series.connectDots ?? false) {
|
|
498
|
+
g.strokeStyle = series.pointColor ? DG.Color.fromHtml(series.pointColor) ?
|
|
499
|
+
series.pointColor : DG.Color.toHtml(DG.Color.getCategoricalColor(i)) : DG.Color.toHtml(DG.Color.getCategoricalColor(i));
|
|
500
|
+
g.lineWidth = 2 * ratio;
|
|
501
|
+
g.beginPath();
|
|
502
|
+
for (let j = 0; j < series.points.length; j++) {
|
|
503
|
+
const x = series.points[j].x;
|
|
504
|
+
const y = series.points[j].y;
|
|
505
|
+
const screenX = viewport.xToScreen(x);
|
|
506
|
+
const screenY = viewport.yToScreen(y);
|
|
507
|
+
if (j === 0)
|
|
508
|
+
g.moveTo(screenX, screenY);
|
|
509
|
+
else
|
|
510
|
+
g.lineTo(screenX, screenY);
|
|
511
|
+
}
|
|
512
|
+
g.stroke();
|
|
513
|
+
}
|
|
514
|
+
|
|
439
515
|
let userParamsFlag = true;
|
|
440
516
|
const fitFunc = getSeriesFitFunction(series);
|
|
441
|
-
let curve: (x: number) => number;
|
|
442
|
-
if (series.
|
|
443
|
-
if (
|
|
444
|
-
if (
|
|
445
|
-
series.parameters[2]
|
|
517
|
+
let curve: ((x: number) => number) | null = null;
|
|
518
|
+
if (!(series.connectDots && !series.showFitLine)) {
|
|
519
|
+
if (series.parameters) {
|
|
520
|
+
if (data.chartOptions?.logX) {
|
|
521
|
+
if (series.parameters[2] > 0)
|
|
522
|
+
series.parameters[2] = Math.log10(series.parameters[2]);
|
|
523
|
+
}
|
|
524
|
+
curve = getCurve(series, fitFunc);
|
|
525
|
+
}
|
|
526
|
+
else {
|
|
527
|
+
const fitResult = fitSeries(series, fitFunc, chartLogOptions);
|
|
528
|
+
curve = fitResult.fittedCurve;
|
|
529
|
+
series.parameters = fitResult.parameters;
|
|
530
|
+
userParamsFlag = false;
|
|
446
531
|
}
|
|
447
|
-
curve = getCurve(series, fitFunc);
|
|
448
|
-
}
|
|
449
|
-
else {
|
|
450
|
-
const fitResult = fitSeries(series, fitFunc, chartLogOptions);
|
|
451
|
-
curve = fitResult.fittedCurve;
|
|
452
|
-
series.parameters = fitResult.parameters;
|
|
453
|
-
userParamsFlag = false;
|
|
454
532
|
}
|
|
455
|
-
|
|
533
|
+
|
|
456
534
|
if (series.showPoints ?? 'points') {
|
|
457
535
|
const pointColor = series.pointColor ? DG.Color.fromHtml(series.pointColor) ?
|
|
458
536
|
series.pointColor : DG.Color.toHtml(DG.Color.getCategoricalColor(i)) : DG.Color.toHtml(DG.Color.getCategoricalColor(i));
|
|
459
537
|
const outlierColor = series.outlierColor ? DG.Color.fromHtml(series.outlierColor) ?
|
|
460
538
|
DG.Color.fromHtml(series.outlierColor) : DG.Color.red : DG.Color.red;
|
|
461
539
|
g.strokeStyle = pointColor;
|
|
462
|
-
if (series.showPoints
|
|
540
|
+
if (series.connectDots && series.showPoints != '')
|
|
541
|
+
drawPoints(g, series, viewport, ratio, chartLogOptions, DG.Color.fromHtml(pointColor), outlierColor);
|
|
542
|
+
else if (series.showPoints === 'points')
|
|
463
543
|
drawPoints(g, series, viewport, ratio, chartLogOptions, DG.Color.fromHtml(pointColor), outlierColor);
|
|
464
544
|
else if (['candlesticks', 'both'].includes(series.showPoints!))
|
|
465
545
|
drawCandles(g, series, viewport, ratio, DG.Color.fromHtml(pointColor));
|
|
@@ -480,7 +560,7 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
480
560
|
for (let j = axesLeftPxMargin; j <= screenBounds.width - axesRightPxMargin; j++) {
|
|
481
561
|
const x = screenBounds.x + j;
|
|
482
562
|
const xForY = data.chartOptions?.logX ? Math.log10(viewport.xToWorld(x)) : viewport.xToWorld(x);
|
|
483
|
-
const y = data.chartOptions?.logY ? viewport.yToScreen(Math.pow(10, curve(xForY))) : viewport.yToScreen(curve(xForY));
|
|
563
|
+
const y = data.chartOptions?.logY ? viewport.yToScreen(Math.pow(10, curve!(xForY))) : viewport.yToScreen(curve!(xForY));
|
|
484
564
|
if (j === axesLeftPxMargin)
|
|
485
565
|
g.moveTo(x, y);
|
|
486
566
|
else
|
|
@@ -502,7 +582,7 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
502
582
|
fillConfidenceInterval(g, confidenceIntervals, screenBounds, viewport, showAxes, showAxesLabels, chartLogOptions);
|
|
503
583
|
}
|
|
504
584
|
|
|
505
|
-
if (series.droplines && showDroplines) {
|
|
585
|
+
if ((series.showFitLine ?? true) && series.droplines && showDroplines) {
|
|
506
586
|
g.save();
|
|
507
587
|
g.strokeStyle = 'blue';
|
|
508
588
|
g.lineWidth = ratio;
|
|
@@ -511,13 +591,13 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
511
591
|
for (let j = 0; j < series.droplines.length; j++) {
|
|
512
592
|
const droplineName = series.droplines[j];
|
|
513
593
|
if (droplineName === 'IC50')
|
|
514
|
-
drawDropline(g, viewport, series.parameters[2], dataBounds, curve
|
|
594
|
+
drawDropline(g, viewport, series.parameters![2], dataBounds, curve!, chartLogOptions);
|
|
515
595
|
}
|
|
516
596
|
g.stroke();
|
|
517
597
|
g.restore();
|
|
518
598
|
}
|
|
519
599
|
|
|
520
|
-
if (data.chartOptions?.showStatistics) {
|
|
600
|
+
if ((series.showFitLine ?? true) && data.chartOptions?.showStatistics) {
|
|
521
601
|
const statistics = getSeriesStatistics(series, fitFunc, chartLogOptions);
|
|
522
602
|
for (let j = 0; j < data.chartOptions.showStatistics.length; j++) {
|
|
523
603
|
const statName = data.chartOptions.showStatistics[j];
|
|
@@ -553,6 +633,30 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
553
633
|
g.restore();
|
|
554
634
|
}
|
|
555
635
|
|
|
636
|
+
if (showLegend) {
|
|
637
|
+
g.font = '11px Roboto, "Roboto Local"';
|
|
638
|
+
for (let i = 0; i < data.series?.length!; i++) {
|
|
639
|
+
g.beginPath();
|
|
640
|
+
const series = data.series![i];
|
|
641
|
+
const lineColor = series.fitLineColor ? DG.Color.fromHtml(series.fitLineColor) ?
|
|
642
|
+
series.fitLineColor : DG.Color.toHtml(DG.Color.getCategoricalColor(i)) : DG.Color.toHtml(DG.Color.getCategoricalColor(i));
|
|
643
|
+
const markerColor = series.pointColor ? DG.Color.fromHtml(series.pointColor) ?
|
|
644
|
+
DG.Color.fromHtml(series.pointColor) : DG.Color.getCategoricalColor(i) : DG.Color.getCategoricalColor(i);
|
|
645
|
+
g.strokeStyle = lineColor;
|
|
646
|
+
g.lineWidth = 2 * ratio;
|
|
647
|
+
const text = `${data.chartOptions?.showColumnLabel ? series.columnName ?? '' : ''} ${series.name ?? ''}`;
|
|
648
|
+
const textWidth = g.measureText(text).width;
|
|
649
|
+
g.moveTo(dataBox.maxX - textWidth - 25, dataBox.y + 20 - 4 + 20 * i);
|
|
650
|
+
g.lineTo(dataBox.maxX - textWidth - 5, dataBox.y + 20 - 4 + 20 * i);
|
|
651
|
+
const marker = series.markerType ? series.markerType as DG.MARKER_TYPE : DG.MARKER_TYPE.CIRCLE;
|
|
652
|
+
DG.Paint.marker(g, marker, dataBox.maxX - textWidth - 25 + (20 / 2), dataBox.y + 20 - 4 + 20 * i, markerColor, POINT_PX_SIZE * ratio);
|
|
653
|
+
g.fillStyle = 'black';
|
|
654
|
+
g.fillText(text, dataBox.maxX - textWidth, dataBox.y + 20 + 20 * i);
|
|
655
|
+
g.stroke();
|
|
656
|
+
}
|
|
657
|
+
g.restore();
|
|
658
|
+
}
|
|
659
|
+
|
|
556
660
|
g.restore();
|
|
557
661
|
}
|
|
558
662
|
|
|
@@ -568,8 +672,31 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
568
672
|
? convertXMLToIFitChartData(gridCell.cell.value)
|
|
569
673
|
: getChartData(gridCell);
|
|
570
674
|
|
|
571
|
-
|
|
675
|
+
const screenBounds = new DG.Rect(x, y, w, h).inflate(INFLATE_SIZE / 2, INFLATE_SIZE / 2);
|
|
676
|
+
|
|
677
|
+
if (data.series?.length === 0) {
|
|
678
|
+
showIncorrectFitCell(g, screenBounds,'No series to show');
|
|
572
679
|
return;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
if (data.series?.some((series) => series.points.length === 0)) {
|
|
683
|
+
showIncorrectFitCell(g, screenBounds, 'There were series with no points');
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
if (data.series?.some((series) => series.points.every((point) => point.x === 0))) {
|
|
688
|
+
showIncorrectFitCell(g, screenBounds, 'There were series with all x zeroes');
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
if (data.series?.some((series) => series.points.every((point) => point.y === 0))) {
|
|
693
|
+
showIncorrectFitCell(g, screenBounds, 'There were series with all y zeroes');
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
data.series?.forEach((series) => series.columnName = gridCell.cell.column.name);
|
|
698
|
+
if (data.chartOptions?.mergeSeries)
|
|
699
|
+
data.series = [mergeSeries(data.series!)!];
|
|
573
700
|
|
|
574
701
|
this.renderCurves(g, x, y, w, h, data);
|
|
575
702
|
}
|
|
@@ -591,27 +718,33 @@ export class FitChartCellRenderer extends DG.GridCellRenderer {
|
|
|
591
718
|
: getChartData(gridCell);
|
|
592
719
|
|
|
593
720
|
const screenBounds = gridCell.bounds.inflate(INFLATE_SIZE / 2, INFLATE_SIZE / 2);
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
721
|
+
if (screenBounds.width >= MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH && screenBounds.height >= MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT) {
|
|
722
|
+
const showAxesLabels = gridCell.bounds.width >= MIN_X_AXIS_NAME_VISIBILITY_PX_WIDTH && gridCell.bounds.height >= MIN_Y_AXIS_NAME_VISIBILITY_PX_HEIGHT;
|
|
723
|
+
const showTitle = gridCell.bounds.width >= MIN_TITLE_PX_WIDTH && gridCell.bounds.height >= MIN_TITLE_PX_HEIGHT;
|
|
724
|
+
const dataBox = layoutChart(screenBounds, showAxesLabels, showTitle)[0];
|
|
725
|
+
const dataBounds = getChartBounds(data);
|
|
726
|
+
const viewport = new Viewport(dataBounds, dataBox, data.chartOptions?.logX ?? false, data.chartOptions?.logY ?? false);
|
|
727
|
+
|
|
728
|
+
for (let i = 0; i < data.series?.length!; i++) {
|
|
729
|
+
if (data.series![i].showPoints !== 'points')
|
|
730
|
+
continue;
|
|
731
|
+
for (let j = 0; j < data.series![i].points.length!; j++) {
|
|
732
|
+
const p = data.series![i].points[j];
|
|
733
|
+
const screenX = viewport.xToScreen(p.x);
|
|
734
|
+
const screenY = viewport.yToScreen(p.y);
|
|
735
|
+
const pxPerMarkerType = ((p.outlier ? OUTLIER_PX_SIZE : POINT_PX_SIZE) / 2) + OUTLIER_HITBOX_RADIUS;
|
|
736
|
+
if (e.offsetX >= screenX - pxPerMarkerType && e.offsetX <= screenX + pxPerMarkerType &&
|
|
737
|
+
e.offsetY >= screenY - pxPerMarkerType && e.offsetY <= screenY + pxPerMarkerType) {
|
|
738
|
+
ui.tooltip.show(ui.divV([ui.divText(`x: ${DG.format(p.x, '#0.000')}`),
|
|
739
|
+
ui.divText(`y: ${DG.format(p.y, '#0.000')}`)]), e.x + 16, e.y + 16);
|
|
740
|
+
if (!data.series![i].connectDots && data.series![i].clickToToggle && screenBounds.width >= MIN_AXES_CELL_PX_WIDTH &&
|
|
741
|
+
screenBounds.height >= MIN_AXES_CELL_PX_HEIGHT)
|
|
742
|
+
document.body.style.cursor = 'pointer';
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
613
745
|
}
|
|
614
746
|
}
|
|
747
|
+
ui.tooltip.hide();
|
|
615
748
|
}
|
|
616
749
|
document.body.style.cursor = 'default';
|
|
617
750
|
}
|
|
@@ -2,33 +2,37 @@ 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} from './fit-renderer';
|
|
6
|
-
import {getChartData, mergeProperties} from '
|
|
5
|
+
import {FitChartCellRenderer, mergeSeries} from './fit-renderer';
|
|
6
|
+
import {getChartData, mergeProperties} from './fit-renderer';
|
|
7
7
|
import {FIT_SEM_TYPE, FitChartData, fitChartDataProperties, IFitChartData} 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
11
|
|
|
12
12
|
|
|
13
|
+
const ERROR_CLASS = 'd4-viewer-error';
|
|
14
|
+
|
|
13
15
|
export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
|
|
14
|
-
|
|
16
|
+
curvesColumnNames?: string[] = [];
|
|
15
17
|
showSelectedRowsCurves: boolean = false;
|
|
16
18
|
showCurrentRowCurve: boolean = true;
|
|
17
19
|
showMouseOverRowCurve: boolean = true;
|
|
20
|
+
mergeColumnSeries: boolean = false;
|
|
18
21
|
rows: number[] = [];
|
|
19
22
|
data: IFitChartData = new FitChartData();
|
|
20
23
|
|
|
21
24
|
constructor() {
|
|
22
25
|
super(new FitChartCellRenderer());
|
|
23
26
|
|
|
24
|
-
this.
|
|
27
|
+
this.curvesColumnNames = this.addProperty('curvesColumnNames', DG.TYPE.COLUMN_LIST);
|
|
25
28
|
|
|
26
29
|
this.showSelectedRowsCurves = this.bool('showSelectedRowsCurves', false, { description: 'Adds curves from the selected rows'});
|
|
27
30
|
this.showCurrentRowCurve = this.bool('showCurrentRowCurve', true);
|
|
28
31
|
this.showMouseOverRowCurve = this.bool('showMouseOverRowCurve', true);
|
|
32
|
+
this.mergeColumnSeries = this.bool('mergeColumnSeries', false);
|
|
29
33
|
|
|
30
34
|
for (const p of fitChartDataProperties)
|
|
31
|
-
this.addProperty(p.name, p.propertyType, p.defaultValue, p.options);
|
|
35
|
+
this.addProperty(p.name === 'mergeSeries' ? 'mergeCellSeries' : p.name, p.propertyType, p.defaultValue, p.options);
|
|
32
36
|
}
|
|
33
37
|
|
|
34
38
|
static fromChartData(chartData: IFitChartData): MultiCurveViewer {
|
|
@@ -43,7 +47,7 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
|
|
|
43
47
|
}
|
|
44
48
|
|
|
45
49
|
createChartData(): void {
|
|
46
|
-
if (
|
|
50
|
+
if (this.curvesColumnNames?.length === 0)
|
|
47
51
|
return;
|
|
48
52
|
this.rows.length = 0;
|
|
49
53
|
if (this.showCurrentRowCurve && this.dataFrame.currentRowIdx !== -1)
|
|
@@ -55,20 +59,48 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
|
|
|
55
59
|
|
|
56
60
|
this.data = new FitChartData();
|
|
57
61
|
const grid = this.tableView?.grid!;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
62
|
+
this.data.chartOptions!.showColumnLabel = this.props.get('showColumnLabel') as unknown as boolean;
|
|
63
|
+
const mergeCellSeries = this.props.get('mergeCellSeries') as unknown as boolean;
|
|
64
|
+
for (const colName of this.curvesColumnNames!) {
|
|
65
|
+
const series = [];
|
|
66
|
+
for (const i of new Set(this.rows)) {
|
|
67
|
+
const gridCell = grid.cell(colName, grid.tableRowToGrid(i));
|
|
68
|
+
const cellCurves = getChartData(gridCell);
|
|
69
|
+
cellCurves.series?.forEach((series) => series.columnName = gridCell.cell.column.name);
|
|
70
|
+
if (mergeCellSeries)
|
|
71
|
+
cellCurves.series = [mergeSeries(cellCurves.series!)!];
|
|
72
|
+
series.push(...cellCurves.series!);
|
|
73
|
+
}
|
|
74
|
+
if (this.mergeColumnSeries)
|
|
75
|
+
this.data.series?.push(mergeSeries(series)!);
|
|
76
|
+
else
|
|
77
|
+
this.data.series?.push(...series);
|
|
61
78
|
}
|
|
79
|
+
this.data.series?.forEach((series, i) => {
|
|
80
|
+
series.pointColor = DG.Color.toHtml(DG.Color.getCategoricalColor(this.data.series?.length! > 20 ? 0 : i));
|
|
81
|
+
series.fitLineColor = DG.Color.toHtml(DG.Color.getCategoricalColor(this.data.series?.length! > 20 ? 0 : i));
|
|
82
|
+
series.showCurveConfidenceInterval = false;
|
|
83
|
+
series.droplines = [];
|
|
84
|
+
if (this.data.series?.length! > 20) {
|
|
85
|
+
series.showPoints = '';
|
|
86
|
+
series.lineStyle = 'solid';
|
|
87
|
+
}
|
|
88
|
+
});
|
|
62
89
|
}
|
|
63
90
|
|
|
64
91
|
onPropertyChanged(property: DG.Property | null): void {
|
|
92
|
+
if (property?.name === 'curvesColumnNames')
|
|
93
|
+
this.props.set('showColumnLabel', this.curvesColumnNames?.length! > 1 as unknown as object);
|
|
65
94
|
this.applyViewerProperties();
|
|
95
|
+
this.createChartData();
|
|
66
96
|
this.render();
|
|
67
97
|
}
|
|
68
98
|
|
|
69
99
|
onTableAttached(): void {
|
|
70
100
|
const grid = this.tableView?.grid!;
|
|
71
|
-
|
|
101
|
+
const fitCol = this.dataFrame.columns.bySemType(FIT_SEM_TYPE);
|
|
102
|
+
if (fitCol !== null)
|
|
103
|
+
this.curvesColumnNames = [fitCol.name];
|
|
72
104
|
|
|
73
105
|
merge(this.dataFrame.onCurrentCellChanged, grid.onCellMouseEnter, this.dataFrame.onSelectionChanged)
|
|
74
106
|
.pipe(debounce(_ => interval(50)))
|
|
@@ -82,14 +114,21 @@ export class MultiCurveViewer extends CellRenderViewer<FitChartCellRenderer> {
|
|
|
82
114
|
this.root.appendChild(ui.divText(msg, 'd4-viewer-error'));
|
|
83
115
|
}
|
|
84
116
|
|
|
117
|
+
_removeErrorMessage() {
|
|
118
|
+
const divTextElement = this.root.getElementsByClassName(ERROR_CLASS)[0];
|
|
119
|
+
if (divTextElement)
|
|
120
|
+
this.root.removeChild(divTextElement);
|
|
121
|
+
}
|
|
122
|
+
|
|
85
123
|
render(): void {
|
|
86
|
-
|
|
124
|
+
const g = this.canvas.getContext('2d')!
|
|
125
|
+
g.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
126
|
+
|
|
127
|
+
if (this.curvesColumnNames?.length === 0) {
|
|
87
128
|
this._showErrorMessage('The MultiCurveViewer viewer requires a minimum of 1 curves column.');
|
|
88
129
|
return;
|
|
89
130
|
}
|
|
90
|
-
|
|
91
|
-
const g = this.canvas.getContext('2d')!
|
|
92
|
-
g.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
131
|
+
this._removeErrorMessage();
|
|
93
132
|
this.renderer.renderCurves(g, 0, 0, this.canvas.width, this.canvas.height, this.data);
|
|
94
133
|
}
|
|
95
134
|
}
|
package/src/package.ts
CHANGED
|
@@ -29,6 +29,7 @@ export function fitCellRenderer(): FitChartCellRenderer {
|
|
|
29
29
|
//name: MultiCurveViewer
|
|
30
30
|
//description: A viewer that superimposes multiple in-cell curves on one chart
|
|
31
31
|
//tags: viewer
|
|
32
|
+
//meta.icon: icons/multi-curve-viewer.png
|
|
32
33
|
//output: viewer result
|
|
33
34
|
export function _FitViewer(): MultiCurveViewer {
|
|
34
35
|
return new MultiCurveViewer();
|