markdown-up 1.5.0 → 1.7.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.
@@ -0,0 +1,632 @@
1
+ // Licensed under the MIT License
2
+ // https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
+
4
+ /** @module lib/lineChart */
5
+
6
+ import {
7
+ categoricalColors, chartCodeBlock, compareValues, formatValue, parameterValue, valueParameter
8
+ } from './util.js';
9
+ import {getCalculatedValueType, loadChartData} from './data.js';
10
+ import {evaluateExpression} from 'calc-script/lib/runtime.js';
11
+ import {parseExpression} from 'calc-script/lib/parser.js';
12
+ import {validateLineChart} from './model.js';
13
+
14
+
15
+ // Line chart defaults
16
+ const defaultWidth = 640;
17
+ const defaultHeight = 320;
18
+ const defaultXAxisTickCount = 3;
19
+ const defaultYAxisTickCount = 3;
20
+ const defaultFontSize = 12;
21
+
22
+
23
+ // Line chart constants (all numbers in pixels)
24
+ const pixelsPerPoint = 4 / 3;
25
+ const svgPrecision = 3;
26
+ const chartFontFamily = 'Arial, Helvetica, sans-serif';
27
+ const chartFontWidthRatio = 0.6;
28
+ const chartBackgroundColor = 'white';
29
+ const axisColor = 'black';
30
+ const axisTickLineColor = 'lightgray';
31
+ const axisLineWidth = 1;
32
+ const axisTickWidth = 1;
33
+ const axisTickLength = 5;
34
+ const annotationBackgroundColor = '#ffffffa0';
35
+ const annotationTextColor = 'black';
36
+ const annotationLineColor = 'black';
37
+ const annotationLineWidth = 2;
38
+ const chartLineWidth = 3;
39
+
40
+
41
+ /**
42
+ * Line chart code block function
43
+ *
44
+ * @param {string} language - The code block language
45
+ * @param {string[]} lines - The code block's text lines
46
+ * @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
47
+ * @returns {Object} The line chart element model
48
+ */
49
+ export function lineChartCodeBlock(language, lines, options = null) {
50
+ return chartCodeBlock(language, lines, options, validateLineChart, lineChartElements);
51
+ }
52
+
53
+
54
+ /**
55
+ * Render a line chart
56
+ *
57
+ * @param {Object} lineChart - The line chart model
58
+ * @param {module:lib/util~MarkdownScriptOptions} [options=null] - Markdown script options
59
+ * @returns {Object} The line chart element model
60
+ */
61
+ export async function lineChartElements(lineChart, options = null) {
62
+ const chartFontSize = (options !== null && 'fontSize' in options ? options.fontSize : defaultFontSize) * pixelsPerPoint;
63
+
64
+ // Load the chart data
65
+ const {data, types, variables} = (await loadChartData(lineChart, options));
66
+
67
+ // Validate X and Y field types
68
+ const {'x': xField, 'y': yFields, 'color': colorField = null} = lineChart;
69
+ for (const [fieldDesc, fields] of [['X-field', [xField]], ['Y-field', yFields]]) {
70
+ for (const field of fields) {
71
+ if (!(field in types)) {
72
+ throw new Error(`Unknown ${fieldDesc} "${field}"`);
73
+ }
74
+ if (types[field] === 'string') {
75
+ throw new Error(`Invalid type "${types[field]}" for ${fieldDesc} "${field}"`);
76
+ }
77
+ }
78
+ }
79
+ const xFieldType = types[xField];
80
+ const yFieldType = types[yFields[0]];
81
+ for (const field of yFields) {
82
+ if (types[field] !== yFieldType) {
83
+ throw new Error(`Invalid type "${types[field]}" for Y-field "${field}"`);
84
+ }
85
+ }
86
+
87
+ // Sort the rows by the X field
88
+ data.sort((row1, row2) => {
89
+ const x1 = xField in row1 ? row1[xField] : null;
90
+ const x2 = xField in row2 ? row2[xField] : null;
91
+ return compareValues(x1, x2);
92
+ });
93
+
94
+ // Generate the line points - [(label, color, points), ...]
95
+ const linePoints = [];
96
+ let xMin = null;
97
+ let yMin = null;
98
+ let xMax = null;
99
+ let yMax = null;
100
+ if (colorField !== null) {
101
+ // Determine the set of color encoding values
102
+ const colorValueSet = new Set();
103
+ const pointsMap = {};
104
+ for (const row of data) {
105
+ for (const yField of yFields) {
106
+ const colorValue = formatValue(colorField in row ? row[colorField] : null, lineChart);
107
+ const rowKey = (yFields.length === 1 ? colorValue : `${yField}, ${colorValue}`);
108
+ colorValueSet.add(rowKey);
109
+ const xRow = xField in row ? row[xField] : null;
110
+ const yRow = yField in row ? row[yField] : null;
111
+ if (xRow !== null && yRow !== null) {
112
+ if (!(rowKey in pointsMap)) {
113
+ pointsMap[rowKey] = [];
114
+ }
115
+ pointsMap[rowKey].push([xRow, yRow]);
116
+ xMin = xMin === null ? xRow : xRow < xMin ? xRow : xMin;
117
+ yMin = yMin === null ? yRow : yRow < yMin ? yRow : yMin;
118
+ xMax = xMax === null ? xRow : xRow > xMax ? xRow : xMax;
119
+ yMax = yMax === null ? yRow : yRow > yMax ? yRow : yMax;
120
+ }
121
+ }
122
+ }
123
+
124
+ // Add the points
125
+ const colorValues = Array.from(colorValueSet.values()).sort();
126
+ const colorValueCount = colorValues.length;
127
+ for (let ixColorValue = 0; ixColorValue < colorValueCount; ixColorValue += 1) {
128
+ const colorValue = colorValues[ixColorValue];
129
+ const color = categoricalColors[ixColorValue % categoricalColors.length];
130
+ const points = pointsMap[colorValue];
131
+ linePoints.push({'label': colorValue, color, points});
132
+ }
133
+ } else {
134
+ // Create a line for each y-field
135
+ const fieldCount = yFields.length;
136
+ for (let ixField = 0; ixField < fieldCount; ixField += 1) {
137
+ const yField = yFields[ixField];
138
+ const color = categoricalColors[ixField % categoricalColors.length];
139
+ const points = [];
140
+ linePoints.push({'label': yField, color, points});
141
+
142
+ // Add the points
143
+ for (const row of data) {
144
+ if (xField in row && yField in row) {
145
+ const xRow = xField in row ? row[xField] : null;
146
+ const yRow = yField in row ? row[yField] : null;
147
+ if (xRow !== null && yRow !== null) {
148
+ points.push([xRow, yRow]);
149
+ xMin = xMin === null ? xRow : xRow < xMin ? xRow : xMin;
150
+ yMin = yMin === null ? yRow : yRow < yMin ? yRow : yMin;
151
+ xMax = xMax === null ? xRow : xRow > xMax ? xRow : xMax;
152
+ yMax = yMax === null ? yRow : yRow > yMax ? yRow : yMax;
153
+ }
154
+ }
155
+ }
156
+ }
157
+ }
158
+
159
+ // No data?
160
+ if (xMin === null) {
161
+ throw new Error('No data');
162
+ }
163
+
164
+ // Calculated expression helper function
165
+ const computeExpr = (exprText, expectedType = null, desc = null) => {
166
+ const exprValue = evaluateExpression(parseExpression(exprText), variables);
167
+ if (exprValue !== null && expectedType !== null) {
168
+ const exprType = getCalculatedValueType(exprValue);
169
+ if (exprType !== expectedType) {
170
+ throw new Error(`Invalid ${desc} ${JSON.stringify(exprValue)} (type "${exprType}"), expected type "${expectedType}"`);
171
+ }
172
+ }
173
+ return exprValue;
174
+ };
175
+
176
+ // Compute the chart title, width, and height
177
+ const chartTitle = 'title' in lineChart ? computeExpr(lineChart.title, 'string', 'chart title') : null;
178
+ const chartWidth = 'width' in lineChart ? computeExpr(lineChart.width, 'number', 'chart width') : defaultWidth;
179
+ const chartHeight = 'height' in lineChart ? computeExpr(lineChart.height, 'number', 'chart height') : defaultHeight;
180
+
181
+ // Compute Y-axis tick values
182
+ const yAxisTicks = [];
183
+ const yTickCount = 'ytick' in lineChart && 'count' in lineChart.ytick
184
+ ? computeExpr(lineChart.ytick.count, 'number', 'Y-axis tick count')
185
+ : defaultYAxisTickCount;
186
+ const yTickSkip = 'ytick' in lineChart && 'skip' in lineChart.ytick ? lineChart.ytick.skip + 1 : 1;
187
+ const yTickStart = 'ytick' in lineChart && 'start' in lineChart.ytick
188
+ ? computeExpr(lineChart.ytick.start, yFieldType, 'Y-axis start value')
189
+ : yMin;
190
+ const yTickEnd = 'ytick' in lineChart && 'end' in lineChart.ytick
191
+ ? computeExpr(lineChart.ytick.end, yFieldType, 'Y-axis end value')
192
+ : yMax;
193
+ for (let ixTick = 0; ixTick < yTickCount; ixTick++) {
194
+ const yTickParam = yTickCount === 1 ? 0 : ixTick / (yTickCount - 1);
195
+ const yTickValue = parameterValue(yTickParam, yTickStart, yTickEnd);
196
+ yAxisTicks.push([yTickValue, (ixTick % yTickSkip) !== 0 ? '' : formatValue(yTickValue, lineChart)]);
197
+ yMin = yTickValue < yMin ? yTickValue : yMin;
198
+ yMax = yTickValue > yMax ? yTickValue : yMax;
199
+ }
200
+
201
+ // Compute X-axis tick values
202
+ const xAxisTicks = [];
203
+ const xTickCount = 'xtick' in lineChart && 'count' in lineChart.xtick
204
+ ? computeExpr(lineChart.xtick.count, 'number', 'X-axis tick count')
205
+ : defaultXAxisTickCount;
206
+ const xTickSkip = 'xtick' in lineChart && 'skip' in lineChart.xtick ? lineChart.xtick.skip + 1 : 1;
207
+ const xTickStart = 'xtick' in lineChart && 'start' in lineChart.xtick
208
+ ? computeExpr(lineChart.xtick.start, xFieldType, 'X-axis start value')
209
+ : xMin;
210
+ const xTickEnd = 'xtick' in lineChart && 'end' in lineChart.xtick
211
+ ? computeExpr(lineChart.xtick.end, xFieldType, 'X-axis end value')
212
+ : xMax;
213
+ for (let ixTick = 0; ixTick < xTickCount; ixTick++) {
214
+ const xTickParam = xTickCount === 1 ? 0 : ixTick / (xTickCount - 1);
215
+ const xTickValue = parameterValue(xTickParam, xTickStart, xTickEnd);
216
+ xAxisTicks.push([xTickValue, (ixTick % xTickSkip) !== 0 ? '' : formatValue(xTickValue, lineChart)]);
217
+ xMin = xTickValue < xMin ? xTickValue : xMin;
218
+ xMax = xTickValue > xMax ? xTickValue : xMax;
219
+ }
220
+
221
+ // Compute Y-axis annotations
222
+ const yAxisAnnotations = [];
223
+ if ('yline' in lineChart) {
224
+ for (const annotation of lineChart.yline) {
225
+ const yAnnotationValue = computeExpr(annotation.value, yFieldType, 'Y-axis annotation value');
226
+ yAxisAnnotations.push([
227
+ yAnnotationValue,
228
+ 'label' in annotation
229
+ ? formatValue(computeExpr(annotation.label), lineChart)
230
+ : formatValue(yAnnotationValue, lineChart)
231
+ ]);
232
+ yMin = yAnnotationValue < yMin ? yAnnotationValue : yMin;
233
+ yMax = yAnnotationValue > yMax ? yAnnotationValue : yMax;
234
+ }
235
+ }
236
+
237
+ // Compute X-axis annotations
238
+ const xAxisAnnotations = [];
239
+ if ('xline' in lineChart) {
240
+ for (const annotation of lineChart.xline) {
241
+ const xAnnotationValue = computeExpr(annotation.value, xFieldType, 'X-axis annotation value');
242
+ xAxisAnnotations.push([
243
+ xAnnotationValue,
244
+ 'label' in annotation
245
+ ? formatValue(computeExpr(annotation.label), lineChart)
246
+ : formatValue(xAnnotationValue, lineChart)
247
+ ]);
248
+ xMin = xAnnotationValue < xMin ? xAnnotationValue : xMin;
249
+ xMax = xAnnotationValue > xMax ? xAnnotationValue : xMax;
250
+ }
251
+ }
252
+
253
+ // Chart title calculations
254
+ const chartBorderSize = chartFontSize;
255
+ const chartTitleFontSize = 1.1 * chartFontSize;
256
+ const chartTitleHeight = chartTitle !== null ? 1.5 * chartTitleFontSize : 0;
257
+
258
+ // Y-axis calculations
259
+ const axisTitleFontSize = 1 * chartFontSize;
260
+ const axisLabelFontSize = chartFontSize;
261
+ const yAxisTitle = yFields.length === 1 ? yFields[0] : null;
262
+ const yAxisTitleWidth = yAxisTitle !== null ? 1.8 * axisTitleFontSize : 0;
263
+ const yAxisLabelWidth = yAxisTicks.reduce((labelMax, [, label]) => {
264
+ const labelWidth = label.length * chartFontWidthRatio * axisLabelFontSize;
265
+ return labelWidth > labelMax ? labelWidth : labelMax;
266
+ }, 0);
267
+ const yAxisTickGap = 0.75 * axisTickLength;
268
+ const yAxisX = Math.min(
269
+ chartBorderSize + yAxisTitleWidth + (yAxisTicks.length === 0 ? 0 : yAxisLabelWidth + yAxisTickGap + axisTickLength),
270
+ 0.4 * chartWidth
271
+ );
272
+
273
+ // X-axis calculations
274
+ const xAxisTitleHeight = 1.8 * axisTitleFontSize;
275
+ const xAxisTickGap = 0.75 * axisTickLength;
276
+ const xAxisY = chartHeight - chartBorderSize - xAxisTitleHeight -
277
+ (xAxisTicks.length === 0 ? 0 : axisLabelFontSize - xAxisTickGap + axisTickLength);
278
+
279
+ // Y-axis annotation calculations
280
+ const annotationLabelFontSize = axisLabelFontSize;
281
+ const annotationLabelMargin = 0.25 * annotationLabelFontSize;
282
+ const annotationLabelHeight = annotationLabelFontSize + 2 * annotationLabelMargin;
283
+ const yAnnotationLabelOffsetX = 0.2 * annotationLabelFontSize;
284
+ const yAnnotationLabelOffsetY = 0.1 * annotationLabelFontSize;
285
+
286
+ // Y-axis annotation calculations
287
+ const xAnnotationLabelOffsetX = 0.2 * annotationLabelFontSize;
288
+ const xAnnotationLabelOffsetY = 0.1 * annotationLabelFontSize;
289
+
290
+ // Color legend calculations
291
+ const colorLegendFontSize = chartFontSize;
292
+ const colorLegendGap = 0.5 * colorLegendFontSize;
293
+ const colorLegendLabelHeight = colorLegendFontSize;
294
+ const colorLegendLabelGap = 0.35 * colorLegendLabelHeight;
295
+ const colorLegendSampleWidth = 1.35 * colorLegendLabelHeight;
296
+ const colorLegendLabelWidth = linePoints.reduce((labelMax, {label}) => {
297
+ const labelWidth = label.length * chartFontWidthRatio * colorLegendFontSize;
298
+ return labelWidth > labelMax ? labelWidth : labelMax;
299
+ }, 0);
300
+ const colorLegendX = yFields.length === 1 && !('color' in lineChart) ? null : Math.max(
301
+ chartWidth - chartBorderSize - colorLegendLabelWidth - colorLegendSampleWidth,
302
+ 0.6 * chartWidth
303
+ );
304
+
305
+ // Chart area calculations
306
+ const chartTop = chartBorderSize + chartTitleHeight + 0.5 * chartLineWidth;
307
+ const chartLeft = yAxisX + 0.5 * axisLineWidth + 0.5 * chartLineWidth;
308
+ const chartBottom = xAxisY - 0.5 * axisLineWidth - 0.5 * chartLineWidth;
309
+ const chartRight = colorLegendX !== null ? colorLegendX - colorLegendGap : chartWidth - chartBorderSize;
310
+
311
+ // Axis label limits
312
+ const xAxisLabelLeft = chartLeft + 0.5 * axisLabelFontSize;
313
+ const xAxisLabelRight = chartRight - 0.5 * axisLabelFontSize;
314
+ const yAxisLabelTop = chartTop + 0.5 * axisLabelFontSize;
315
+ const yAxisLabelBottom = chartBottom - 0.5 * axisLabelFontSize;
316
+
317
+ // Helper functions to compute chart coordindate points
318
+ const chartPointX = (xCoord) => parameterValue(valueParameter(xCoord, xMin, xMax), chartLeft, chartRight);
319
+ const chartPointY = (yCoord) => parameterValue(valueParameter(yCoord, yMin, yMax), chartBottom, chartTop);
320
+ const svgValue = (value) => value.toFixed(svgPrecision);
321
+ const chartPathPoint = ([xCoord, yCoord], ixPoint, points) => {
322
+ const xPoint = chartPointX(xCoord);
323
+ const yPoint = chartPointY(yCoord);
324
+ if (points.length === 1) {
325
+ return `M ${svgValue(xPoint - 0.5 * chartLineWidth)} ${svgValue(yPoint)} ` +
326
+ `L ${svgValue(xPoint + 0.5 * chartLineWidth)} ${svgValue(yPoint)}`;
327
+ }
328
+ return `${ixPoint === 0 ? 'M' : 'L'} ${svgValue(xPoint)} ${svgValue(yPoint)}`;
329
+ };
330
+
331
+ // Render the chart
332
+ return {
333
+ 'svg': 'svg',
334
+ 'attr': {
335
+ 'width': chartWidth,
336
+ 'height': chartHeight
337
+ },
338
+ 'elem': [
339
+ // Background
340
+ {
341
+ 'svg': 'rect',
342
+ 'attr': {
343
+ 'width': chartWidth,
344
+ 'height': chartHeight,
345
+ 'fill': chartBackgroundColor
346
+ }
347
+ },
348
+
349
+ // Chart title
350
+ chartTitle === null ? null : {
351
+ 'svg': 'text',
352
+ 'attr': {
353
+ 'font-family': chartFontFamily,
354
+ 'font-size': `${svgValue(chartTitleFontSize)}px`,
355
+ 'fill': axisColor,
356
+ 'style': 'font-weight: bold',
357
+ 'x': svgValue(0.5 * (chartLeft + chartRight)),
358
+ 'y': svgValue(chartBorderSize),
359
+ 'text-anchor': 'middle',
360
+ 'dominant-baseline': 'hanging'
361
+ },
362
+ 'elem': {'text': chartTitle}
363
+ },
364
+
365
+ // Y-Axis title
366
+ yAxisTitle === null ? null : {
367
+ 'svg': 'text',
368
+ 'attr': {
369
+ 'font-family': chartFontFamily,
370
+ 'font-size': `${svgValue(axisTitleFontSize)}px`,
371
+ 'fill': axisColor,
372
+ 'style': 'font-weight: bold',
373
+ 'x': svgValue(chartBorderSize),
374
+ 'y': svgValue(0.5 * (chartTop + chartBottom)),
375
+ 'transform': `rotate(-90 ${svgValue(chartBorderSize)}, ${svgValue(0.5 * (chartTop + chartBottom))})`,
376
+ 'text-anchor': 'middle',
377
+ 'dominant-baseline': 'hanging'
378
+ },
379
+ 'elem': {'text': yAxisTitle}
380
+ },
381
+
382
+ // Y-axis ticks
383
+ yAxisTicks.map(([yCoord, yLabel]) => {
384
+ const yPoint = chartPointY(yCoord);
385
+ const hasLabel = yLabel !== '';
386
+ const hasLine = !hasLabel || (yPoint > yAxisLabelTop && yPoint < yAxisLabelBottom);
387
+ return [
388
+ !hasLabel ? null : {
389
+ 'svg': 'path',
390
+ 'attr': {
391
+ 'stroke': axisColor,
392
+ 'stroke-width': svgValue(axisTickWidth),
393
+ 'fill': 'none',
394
+ 'd': `M ${svgValue(yAxisX)} ${svgValue(yPoint)} H ${svgValue(yAxisX - axisTickLength)}`
395
+ }
396
+ },
397
+ !hasLine ? null : {
398
+ 'svg': 'path',
399
+ 'attr': {
400
+ 'stroke': axisTickLineColor,
401
+ 'stroke-width': svgValue(axisTickWidth),
402
+ 'fill': 'none',
403
+ 'd': `M ${svgValue(yAxisX)} ${svgValue(yPoint)} H ${svgValue(chartRight)}`
404
+ }
405
+ }
406
+ ];
407
+ }),
408
+
409
+ // Y-axis labels
410
+ yAxisTicks.map(([yCoord, yLabel]) => {
411
+ const yPoint = chartPointY(yCoord);
412
+ const hasLabel = yLabel !== '';
413
+ return !hasLabel ? null : {
414
+ 'svg': 'text',
415
+ 'attr': {
416
+ 'font-family': chartFontFamily,
417
+ 'font-size': `${svgValue(axisLabelFontSize)}px`,
418
+ 'fill': axisColor,
419
+ 'x': svgValue(yAxisX - axisTickLength - yAxisTickGap),
420
+ 'y': svgValue(yPoint),
421
+ 'text-anchor': 'end',
422
+ 'dominant-baseline': yPoint > yAxisLabelBottom ? 'auto' : (yPoint < yAxisLabelTop ? 'hanging' : 'middle')
423
+ },
424
+ 'elem': {'text': yLabel}
425
+ };
426
+ }),
427
+
428
+ // X-Axis title
429
+ {
430
+ 'svg': 'text',
431
+ 'attr': {
432
+ 'font-family': chartFontFamily,
433
+ 'font-size': `${svgValue(axisTitleFontSize)}px`,
434
+ 'fill': axisColor,
435
+ 'style': 'font-weight: bold',
436
+ 'x': svgValue(0.5 * (chartLeft + chartRight)),
437
+ 'y': svgValue(chartHeight - chartBorderSize),
438
+ 'text-anchor': 'middle',
439
+ 'dominant-baseline': 'auto'
440
+ },
441
+ 'elem': {'text': xField}
442
+ },
443
+
444
+ // X-axis ticks
445
+ xAxisTicks.map(([xCoord, xLabel]) => {
446
+ const xPoint = chartPointX(xCoord);
447
+ const hasLabel = xLabel !== '';
448
+ const hasLine = !hasLabel || (xPoint > xAxisLabelLeft && xPoint < xAxisLabelRight);
449
+ return [
450
+ !hasLabel ? null : {
451
+ 'svg': 'path',
452
+ 'attr': {
453
+ 'stroke': axisColor,
454
+ 'stroke-width': svgValue(axisTickWidth),
455
+ 'fill': 'none',
456
+ 'd': `M ${svgValue(xPoint)} ${svgValue(xAxisY)} V ${svgValue(xAxisY + axisTickLength)}`
457
+ }
458
+ },
459
+ !hasLine ? null : {
460
+ 'svg': 'path',
461
+ 'attr': {
462
+ 'stroke': axisTickLineColor,
463
+ 'stroke-width': svgValue(axisTickWidth),
464
+ 'fill': 'none',
465
+ 'd': `M ${svgValue(xPoint)} ${svgValue(xAxisY)} V ${svgValue(chartTop)}`
466
+ }
467
+ }
468
+ ];
469
+ }),
470
+
471
+ // X-axis labels
472
+ xAxisTicks.map(([xCoord, xLabel]) => {
473
+ const xPoint = chartPointX(xCoord);
474
+ const hasLabel = xLabel !== '';
475
+ return !hasLabel ? null : {
476
+ 'svg': 'text',
477
+ 'attr': {
478
+ 'font-family': chartFontFamily,
479
+ 'font-size': `${svgValue(axisLabelFontSize)}px`,
480
+ 'fill': axisColor,
481
+ 'x': svgValue(xPoint),
482
+ 'y': svgValue(xAxisY + axisTickLength + xAxisTickGap),
483
+ 'text-anchor': xPoint < xAxisLabelLeft ? 'start' : (xPoint > xAxisLabelRight ? 'end' : 'middle'),
484
+ 'dominant-baseline': 'hanging'
485
+ },
486
+ 'elem': {'text': xLabel}
487
+ };
488
+ }),
489
+
490
+ // Axis lines
491
+ {
492
+ 'svg': 'path',
493
+ 'attr': {
494
+ 'stroke': axisColor,
495
+ 'stroke-width': svgValue(axisLineWidth),
496
+ 'fill': 'none',
497
+ 'd': `M ${svgValue(yAxisX)} ${svgValue(chartTop - 0.5 * axisTickWidth)} ` +
498
+ `V ${svgValue(xAxisY)} H ${svgValue(chartRight + 0.5 * axisTickWidth)}`
499
+ }
500
+ },
501
+
502
+ // Lines
503
+ linePoints.map(({color, points}) => ({
504
+ 'svg': 'path',
505
+ 'attr': {
506
+ 'stroke': color,
507
+ 'stroke-width': svgValue(chartLineWidth),
508
+ 'fill': 'none',
509
+ 'd': points.map(chartPathPoint).join(' ')
510
+ }
511
+ })),
512
+
513
+ // Y-axis annotations
514
+ yAxisAnnotations.map(([yCoord, yLabel]) => {
515
+ const yPoint = chartPointY(yCoord);
516
+ const isUnder = yPoint < 0.5 * (chartTop + chartBottom);
517
+ const labelWidth = 2 * annotationLabelMargin + yLabel.length * chartFontWidthRatio * annotationLabelFontSize;
518
+ const labelY = isUnder
519
+ ? yPoint + annotationLineWidth + yAnnotationLabelOffsetY
520
+ : yPoint - annotationLineWidth - yAnnotationLabelOffsetY - annotationLabelHeight;
521
+ return [
522
+ yLabel === '' ? null : {
523
+ 'svg': 'rect',
524
+ 'attr': {
525
+ 'x': svgValue(chartLeft + yAnnotationLabelOffsetX),
526
+ 'y': svgValue(labelY),
527
+ 'width': svgValue(labelWidth),
528
+ 'height': svgValue(annotationLabelHeight),
529
+ 'fill': annotationBackgroundColor
530
+ }
531
+ },
532
+ yLabel === '' ? null : {
533
+ 'svg': 'text',
534
+ 'attr': {
535
+ 'font-family': chartFontFamily,
536
+ 'font-size': `${svgValue(annotationLabelFontSize)}px`,
537
+ 'fill': annotationTextColor,
538
+ 'x': svgValue(chartLeft + yAnnotationLabelOffsetX + annotationLabelMargin),
539
+ 'y': svgValue(labelY + annotationLabelMargin + 0.5 * annotationLabelFontSize),
540
+ 'text-anchor': 'start',
541
+ 'dominant-baseline': 'middle'
542
+ },
543
+ 'elem': {'text': yLabel}
544
+ },
545
+ {
546
+ 'svg': 'path',
547
+ 'attr': {
548
+ 'stroke': annotationLineColor,
549
+ 'stroke-width': svgValue(annotationLineWidth),
550
+ 'fill': 'none',
551
+ 'd': `M ${svgValue(yAxisX)} ${svgValue(yPoint)} H ${svgValue(chartRight)}`
552
+ }
553
+ }
554
+ ];
555
+ }),
556
+
557
+ // X-axis annotations
558
+ xAxisAnnotations.map(([xCoord, xLabel]) => {
559
+ const xPoint = chartPointX(xCoord);
560
+ const isLeft = xPoint > 0.5 * (chartLeft + chartRight);
561
+ const labelWidth = 2 * annotationLabelMargin + xLabel.length * chartFontWidthRatio * annotationLabelFontSize;
562
+ const labelY = chartBottom - xAnnotationLabelOffsetY - annotationLabelHeight;
563
+ return [
564
+ xLabel === '' ? null : {
565
+ 'svg': 'rect',
566
+ 'attr': {
567
+ 'x': svgValue(isLeft
568
+ ? xPoint - 0.5 * annotationLineWidth - xAnnotationLabelOffsetX - labelWidth
569
+ : xPoint + 0.5 * annotationLineWidth + xAnnotationLabelOffsetX),
570
+ 'y': svgValue(labelY),
571
+ 'width': svgValue(labelWidth),
572
+ 'height': svgValue(annotationLabelHeight),
573
+ 'fill': annotationBackgroundColor
574
+ }
575
+ },
576
+ xLabel === '' ? null : {
577
+ 'svg': 'text',
578
+ 'attr': {
579
+ 'font-family': chartFontFamily,
580
+ 'font-size': `${svgValue(annotationLabelFontSize)}px`,
581
+ 'fill': annotationTextColor,
582
+ 'x': svgValue(isLeft
583
+ ? xPoint - 0.5 * annotationLineWidth - xAnnotationLabelOffsetX - annotationLabelMargin
584
+ : xPoint + 0.5 * annotationLineWidth + xAnnotationLabelOffsetX + annotationLabelMargin),
585
+ 'y': svgValue(labelY + annotationLabelMargin + 0.5 * annotationLabelFontSize),
586
+ 'text-anchor': isLeft ? 'end' : 'start',
587
+ 'dominant-baseline': 'middle'
588
+ },
589
+ 'elem': {'text': xLabel}
590
+ },
591
+ {
592
+ 'svg': 'path',
593
+ 'attr': {
594
+ 'stroke': annotationLineColor,
595
+ 'stroke-width': svgValue(annotationLineWidth),
596
+ 'fill': 'none',
597
+ 'd': `M ${svgValue(xPoint)} ${svgValue(xAxisY)} V ${svgValue(chartTop)}`
598
+ }
599
+ }
600
+ ];
601
+ }),
602
+
603
+ // Color legend
604
+ colorLegendX === null ? null : linePoints.map(({label, color}, ix) => [
605
+ {
606
+ 'svg': 'rect',
607
+ 'attr': {
608
+ 'x': svgValue(colorLegendX),
609
+ 'y': svgValue(chartTop + ix * (colorLegendLabelHeight + colorLegendLabelGap)),
610
+ 'width': svgValue(colorLegendLabelHeight),
611
+ 'height': svgValue(colorLegendLabelHeight),
612
+ 'stroke': 'none',
613
+ 'fill': color
614
+ }
615
+ },
616
+ {
617
+ 'svg': 'text',
618
+ 'attr': {
619
+ 'font-family': chartFontFamily,
620
+ 'font-size': `${svgValue(colorLegendFontSize)}px`,
621
+ 'fill': axisColor,
622
+ 'x': svgValue(colorLegendX + colorLegendSampleWidth),
623
+ 'y': svgValue(chartTop + ix * (colorLegendLabelHeight + colorLegendLabelGap) + 0.5 * colorLegendLabelHeight),
624
+ 'text-anchor': 'start',
625
+ 'dominant-baseline': 'middle'
626
+ },
627
+ 'elem': {'text': label}
628
+ }
629
+ ])
630
+ ]
631
+ };
632
+ }