@datagrok/curves 1.5.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.
@@ -17,9 +17,18 @@ import {BoxPlotStatistics, calculateBoxPlotStatistics} from "@datagrok-libraries
17
17
  import {StringUtils} from "@datagrok-libraries/utils/src/string-utils";
18
18
 
19
19
 
20
+ export enum ColorType {
21
+ POINT = 'pointColor',
22
+ OUTLIER = 'outlierColor',
23
+ FIT_LINE = 'fitLineColor',
24
+ }
25
+ export type SeriesColorType = `${ColorType}` | string;
26
+
20
27
  interface FitRenderOptions {
21
28
  viewport: Viewport;
29
+ screenBounds?: DG.Rect;
22
30
  ratio?: number;
31
+ seriesIdx?: number;
23
32
  }
24
33
 
25
34
  interface FitPointRenderOptions extends FitRenderOptions {
@@ -41,6 +50,7 @@ interface FitConfidenceIntervalRenderOptions extends FitLineRenderOptions {
41
50
  userParamsFlag?: boolean;
42
51
  confidenceIntervals?: FitConfidenceIntervals;
43
52
  confidenceType?: string;
53
+ dataPoints?: {x: number[], y: number[]};
44
54
  }
45
55
 
46
56
  interface FitDroplineRenderOptions extends FitRenderOptions {
@@ -56,6 +66,9 @@ interface FitStatisticsRenderOptions {
56
66
  fitFunc: FitFunction;
57
67
  logOptions: LogOptions;
58
68
  dataBox: DG.Rect;
69
+ screenBounds?: DG.Rect;
70
+ dataPoints?: {x: number[], y: number[]};
71
+ seriesIdx?: number;
59
72
  }
60
73
 
61
74
  interface FitTitleRenderOptions {
@@ -81,31 +94,26 @@ interface FitLegendColumnlabelSeriesRenderOptions extends FitLegendRenderOptions
81
94
  columnIdx: number;
82
95
  drawnCurvesInLegend: number;
83
96
  showColumnLabel?: boolean;
97
+ seriesIdx?: number;
84
98
  }
85
99
 
86
100
 
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;
101
+ export function getSeriesColor(series: IFitSeries, seriesIdx: number, colorType: SeriesColorType): string {
102
+ const color = DG.Color.toHtml(colorType === 'outlierColor' ? DG.Color.red : DG.Color.getCategoricalColor(seriesIdx));
103
+ return series[colorType] ? DG.Color.fromHtml(series[colorType]) ? series[colorType] : color : color;
97
104
  }
98
105
 
99
106
  export function renderConnectDots(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitRenderOptions): void {
100
107
  if (series.connectDots ?? false) {
101
- g.strokeStyle = series.pointColor!;
108
+ const viewport = renderOptions.viewport;
109
+ g.strokeStyle = getSeriesColor(series, renderOptions.seriesIdx!, 'pointColor');
102
110
  g.lineWidth = 2 * renderOptions.ratio!;
103
111
  g.beginPath();
104
112
  for (let i = 0; i < series.points.length; i++) {
105
113
  const x = series.points[i].x;
106
114
  const y = series.points[i].y;
107
- const screenX = renderOptions.viewport.xToScreen(x);
108
- const screenY = renderOptions.viewport.yToScreen(y);
115
+ const screenX = viewport.xToScreen(x);
116
+ const screenY = viewport.yToScreen(y);
109
117
  if (i === 0)
110
118
  g.moveTo(screenX, screenY);
111
119
  else
@@ -116,8 +124,11 @@ export function renderConnectDots(g: CanvasRenderingContext2D, series: IFitSerie
116
124
  }
117
125
 
118
126
  export function renderPoints(g: CanvasRenderingContext2D, series: IFitSeries, options: FitRenderOptions) {
119
- if (series.showPoints ?? 'points') {
120
- g.strokeStyle = series.pointColor!;
127
+ const screenBounds = options.screenBounds!;
128
+ const showPoints = screenBounds.width < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH ||
129
+ screenBounds.height < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT ? '' : series.showPoints ?? 'points';
130
+ if (showPoints) {
131
+ g.strokeStyle = getSeriesColor(series, options.seriesIdx!, ColorType.POINT);
121
132
  if ((series.connectDots && series.showPoints !== '') || series.showPoints === 'points')
122
133
  drawPoints(g, series, options);
123
134
  else if (['candlesticks', 'both'].includes(series.showPoints!))
@@ -127,22 +138,31 @@ export function renderPoints(g: CanvasRenderingContext2D, series: IFitSeries, op
127
138
 
128
139
  /** Performs points drawing */
129
140
  function drawPoints(g: CanvasRenderingContext2D, series: IFitSeries, options: FitRenderOptions): void {
141
+ const ratio = options.ratio!;
142
+ const defaultSize = FitConstants.POINT_PX_SIZE * ratio;
143
+ const viewport = options.viewport;
144
+ const connectDots = series.connectDots;
145
+ const pointColor = getSeriesColor(series, options.seriesIdx!, ColorType.POINT);
146
+ const outlierColor = getSeriesColor(series, options.seriesIdx!, ColorType.OUTLIER);
147
+
130
148
  for (let i = 0; i < series.points.length!; i++) {
131
149
  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!);
150
+ const xScreen = viewport.xToScreen(p.x);
151
+ const yScreen = viewport.yToScreen(p.y);
152
+ const color = connectDots ? pointColor :
153
+ p.outlier ? (p.outlierColor ? DG.Color.fromHtml(p.outlierColor) ? p.outlierColor : outlierColor : outlierColor) :
154
+ (p.color ? DG.Color.fromHtml(p.color) ? p.color : pointColor : pointColor);
135
155
  const marker = p.marker ? p.marker as DG.MARKER_TYPE : series.markerType as DG.MARKER_TYPE;
136
156
  const outlierMarker = p.outlierMarker ? p.outlierMarker as DG.MARKER_TYPE : series.outlierMarkerType as DG.MARKER_TYPE;
137
- const size = !series.connectDots ? p.outlier ? FitConstants.OUTLIER_PX_SIZE * options.ratio! :
138
- p.size ? p.size : FitConstants.POINT_PX_SIZE * options.ratio! : FitConstants.POINT_PX_SIZE * options.ratio!;
139
- DG.Paint.marker(g, !series.connectDots ? p.outlier ? outlierMarker : marker : marker,
140
- options.viewport.xToScreen(p.x), options.viewport.yToScreen(p.y), color, size);
157
+ const size = !connectDots ? p.outlier ? FitConstants.OUTLIER_PX_SIZE * ratio : p.size ? p.size : defaultSize : defaultSize;
158
+ const markerToDraw = !connectDots ? p.outlier ? outlierMarker : marker : marker;
159
+
160
+ DG.Paint.marker(g, markerToDraw, xScreen, yScreen, color, size);
141
161
  if (p.stdev && !p.outlier) {
142
162
  g.strokeStyle = color;
143
163
  g.beginPath();
144
- g.moveTo(options.viewport.xToScreen(p.x), options.viewport.yToScreen(p.y + p.stdev));
145
- g.lineTo(options.viewport.xToScreen(p.x), options.viewport.yToScreen(p.y - p.stdev));
164
+ g.moveTo(xScreen, viewport.yToScreen(p.y + p.stdev));
165
+ g.lineTo(xScreen, viewport.yToScreen(p.y - p.stdev));
146
166
  g.stroke();
147
167
  }
148
168
  }
@@ -150,6 +170,9 @@ function drawPoints(g: CanvasRenderingContext2D, series: IFitSeries, options: Fi
150
170
 
151
171
  /** Performs candles drawing */
152
172
  function drawCandles(g: CanvasRenderingContext2D, series: IFitSeries, options: FitRenderOptions) : void {
173
+ const ratio = options.ratio!;
174
+ const viewport = options.viewport;
175
+ const pointColor = getSeriesColor(series, options.seriesIdx!, ColorType.POINT);
153
176
  for (let i = 0, candleStart = null; i < series.points.length!; i++) {
154
177
  const p = series.points[i];
155
178
  if (p.outlier)
@@ -164,16 +187,14 @@ function drawCandles(g: CanvasRenderingContext2D, series: IFitSeries, options: F
164
187
  const boxPlotStats = calculateBoxPlotStatistics(values);
165
188
 
166
189
  g.beginPath();
167
- drawCandlestick(g, {viewport: options.viewport, ratio: options.ratio!, x: p.x,
168
- boxPlotStats: boxPlotStats, color: series.pointColor!});
190
+ drawCandlestick(g, {viewport: viewport, ratio: ratio, x: p.x, boxPlotStats: boxPlotStats, color: pointColor});
169
191
  g.stroke();
170
192
 
171
193
  if (series.showPoints === 'both') {
172
194
  for (let ind = 0; ind < values.length; ind++) {
173
195
  if (values[ind] < boxPlotStats.lowerAdjacentValue || values[ind] > boxPlotStats.upperAdjacentValue) {
174
- DG.Paint.marker(g, DG.MARKER_TYPE.OUTLIER,
175
- options.viewport.xToScreen(p.x), options.viewport.yToScreen(values[ind]),
176
- series.pointColor!, FitConstants.CANDLESTICK_OUTLIER_PX_SIZE * options.ratio!);
196
+ DG.Paint.marker(g, DG.MARKER_TYPE.OUTLIER, viewport.xToScreen(p.x),
197
+ viewport.yToScreen(values[ind]), pointColor, FitConstants.CANDLESTICK_OUTLIER_PX_SIZE * ratio);
177
198
  }
178
199
  }
179
200
  }
@@ -185,43 +206,59 @@ function drawCandles(g: CanvasRenderingContext2D, series: IFitSeries, options: F
185
206
 
186
207
  /** Performs candlestick drawing */
187
208
  function drawCandlestick(g: CanvasRenderingContext2D, renderOptions: FitPointRenderOptions): void {
188
- drawCandlestickBorder(g, renderOptions.x, renderOptions.boxPlotStats.lowerAdjacentValue, renderOptions.viewport);
189
- g.moveTo(renderOptions.viewport.xToScreen(renderOptions.x), renderOptions.viewport.yToScreen(renderOptions.boxPlotStats.lowerAdjacentValue));
190
- g.lineTo(renderOptions.viewport.xToScreen(renderOptions.x), renderOptions.viewport.yToScreen(renderOptions.boxPlotStats.upperAdjacentValue));
191
- drawCandlestickBorder(g, renderOptions.x, renderOptions.boxPlotStats.upperAdjacentValue, renderOptions.viewport);
192
- DG.Paint.marker(g, DG.MARKER_TYPE.CIRCLE, renderOptions.viewport.xToScreen(renderOptions.x),
193
- renderOptions.viewport.yToScreen(renderOptions.boxPlotStats.q2), renderOptions.color,
209
+ const viewport = renderOptions.viewport;
210
+ const x = renderOptions.x;
211
+ const screenX = viewport.xToScreen(x);
212
+ const boxPlotStats = renderOptions.boxPlotStats;
213
+
214
+ drawCandlestickBorder(g, x, boxPlotStats.lowerAdjacentValue, viewport);
215
+ g.moveTo(screenX, viewport.yToScreen(boxPlotStats.lowerAdjacentValue));
216
+ g.lineTo(screenX, viewport.yToScreen(boxPlotStats.upperAdjacentValue));
217
+ drawCandlestickBorder(g, x, boxPlotStats.upperAdjacentValue, viewport);
218
+ DG.Paint.marker(g, DG.MARKER_TYPE.CIRCLE, screenX, viewport.yToScreen(boxPlotStats.q2), renderOptions.color,
194
219
  FitConstants.CANDLESTICK_MEDIAN_PX_SIZE * renderOptions.ratio!);
195
220
  }
196
221
 
197
222
  /** Performs candlestick border drawing */
198
223
  function drawCandlestickBorder(g: CanvasRenderingContext2D, x: number, adjacentValue: number, transform: Viewport): void {
199
- g.moveTo(transform.xToScreen(x) - (FitConstants.CANDLESTICK_BORDER_PX_SIZE / 2), transform.yToScreen(adjacentValue));
200
- g.lineTo(transform.xToScreen(x) + (FitConstants.CANDLESTICK_BORDER_PX_SIZE / 2), transform.yToScreen(adjacentValue));
224
+ const xScreen = transform.xToScreen(x);
225
+ const yScreen = transform.yToScreen(adjacentValue);
226
+ g.moveTo(xScreen - (FitConstants.CANDLESTICK_BORDER_PX_SIZE / 2), yScreen);
227
+ g.lineTo(xScreen + (FitConstants.CANDLESTICK_BORDER_PX_SIZE / 2), yScreen);
228
+ }
229
+
230
+ /** Gets rendering variables */
231
+ function getRenderingVariables(renderOptions: FitConfidenceIntervalRenderOptions | FitLineRenderOptions) {
232
+ const logX = renderOptions.logOptions.logX;
233
+ const logY = renderOptions.logOptions.logY;
234
+ const screenBounds = renderOptions.screenBounds;
235
+ const axesLeftPxMargin = renderOptions.showAxes ? renderOptions.showAxesLabels ?
236
+ FitConstants.AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS : FitConstants.AXES_LEFT_PX_MARGIN : 0;
237
+ const axesRightPxMargin = renderOptions.showAxes ? FitConstants.AXES_RIGHT_PX_MARGIN : 0;
238
+ const xMin = screenBounds.x + axesLeftPxMargin;
239
+ const xMax = screenBounds.x + screenBounds.width - axesRightPxMargin;
240
+ const viewport = renderOptions.viewport;
241
+ return {logX, logY, xMin, xMax, viewport};
201
242
  }
202
243
 
203
244
  export function renderFitLine(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitLineRenderOptions): void {
204
245
  if (series.showFitLine ?? true) {
205
246
  g.save();
206
- g.strokeStyle = series.fitLineColor!;
247
+ g.strokeStyle = getSeriesColor(series, renderOptions.seriesIdx!, ColorType.FIT_LINE);
207
248
  g.lineWidth = 2 * renderOptions.ratio!;
208
249
  g.beginPath();
209
250
  if (series.lineStyle)
210
251
  g.setLineDash(FitConstants.LINE_STYLES[series.lineStyle]);
211
-
212
- const axesLeftPxMargin = renderOptions.showAxes ? renderOptions.showAxesLabels ?
213
- FitConstants.AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS : FitConstants.AXES_LEFT_PX_MARGIN : 0;
214
- const axesRightPxMargin = renderOptions.showAxes ? FitConstants.AXES_RIGHT_PX_MARGIN : 0;
215
- for (let j = axesLeftPxMargin; j <= renderOptions.screenBounds.width - axesRightPxMargin; j++) {
216
- const x = renderOptions.screenBounds.x + j;
217
- const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) :
218
- renderOptions.viewport.xToWorld(x);
219
- const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(Math.pow(10, renderOptions.curveFunc!(xForY))) :
220
- renderOptions.viewport.yToScreen(renderOptions.curveFunc!(xForY));
221
- if (j === axesLeftPxMargin)
222
- g.moveTo(x, y);
252
+ const {logX, logY, xMin, xMax, viewport} = getRenderingVariables(renderOptions);
253
+ const curveFunc = renderOptions.curveFunc!;
254
+
255
+ for (let i = xMin; i <= xMax; i++) {
256
+ const xForY = logX ? Math.log10(viewport.xToWorld(i)) : viewport.xToWorld(i);
257
+ const y = logY ? viewport.yToScreen(Math.pow(10, curveFunc(xForY))) : viewport.yToScreen(curveFunc(xForY));
258
+ if (i === xMin)
259
+ g.moveTo(i, y);
223
260
  else
224
- g.lineTo(x, y);
261
+ g.lineTo(i, y);
225
262
  }
226
263
  g.stroke();
227
264
  g.restore();
@@ -233,38 +270,38 @@ export function renderConfidenceIntervals(g: CanvasRenderingContext2D, series: I
233
270
  // TODO: improve confidence intervals colors
234
271
  g.strokeStyle = series.confidenceIntervalColor ?? FitConstants.CONFIDENCE_INTERVAL_STROKE_COLOR;
235
272
  g.fillStyle = series.confidenceIntervalColor ?? FitConstants.CONFIDENCE_INTERVAL_FILL_COLOR;
236
-
237
- const confidenceIntervals = getSeriesConfidenceInterval(series, renderOptions.fitFunc!,
238
- renderOptions.userParamsFlag!, renderOptions.logOptions);
239
- drawConfidenceInterval(g, {viewport: renderOptions.viewport, logOptions: renderOptions.logOptions,
240
- showAxes: renderOptions.showAxes, showAxesLabels: renderOptions.showAxesLabels, screenBounds: renderOptions.screenBounds,
241
- confidenceIntervals: confidenceIntervals, confidenceType: FitConstants.CURVE_CONFIDENCE_INTERVAL_BOUNDS.TOP});
242
- drawConfidenceInterval(g, {viewport: renderOptions.viewport, logOptions: renderOptions.logOptions,
243
- showAxes: renderOptions.showAxes, showAxesLabels: renderOptions.showAxesLabels, screenBounds: renderOptions.screenBounds,
244
- confidenceIntervals: confidenceIntervals, confidenceType: FitConstants.CURVE_CONFIDENCE_INTERVAL_BOUNDS.BOTTOM});
245
- fillConfidenceInterval(g, {viewport: renderOptions.viewport, logOptions: renderOptions.logOptions,
246
- showAxes: renderOptions.showAxes, showAxesLabels: renderOptions.showAxesLabels, screenBounds: renderOptions.screenBounds,
247
- confidenceIntervals: confidenceIntervals});
273
+ const showAxes = renderOptions.showAxes;
274
+ const showAxesLabels = renderOptions.showAxesLabels;
275
+ const logOptions = renderOptions.logOptions;
276
+ const viewport = renderOptions.viewport;
277
+ const screenBounds = renderOptions.screenBounds;
278
+ const dataPoints = series.dataPoints;
279
+
280
+ const confidenceIntervals = getSeriesConfidenceInterval(series, renderOptions.fitFunc!, renderOptions.userParamsFlag!, dataPoints, logOptions);
281
+ drawConfidenceInterval(g, {viewport: viewport, logOptions: logOptions, showAxes: showAxes, showAxesLabels: showAxesLabels,
282
+ screenBounds: screenBounds, confidenceIntervals: confidenceIntervals, confidenceType: FitConstants.CURVE_CONFIDENCE_INTERVAL_BOUNDS.TOP});
283
+ drawConfidenceInterval(g, {viewport: viewport, logOptions: logOptions, showAxes: showAxes, showAxesLabels: showAxesLabels,
284
+ screenBounds: screenBounds, confidenceIntervals: confidenceIntervals, confidenceType: FitConstants.CURVE_CONFIDENCE_INTERVAL_BOUNDS.BOTTOM});
285
+ fillConfidenceInterval(g, {viewport: viewport, logOptions: logOptions, showAxes: showAxes, showAxesLabels: showAxesLabels,
286
+ screenBounds: screenBounds, confidenceIntervals: confidenceIntervals});
248
287
  }
249
288
  }
250
289
 
251
290
  /** Performs a curve confidence interval drawing */
252
291
  function drawConfidenceInterval(g: CanvasRenderingContext2D, renderOptions: FitConfidenceIntervalRenderOptions): void {
253
292
  g.beginPath();
254
- const axesLeftPxMargin = renderOptions.showAxes ? renderOptions.showAxesLabels ?
255
- FitConstants.AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS : FitConstants.AXES_LEFT_PX_MARGIN : 0;
256
- const axesRightPxMargin = renderOptions.showAxes ? FitConstants.AXES_RIGHT_PX_MARGIN : 0;
293
+
294
+ const {logX, logY, xMin, xMax, viewport} = getRenderingVariables(renderOptions);
257
295
  const confIntervalFunc = renderOptions.confidenceType === FitConstants.CURVE_CONFIDENCE_INTERVAL_BOUNDS.TOP ?
258
296
  renderOptions.confidenceIntervals!.confidenceTop : renderOptions.confidenceIntervals!.confidenceBottom;
259
- for (let i = axesLeftPxMargin; i <= renderOptions.screenBounds.width - axesRightPxMargin; i++) {
260
- const x = renderOptions.screenBounds.x + i;
261
- const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) : renderOptions.viewport.xToWorld(x);
262
- const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(Math.pow(10, confIntervalFunc(xForY))) :
263
- renderOptions.viewport.yToScreen(confIntervalFunc(xForY));
264
- if (i === axesLeftPxMargin)
265
- g.moveTo(x, y);
297
+
298
+ for (let i = xMin; i <= xMax; i++) {
299
+ const xForY = logX ? Math.log10(viewport.xToWorld(i)) : viewport.xToWorld(i);
300
+ const y = logY ? viewport.yToScreen(Math.pow(10, confIntervalFunc(xForY))) : viewport.yToScreen(confIntervalFunc(xForY));
301
+ if (i === xMin)
302
+ g.moveTo(i, y);
266
303
  else
267
- g.lineTo(x, y);
304
+ g.lineTo(i, y);
268
305
  }
269
306
  g.stroke();
270
307
  }
@@ -272,31 +309,25 @@ function drawConfidenceInterval(g: CanvasRenderingContext2D, renderOptions: FitC
272
309
  /** Performs a curve confidence interval color filling */
273
310
  function fillConfidenceInterval(g: CanvasRenderingContext2D, renderOptions: FitConfidenceIntervalRenderOptions): void {
274
311
  g.beginPath();
275
- const axesLeftPxMargin = renderOptions.showAxes ? renderOptions.showAxesLabels ?
276
- FitConstants.AXES_LEFT_PX_MARGIN_WITH_AXES_LABELS : FitConstants.AXES_LEFT_PX_MARGIN : 0;
277
- const axesRightPxMargin = renderOptions.showAxes ? FitConstants.AXES_RIGHT_PX_MARGIN : 0;
278
- for (let i = axesLeftPxMargin; i <= renderOptions.screenBounds.width - axesRightPxMargin; i++) {
279
- const x = renderOptions.screenBounds.x + i;
280
- const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) :
281
- renderOptions.viewport.xToWorld(x);
282
- const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(
283
- Math.pow(10, renderOptions.confidenceIntervals!.confidenceTop(xForY))) :
284
- renderOptions.viewport.yToScreen(renderOptions.confidenceIntervals!.confidenceTop(xForY));
285
- if (i === axesLeftPxMargin)
286
- g.moveTo(x, y);
312
+
313
+ const {logX, logY, xMin, xMax, viewport} = getRenderingVariables(renderOptions);
314
+ const confTop = renderOptions.confidenceIntervals!.confidenceTop;
315
+ const confBottom = renderOptions.confidenceIntervals!.confidenceBottom;
316
+
317
+ for (let i = xMin; i <= xMax; i++) {
318
+ const xForY = logX ? Math.log10(viewport.xToWorld(i)) : viewport.xToWorld(i);
319
+ const y = logY ? viewport.yToScreen(Math.pow(10, confTop(xForY))) : viewport.yToScreen(confTop(xForY));
320
+ if (i === xMin)
321
+ g.moveTo(i, y);
287
322
  else
288
- g.lineTo(x, y);
323
+ g.lineTo(i, y);
289
324
  }
290
325
 
291
326
  // reverse traverse to make a shape of confidence interval to fill it
292
- for (let i = renderOptions.screenBounds.width - axesRightPxMargin; i >= axesLeftPxMargin; i--) {
293
- const x = renderOptions.screenBounds.x + i;
294
- const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.viewport.xToWorld(x)) :
295
- renderOptions.viewport.xToWorld(x);
296
- const y = renderOptions.logOptions.logY ? renderOptions.viewport.yToScreen(
297
- Math.pow(10, renderOptions.confidenceIntervals!.confidenceBottom(xForY))) :
298
- renderOptions.viewport.yToScreen(renderOptions.confidenceIntervals!.confidenceBottom(xForY));
299
- g.lineTo(x, y);
327
+ for (let i = xMax; i >= xMin; i--) {
328
+ const xForY = logX ? Math.log10(viewport.xToWorld(i)) : viewport.xToWorld(i);
329
+ const y = logY ? viewport.yToScreen(Math.pow(10, confBottom(xForY))) : viewport.yToScreen(confBottom(xForY));
330
+ g.lineTo(i, y);
300
331
  }
301
332
  g.closePath();
302
333
  g.fill();
@@ -309,11 +340,15 @@ export function renderDroplines(g: CanvasRenderingContext2D, series: IFitSeries,
309
340
  g.lineWidth = renderOptions.ratio!;
310
341
  g.beginPath();
311
342
  g.setLineDash([5, 5]);
343
+ const viewport = renderOptions.viewport;
344
+ const dataBounds = renderOptions.dataBounds;
345
+ const curveFunc = renderOptions.curveFunc;
346
+ const logOptions = renderOptions.logOptions;
312
347
  for (let j = 0; j < series.droplines.length; j++) {
313
348
  const droplineName = series.droplines[j];
314
349
  if (droplineName === 'IC50')
315
- drawDropline(g, {viewport: renderOptions.viewport, xValue: series.parameters![2],
316
- dataBounds: renderOptions.dataBounds, curveFunc: renderOptions.curveFunc, logOptions: renderOptions.logOptions});
350
+ drawDropline(g, {viewport: viewport, xValue: series.parameters![2], dataBounds: dataBounds,
351
+ curveFunc: curveFunc, logOptions: logOptions});
317
352
  }
318
353
  g.stroke();
319
354
  g.restore();
@@ -322,26 +357,38 @@ export function renderDroplines(g: CanvasRenderingContext2D, series: IFitSeries,
322
357
 
323
358
  /** Performs a dropline drawing */
324
359
  function drawDropline(g: CanvasRenderingContext2D, renderOptions: FitDroplineRenderOptions): void {
325
- if (renderOptions.logOptions.logX)
326
- renderOptions.xValue = Math.pow(10, renderOptions.xValue);
327
- const xForY = renderOptions.logOptions.logX ? Math.log10(renderOptions.xValue) : renderOptions.xValue;
328
- const y = renderOptions.logOptions.logY ? Math.pow(10, renderOptions.curveFunc(xForY)) : renderOptions.curveFunc(xForY);
329
- g.moveTo(renderOptions.viewport.xToScreen(renderOptions.dataBounds.minX), renderOptions.viewport.yToScreen(y));
330
- g.lineTo(renderOptions.viewport.xToScreen(renderOptions.xValue), renderOptions.viewport.yToScreen(y));
331
- g.lineTo(renderOptions.viewport.xToScreen(renderOptions.xValue), renderOptions.viewport.yToScreen(renderOptions.dataBounds.minY));
360
+ const logX = renderOptions.logOptions.logX;
361
+ const logY = renderOptions.logOptions.logY;
362
+ const xValue = logX ? Math.pow(10, renderOptions.xValue) : renderOptions.xValue;
363
+ const viewport = renderOptions.viewport;
364
+ const dataBounds = renderOptions.dataBounds;
365
+
366
+ const xForY = logX ? Math.log10(xValue) : xValue;
367
+ const y = logY ? Math.pow(10, renderOptions.curveFunc(xForY)) : renderOptions.curveFunc(xForY);
368
+ const screenX = viewport.xToScreen(xValue);
369
+ const screenY = viewport.yToScreen(y);
370
+ g.moveTo(viewport.xToScreen(dataBounds.minX), screenY);
371
+ g.lineTo(screenX, screenY);
372
+ g.lineTo(screenX, viewport.yToScreen(dataBounds.minY));
332
373
  }
333
374
 
334
375
  export function renderStatistics(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitStatisticsRenderOptions): void {
335
- if ((series.showFitLine ?? true) && renderOptions.statistics) {
336
- const statistics = getSeriesStatistics(series, renderOptions.fitFunc, renderOptions.logOptions);
337
- for (let i = 0; i < renderOptions.statistics.length; i++) {
338
- const statName = renderOptions.statistics[i];
376
+ const screenBounds = renderOptions.screenBounds!;
377
+ const statistics = screenBounds.width < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_WIDTH ||
378
+ screenBounds.height < FitConstants.MIN_POINTS_AND_STATS_VISIBILITY_PX_HEIGHT ? [] : renderOptions.statistics;
379
+ if ((series.showFitLine ?? true) && statistics && statistics.length > 0) {
380
+ const dataBox = renderOptions.dataBox;
381
+ const dataPoints = renderOptions.dataPoints;
382
+ const seriesStatistics = getSeriesStatistics(series, renderOptions.fitFunc, dataPoints, renderOptions.logOptions);
383
+ const color = getSeriesColor(series, renderOptions.seriesIdx!, ColorType.FIT_LINE);
384
+ for (let i = 0; i < statistics.length; i++) {
385
+ const statName = statistics[i];
339
386
  const prop = statisticsProperties.find((p) => p.name === statName);
340
387
  if (prop) {
341
- const s = StringUtils.formatNumber(prop.get(statistics));
342
- g.fillStyle = series.fitLineColor!;
388
+ const s = StringUtils.formatNumber(prop.get(seriesStatistics));
389
+ g.fillStyle = color;
343
390
  g.textAlign = 'left';
344
- g.fillText(prop.name + ': ' + s, renderOptions.dataBox.x + 5, renderOptions.dataBox.y + 20 + 20 * i);
391
+ g.fillText(prop.name + ': ' + s, dataBox.x + 5, dataBox.y + 20 + 20 * i);
345
392
  }
346
393
  }
347
394
  }
@@ -358,21 +405,24 @@ export function renderTitle(g: CanvasRenderingContext2D, renderOptions: FitTitle
358
405
 
359
406
  export function renderAxesLabels(g: CanvasRenderingContext2D, renderOptions: FitAxesLabelsRenderOptions): void {
360
407
  if (renderOptions.showAxesLabels) {
408
+ const screenBounds = renderOptions.screenBounds;
409
+ const dataBox = renderOptions.dataBox;
361
410
  g.font = '11px Roboto, "Roboto Local"';
362
411
  g.textAlign = 'center';
363
412
  g.fillStyle = 'black';
364
- g.fillText(renderOptions.xAxisName!, renderOptions.dataBox.midX - 5,
365
- renderOptions.screenBounds.maxY - FitConstants.X_AXIS_LABEL_BOTTOM_PX_MARGIN);
366
- g.translate(renderOptions.screenBounds.x, renderOptions.screenBounds.y);
413
+ g.fillText(renderOptions.xAxisName!, dataBox.midX - 5, screenBounds.maxY - FitConstants.X_AXIS_LABEL_BOTTOM_PX_MARGIN);
414
+ g.translate(screenBounds.x, screenBounds.y);
367
415
  g.rotate(-Math.PI / 2);
368
416
  const axesTopPxMargin = renderOptions.showTitle ? FitConstants.AXES_TOP_PX_MARGIN_WITH_TITLE : FitConstants.AXES_TOP_PX_MARGIN;
369
- g.fillText(renderOptions.yAxisName!, -(renderOptions.dataBox.height / 2 + axesTopPxMargin + 15), 15);
417
+ g.fillText(renderOptions.yAxisName!, -(dataBox.height / 2 + axesTopPxMargin + 15), 15);
370
418
  g.restore();
371
419
  }
372
420
  }
373
421
 
374
422
  export function renderLegend(g: CanvasRenderingContext2D, data: IFitChartData, renderOptions: FitLegendRenderOptions): void {
375
423
  if (renderOptions.showLegend) {
424
+ const dataBox = renderOptions.dataBox;
425
+ const ratio = renderOptions.ratio;
376
426
  g.font = '11px Roboto, "Roboto Local"';
377
427
  const columnNames = [...new Set(data.series?.map((series) => series.columnName))]
378
428
  .filter((colName) => colName !== null && colName !== undefined);
@@ -380,15 +430,15 @@ export function renderLegend(g: CanvasRenderingContext2D, data: IFitChartData, r
380
430
  for (let i = 0; i < columnNames.length; i++) {
381
431
  const colName = columnNames[i];
382
432
  if (data.chartOptions?.showColumnLabel)
383
- renderColumnLabel(g, colName!, {dataBox: renderOptions.dataBox, columnIdx: i, drawnCurvesInLegend});
433
+ renderColumnLabel(g, colName!, {dataBox: dataBox, columnIdx: i, drawnCurvesInLegend});
384
434
 
385
435
  const series = data.series?.filter((series) => series.columnName === colName);
386
436
  for (let j = 0; j < series?.length!; j++) {
387
437
  const currentSeries = series![j];
388
438
  if (currentSeries.name === '' || currentSeries.name === null || currentSeries.name === undefined)
389
439
  continue;
390
- renderLegendSeries(g, currentSeries, {dataBox: renderOptions.dataBox, ratio: renderOptions.ratio,
391
- columnIdx: i, drawnCurvesInLegend, showColumnLabel: data.chartOptions?.showColumnLabel});
440
+ renderLegendSeries(g, currentSeries, {dataBox: dataBox, ratio: ratio,
441
+ columnIdx: i, drawnCurvesInLegend, showColumnLabel: data.chartOptions?.showColumnLabel, seriesIdx: j});
392
442
  drawnCurvesInLegend++;
393
443
  }
394
444
  }
@@ -397,35 +447,43 @@ export function renderLegend(g: CanvasRenderingContext2D, data: IFitChartData, r
397
447
  }
398
448
 
399
449
  function renderColumnLabel(g: CanvasRenderingContext2D, colName: string, renderOptions: FitLegendColumnlabelSeriesRenderOptions): void {
450
+ const dataBox = renderOptions.dataBox;
400
451
  g.beginPath();
401
452
  g.fillStyle = 'black';
402
453
  const colNameWidth = g.measureText(colName!).width;
403
- g.fillText(colName!, renderOptions.dataBox.maxX - colNameWidth, renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN +
454
+ g.fillText(colName!, dataBox.maxX - colNameWidth, dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN +
404
455
  (renderOptions.columnIdx * FitConstants.LEGEND_RECORD_PX_HEIGHT + FitConstants.LEGEND_RECORD_PX_HEIGHT * renderOptions.drawnCurvesInLegend));
405
456
  }
406
457
 
407
458
  function renderLegendSeries(g: CanvasRenderingContext2D, series: IFitSeries, renderOptions: FitLegendColumnlabelSeriesRenderOptions): void {
459
+ const dataBox = renderOptions.dataBox;
460
+ const columnIdx = renderOptions.columnIdx;
461
+ const ratio = renderOptions.ratio!;
462
+ const showColumnLabel = renderOptions.showColumnLabel;
463
+ const drawnCurvesInLegend = renderOptions.drawnCurvesInLegend;
464
+ const pointColor = getSeriesColor(series, renderOptions.seriesIdx!, ColorType.POINT);
465
+ const fitLineColor = getSeriesColor(series, renderOptions.seriesIdx!, ColorType.FIT_LINE);
408
466
  g.beginPath();
409
- g.strokeStyle = series.fitLineColor!;
410
- g.lineWidth = 2 * renderOptions.ratio!;
467
+ g.strokeStyle = fitLineColor;
468
+ g.lineWidth = 2 * ratio;
411
469
  const textWidth = g.measureText(series.name!).width;
412
- g.moveTo(renderOptions.dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_PX_WIDTH - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN,
413
- renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN - FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN +
414
- (renderOptions.showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT * (renderOptions.columnIdx + 1) : 0) +
415
- FitConstants.LEGEND_RECORD_PX_HEIGHT * renderOptions.drawnCurvesInLegend);
416
- g.lineTo(renderOptions.dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN,
417
- renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN - FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN +
418
- (renderOptions.showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT * (renderOptions.columnIdx + 1) : 0) +
419
- FitConstants.LEGEND_RECORD_PX_HEIGHT * renderOptions.drawnCurvesInLegend);
470
+ g.moveTo(dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_PX_WIDTH - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN,
471
+ dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN - FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN +
472
+ (showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT * (columnIdx + 1) : 0) +
473
+ FitConstants.LEGEND_RECORD_PX_HEIGHT * drawnCurvesInLegend);
474
+ g.lineTo(dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN,
475
+ dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN - FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN +
476
+ (showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT * (columnIdx + 1) : 0) +
477
+ FitConstants.LEGEND_RECORD_PX_HEIGHT * drawnCurvesInLegend);
420
478
  const marker = series.markerType ? series.markerType as DG.MARKER_TYPE : DG.MARKER_TYPE.CIRCLE;
421
- DG.Paint.marker(g, marker, renderOptions.dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN
422
- - FitConstants.LEGEND_RECORD_LINE_PX_WIDTH / 2, renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN -
423
- FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN + (renderOptions.showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT
424
- * (renderOptions.columnIdx + 1) : 0) + FitConstants.LEGEND_RECORD_PX_HEIGHT * renderOptions.drawnCurvesInLegend,
425
- series.pointColor!, FitConstants.POINT_PX_SIZE * renderOptions.ratio!);
426
- g.fillStyle = series.fitLineColor!;
427
- g.fillText(series.name!, renderOptions.dataBox.maxX - textWidth,
428
- renderOptions.dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN + (renderOptions.showColumnLabel ?
429
- FitConstants.LEGEND_RECORD_PX_HEIGHT * (renderOptions.columnIdx + 1) : 0) + FitConstants.LEGEND_RECORD_PX_HEIGHT * renderOptions.drawnCurvesInLegend);
479
+ DG.Paint.marker(g, marker, dataBox.maxX - textWidth - FitConstants.LEGEND_RECORD_LINE_RIGHT_PX_MARGIN
480
+ - FitConstants.LEGEND_RECORD_LINE_PX_WIDTH / 2, dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN -
481
+ FitConstants.LEGEND_RECORD_LINE_BOTTOM_PX_MARGIN + (showColumnLabel ? FitConstants.LEGEND_RECORD_PX_HEIGHT
482
+ * (columnIdx + 1) : 0) + FitConstants.LEGEND_RECORD_PX_HEIGHT * drawnCurvesInLegend,
483
+ pointColor, FitConstants.POINT_PX_SIZE * ratio);
484
+ g.fillStyle = fitLineColor;
485
+ g.fillText(series.name!, dataBox.maxX - textWidth,
486
+ dataBox.y + FitConstants.LEGEND_TOP_PX_MARGIN + (showColumnLabel ?
487
+ FitConstants.LEGEND_RECORD_PX_HEIGHT * (columnIdx + 1) : 0) + FitConstants.LEGEND_RECORD_PX_HEIGHT * drawnCurvesInLegend);
430
488
  g.stroke();
431
489
  }
@@ -1,11 +1,12 @@
1
1
  import * as DG from "datagrok-api/dg";
2
2
 
3
- import {runTests, tests, TestContext} from '@datagrok-libraries/utils/src/test';
3
+ import {runTests, tests, TestContext, initAutoTests as initTests } from '@datagrok-libraries/utils/src/test';
4
4
 
5
5
  import './tests/detector-tests';
6
6
  import './tests/curves-cell-renderer-tests';
7
7
  import './tests/transform-tests';
8
8
  import './tests/fit-tests';
9
+ import './tests/plate-tests';
9
10
 
10
11
  export const _package = new DG.Package();
11
12
  export {tests};
@@ -19,3 +20,8 @@ export async function test(category: string, test: string, testContext: TestCont
19
20
  const data = await runTests({category, test, testContext});
20
21
  return DG.DataFrame.fromObjects(data)!;
21
22
  }
23
+
24
+ //name: initAutoTests
25
+ export async function initAutoTests() {
26
+ await initTests(_package, _package.getModule('package-test.js'));
27
+ }
package/src/package.g.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import {FitChartCellRenderer} from './fit/fit-renderer';
2
2
  import {MultiCurveViewer} from './fit/multi-curve-viewer';
3
+ import {PlateGridCellRenderer} from './plate/plate-cell-renderer';
3
4
 
4
5
  //name: Fit
5
6
  //tags: cellRenderer
@@ -19,3 +20,10 @@ export function _MultiCurveViewer() {
19
20
  return new MultiCurveViewer();
20
21
  }
21
22
 
23
+ //tags: cellRenderer
24
+ //output: grid_cell_renderer renderer
25
+ //meta.cellType: Plate
26
+ export function _PlateGridCellRenderer() {
27
+ return new PlateGridCellRenderer();
28
+ }
29
+