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