markdown-up 1.7.5 → 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.
- package/README.md +62 -7
- package/lib/app.js +4 -35
- package/lib/data.js +347 -340
- package/lib/dataTable.js +93 -41
- package/lib/lineChart.js +192 -129
- package/lib/script.js +39 -329
- package/lib/scriptLibrary.js +377 -62
- package/package.json +4 -4
- package/lib/barChart.js +0 -36
- package/lib/csv.js +0 -68
- package/lib/model.js +0 -292
- package/lib/util.js +0 -176
package/lib/dataTable.js
CHANGED
|
@@ -3,49 +3,104 @@
|
|
|
3
3
|
|
|
4
4
|
/** @module lib/dataTable */
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import {loadChartData} from './data.js';
|
|
6
|
+
import {compareValues, formatValue} from './data.js';
|
|
8
7
|
import {markdownElements} from 'markdown-model/lib/elements.js';
|
|
9
8
|
import {parseMarkdown} from 'markdown-model/lib/parser.js';
|
|
10
|
-
import {
|
|
9
|
+
import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
|
|
10
|
+
import {validateType} from 'schema-markdown/lib/schema.js';
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
// The data table model's Schema Markdown
|
|
14
|
+
export const dataTableTypes = parseSchemaMarkdown(`\
|
|
15
|
+
group "Data Table"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# A data table model
|
|
19
|
+
struct DataTable
|
|
20
|
+
|
|
21
|
+
# The table's fields
|
|
22
|
+
optional string[len > 0] fields
|
|
23
|
+
|
|
24
|
+
# The table's category fields
|
|
25
|
+
optional string[len > 0] categories
|
|
26
|
+
|
|
27
|
+
# The "categories" and "fields" to be rendered as Markdown text
|
|
28
|
+
optional string[len > 0] markdown
|
|
29
|
+
|
|
30
|
+
# The numeric formatting precision (default is 2)
|
|
31
|
+
optional int(>= 0) precision
|
|
32
|
+
|
|
33
|
+
# The datetime format
|
|
34
|
+
optional DataTableDatetimeFormat datetime
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# A datetime format
|
|
38
|
+
enum DataTableDatetimeFormat
|
|
39
|
+
|
|
40
|
+
# ISO datetime year format
|
|
41
|
+
year
|
|
42
|
+
|
|
43
|
+
# ISO datetime month format
|
|
44
|
+
month
|
|
45
|
+
|
|
46
|
+
# ISO datetime day format
|
|
47
|
+
day
|
|
48
|
+
`);
|
|
14
49
|
|
|
15
50
|
|
|
16
51
|
/**
|
|
17
|
-
*
|
|
52
|
+
* Validate a data table model
|
|
18
53
|
*
|
|
19
|
-
* @param {
|
|
20
|
-
*
|
|
21
|
-
* @returns {Object}
|
|
54
|
+
* @param {Object} dataTable - The
|
|
55
|
+
* [data table model]{@link https://craigahobbs.github.io/markdown-up/library/model.html#var.vName='DataTable'}
|
|
56
|
+
* @returns {Object}
|
|
57
|
+
* @throws [ValidationError]{@link https://craigahobbs.github.io/schema-markdown-js/module-lib_schema.ValidationError.html}
|
|
22
58
|
*/
|
|
23
|
-
export function
|
|
24
|
-
return
|
|
59
|
+
export function validateDataTable(dataTable) {
|
|
60
|
+
return validateType(dataTableTypes, 'DataTable', dataTable);
|
|
25
61
|
}
|
|
26
62
|
|
|
27
63
|
|
|
28
64
|
/**
|
|
29
|
-
*
|
|
65
|
+
* The data table options object
|
|
30
66
|
*
|
|
31
|
-
* @
|
|
32
|
-
* @
|
|
33
|
-
*
|
|
67
|
+
* @typedef {Object} DataTableOptions
|
|
68
|
+
* @property {function} [urlFn] - The
|
|
69
|
+
* [Markdown URL modifier function]{@link https://craigahobbs.github.io/markdown-model/module-lib_elements.html#~URLFn}
|
|
34
70
|
*/
|
|
35
|
-
export async function dataTableElements(dataTable, options = null) {
|
|
36
|
-
const {data, types} = await loadChartData(dataTable, options);
|
|
37
71
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Render a data table
|
|
75
|
+
*
|
|
76
|
+
* @param {Object[]} data - The data array
|
|
77
|
+
* @param {?Object} [dataTable = null] - The
|
|
78
|
+
* [data table model]{@link https://craigahobbs.github.io/markdown-up/library/model.html#var.vName='DataTable'}
|
|
79
|
+
* @param {?Object} [options = null] - The [data table options]{@link module:lib/dataTable~DataTableOptions}
|
|
80
|
+
* @returns {Object} The data table [element model]{@link https://github.com/craigahobbs/element-model#readme}
|
|
81
|
+
*/
|
|
82
|
+
export function dataTableElements(data, dataTable = null, options = null) {
|
|
83
|
+
// Compute the table's field names
|
|
84
|
+
const categories = (dataTable !== null ? (dataTable.categories ?? []) : []);
|
|
85
|
+
let tableFields = (dataTable !== null ? (dataTable.fields ?? null) : null);
|
|
86
|
+
if (tableFields === null) {
|
|
87
|
+
const allFields = new Set();
|
|
88
|
+
for (const row of data) {
|
|
89
|
+
for (const fieldName of Object.keys(row)) {
|
|
90
|
+
allFields.add(fieldName);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
tableFields = Array.from(allFields.values());
|
|
94
|
+
if (categories !== null) {
|
|
95
|
+
tableFields = tableFields.filter((key) => categories.indexOf(key) === -1);
|
|
96
|
+
}
|
|
42
97
|
}
|
|
43
98
|
|
|
44
99
|
// Generate the data table's element model
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
100
|
+
const markdown = (dataTable !== null ? (dataTable.markdown ?? null) : null);
|
|
101
|
+
const markdownOptions = (markdown !== null && options !== null && 'urlFn' in options ? {'urlFn': options.urlFn} : null);
|
|
102
|
+
const formatPrecision = (dataTable !== null ? (dataTable.precision ?? null) : null);
|
|
103
|
+
const formatDatetime = (dataTable !== null ? (dataTable.datetime ?? null) : null);
|
|
49
104
|
return {
|
|
50
105
|
'html': 'table',
|
|
51
106
|
'elem': [
|
|
@@ -53,8 +108,8 @@ export async function dataTableElements(dataTable, options = null) {
|
|
|
53
108
|
{
|
|
54
109
|
'html': 'tr',
|
|
55
110
|
'elem': [
|
|
56
|
-
|
|
57
|
-
|
|
111
|
+
categories.map((field) => ({'html': 'th', 'elem': {'text': field}})),
|
|
112
|
+
tableFields.map((field) => ({'html': 'th', 'elem': {'text': field}}))
|
|
58
113
|
]
|
|
59
114
|
},
|
|
60
115
|
|
|
@@ -62,31 +117,28 @@ export async function dataTableElements(dataTable, options = null) {
|
|
|
62
117
|
data.map((row, ixRow) => {
|
|
63
118
|
const rowPrev = ixRow > 0 ? data[ixRow - 1] : null;
|
|
64
119
|
let skip = rowPrev !== null;
|
|
65
|
-
|
|
66
120
|
return {
|
|
67
121
|
'html': 'tr',
|
|
68
122
|
'elem': [
|
|
69
|
-
|
|
70
|
-
const value =
|
|
123
|
+
categories.map((field) => {
|
|
124
|
+
const value = row[field] ?? null;
|
|
71
125
|
|
|
72
126
|
// Skip this value?
|
|
73
127
|
if (skip) {
|
|
74
|
-
|
|
75
|
-
const skipValuePrev = field in rowPrev ? rowPrev[field] : null;
|
|
76
|
-
skip = (compareValues(skipValue, skipValuePrev) === 0);
|
|
128
|
+
skip = (compareValues(value, rowPrev[field] ?? null) === 0);
|
|
77
129
|
}
|
|
78
130
|
|
|
79
|
-
const fieldElements =
|
|
80
|
-
? markdownElements(parseMarkdown(
|
|
81
|
-
: {'text': formatValue(value,
|
|
131
|
+
const fieldElements = markdown !== null && markdown.indexOf(field) !== -1
|
|
132
|
+
? markdownElements(parseMarkdown(`${value}`), markdownOptions)
|
|
133
|
+
: {'text': formatValue(value, formatPrecision, formatDatetime)};
|
|
82
134
|
return {'html': 'td', 'elem': skip ? null : fieldElements};
|
|
83
135
|
}),
|
|
84
|
-
|
|
85
|
-
const value =
|
|
86
|
-
const fieldElements =
|
|
87
|
-
? markdownElements(parseMarkdown(
|
|
88
|
-
: {'text': formatValue(value,
|
|
89
|
-
return {'html': 'td', 'elem':
|
|
136
|
+
tableFields.map((field) => {
|
|
137
|
+
const value = row[field] ?? null;
|
|
138
|
+
const fieldElements = markdown !== null && markdown.indexOf(field) !== -1
|
|
139
|
+
? markdownElements(parseMarkdown(`${value}`), markdownOptions)
|
|
140
|
+
: {'text': formatValue(value, formatPrecision, formatDatetime)};
|
|
141
|
+
return {'html': 'td', 'elem': fieldElements};
|
|
90
142
|
})
|
|
91
143
|
]
|
|
92
144
|
};
|
package/lib/lineChart.js
CHANGED
|
@@ -3,16 +3,107 @@
|
|
|
3
3
|
|
|
4
4
|
/** @module lib/lineChart */
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
} from '
|
|
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';
|
|
6
|
+
import {compareValues, formatValue, parameterValue, valueParameter} from './data.js';
|
|
7
|
+
import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
|
|
8
|
+
import {validateType} from 'schema-markdown/lib/schema.js';
|
|
13
9
|
|
|
14
10
|
|
|
15
|
-
|
|
11
|
+
// The line chart model's Schema Markdown
|
|
12
|
+
export const lineChartTypes = parseSchemaMarkdown(`\
|
|
13
|
+
group "Line Chart"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# A line chart model
|
|
17
|
+
struct LineChart
|
|
18
|
+
|
|
19
|
+
# The chart title
|
|
20
|
+
optional string title
|
|
21
|
+
|
|
22
|
+
# The chart width
|
|
23
|
+
optional int width
|
|
24
|
+
|
|
25
|
+
# The chart height
|
|
26
|
+
optional int height
|
|
27
|
+
|
|
28
|
+
# The numeric formatting precision (default is 2)
|
|
29
|
+
optional int(>= 0) precision
|
|
30
|
+
|
|
31
|
+
# The datetime format
|
|
32
|
+
optional LineChartDatetimeFormat datetime
|
|
33
|
+
|
|
34
|
+
# The line chart's X-axis field
|
|
35
|
+
string x
|
|
36
|
+
|
|
37
|
+
# The line chart's Y-axis fields
|
|
38
|
+
string[len > 0] y
|
|
39
|
+
|
|
40
|
+
# The color encoding field
|
|
41
|
+
optional string color
|
|
42
|
+
|
|
43
|
+
# The X-axis tick marks
|
|
44
|
+
optional LineChartAxisTicks xTicks
|
|
45
|
+
|
|
46
|
+
# The Y-axis tick marks
|
|
47
|
+
optional LineChartAxisTicks yTicks
|
|
48
|
+
|
|
49
|
+
# The X-axis annotations
|
|
50
|
+
optional LineChartAxisAnnotation[len > 0] xLines
|
|
51
|
+
|
|
52
|
+
# The Y-axis annotations
|
|
53
|
+
optional LineChartAxisAnnotation[len > 0] yLines
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
# The axis tick mark model
|
|
57
|
+
struct LineChartAxisTicks
|
|
58
|
+
|
|
59
|
+
# The count of evenly-spaced tick marks. The default is 3.
|
|
60
|
+
optional int(>= 0) count
|
|
61
|
+
|
|
62
|
+
# The value of the first tick mark. Default is the minimum axis value.
|
|
63
|
+
optional object start
|
|
64
|
+
|
|
65
|
+
# The value of the last tick mark. Default is the maximum axis value.
|
|
66
|
+
optional object end
|
|
67
|
+
|
|
68
|
+
# The number of tick mark labels to skip after a rendered label
|
|
69
|
+
optional int(> 0) skip
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
# An axis annotation
|
|
73
|
+
struct LineChartAxisAnnotation
|
|
74
|
+
|
|
75
|
+
# The axis value
|
|
76
|
+
object value
|
|
77
|
+
|
|
78
|
+
# The annotation label
|
|
79
|
+
optional string label
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# A datetime format
|
|
83
|
+
enum LineChartDatetimeFormat
|
|
84
|
+
|
|
85
|
+
# ISO datetime year format
|
|
86
|
+
year
|
|
87
|
+
|
|
88
|
+
# ISO datetime month format
|
|
89
|
+
month
|
|
90
|
+
|
|
91
|
+
# ISO datetime day format
|
|
92
|
+
day
|
|
93
|
+
`);
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Validate a line chart model
|
|
98
|
+
*
|
|
99
|
+
* @param {Object} lineChart - The
|
|
100
|
+
* [line chart model]{@link https://craigahobbs.github.io/markdown-up/library/model.html#var.vName='LineChart'}
|
|
101
|
+
* @returns {Object}
|
|
102
|
+
* @throws [ValidationError]{@link https://craigahobbs.github.io/schema-markdown-js/module-lib_schema.ValidationError.html}
|
|
103
|
+
*/
|
|
104
|
+
export function validateLineChart(lineChart) {
|
|
105
|
+
return validateType(lineChartTypes, 'LineChart', lineChart);
|
|
106
|
+
}
|
|
16
107
|
|
|
17
108
|
|
|
18
109
|
// Line chart defaults
|
|
@@ -20,7 +111,31 @@ const defaultWidth = 640;
|
|
|
20
111
|
const defaultHeight = 320;
|
|
21
112
|
const defaultXAxisTickCount = 3;
|
|
22
113
|
const defaultYAxisTickCount = 3;
|
|
23
|
-
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
// The categorical color palette
|
|
117
|
+
const categoricalColors = [
|
|
118
|
+
'#1f77b4',
|
|
119
|
+
'#ff7f0e',
|
|
120
|
+
'#2ca02c',
|
|
121
|
+
'#d62728',
|
|
122
|
+
'#9467bd',
|
|
123
|
+
'#8c564b',
|
|
124
|
+
'#e377c2',
|
|
125
|
+
'#7f7f7f',
|
|
126
|
+
'#bcbd22',
|
|
127
|
+
'#17becf',
|
|
128
|
+
'#aec7e8',
|
|
129
|
+
'#ffbb78',
|
|
130
|
+
'#98df8a',
|
|
131
|
+
'#ff9896',
|
|
132
|
+
'#c5b0d5',
|
|
133
|
+
'#c49c94',
|
|
134
|
+
'#f7b6d2',
|
|
135
|
+
'#c7c7c7',
|
|
136
|
+
'#dbdb8d',
|
|
137
|
+
'#9edae5'
|
|
138
|
+
];
|
|
24
139
|
|
|
25
140
|
|
|
26
141
|
// Line chart constants (all numbers in pixels)
|
|
@@ -28,6 +143,7 @@ const pixelsPerPoint = 4 / 3;
|
|
|
28
143
|
const svgPrecision = 3;
|
|
29
144
|
const chartFontFamily = 'Arial, Helvetica, sans-serif';
|
|
30
145
|
const chartFontWidthRatio = 0.6;
|
|
146
|
+
const chartFontDefaultSize = 12;
|
|
31
147
|
const chartBackgroundColor = 'white';
|
|
32
148
|
const axisColor = 'black';
|
|
33
149
|
const axisTickLineColor = 'lightgray';
|
|
@@ -42,54 +158,32 @@ const chartLineWidth = 3;
|
|
|
42
158
|
|
|
43
159
|
|
|
44
160
|
/**
|
|
45
|
-
*
|
|
161
|
+
* The line chart options object
|
|
46
162
|
*
|
|
47
|
-
* @
|
|
48
|
-
* @
|
|
49
|
-
* @returns {Object} The line chart element model
|
|
163
|
+
* @typedef {Object} LineChartOptions
|
|
164
|
+
* @property {number} [fontSize] - The font size, in points
|
|
50
165
|
*/
|
|
51
|
-
export function lineChartCodeBlock(codeBlock, options = null) {
|
|
52
|
-
return chartCodeBlock(codeBlock, options, validateLineChart, lineChartElements);
|
|
53
|
-
}
|
|
54
166
|
|
|
55
167
|
|
|
56
168
|
/**
|
|
57
169
|
* Render a line chart
|
|
58
170
|
*
|
|
59
|
-
* @param {Object}
|
|
60
|
-
* @param {Object}
|
|
61
|
-
*
|
|
171
|
+
* @param {Object[]} data - The data array
|
|
172
|
+
* @param {Object} lineChart - The
|
|
173
|
+
* [line chart model]{@link https://craigahobbs.github.io/markdown-up/library/model.html#var.vName='LineChart'}
|
|
174
|
+
* @param {?Object} [options = null] - The [line chart options]{@link module:lib/lineChart~LineChartOptions}
|
|
175
|
+
* @returns {Object} The line chart [element model]{@link https://github.com/craigahobbs/element-model#readme}
|
|
62
176
|
*/
|
|
63
|
-
export
|
|
64
|
-
const chartFontSize = (options !== null && 'fontSize' in options ? options.fontSize :
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
// Validate X and Y field types
|
|
70
|
-
const {'x': xField, 'y': yFields, 'color': colorField = null} = lineChart;
|
|
71
|
-
for (const [fieldDesc, fields] of [['X-field', [xField]], ['Y-field', yFields]]) {
|
|
72
|
-
for (const field of fields) {
|
|
73
|
-
if (!(field in types)) {
|
|
74
|
-
throw new Error(`Unknown ${fieldDesc} "${field}"`);
|
|
75
|
-
}
|
|
76
|
-
if (types[field] === 'string') {
|
|
77
|
-
throw new Error(`Invalid type "${types[field]}" for ${fieldDesc} "${field}"`);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
const xFieldType = types[xField];
|
|
82
|
-
const yFieldType = types[yFields[0]];
|
|
83
|
-
for (const field of yFields) {
|
|
84
|
-
if (types[field] !== yFieldType) {
|
|
85
|
-
throw new Error(`Invalid type "${types[field]}" for Y-field "${field}"`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
177
|
+
export function lineChartElements(data, lineChart, options = null) {
|
|
178
|
+
const chartFontSize = pixelsPerPoint * (options !== null && 'fontSize' in options ? options.fontSize : chartFontDefaultSize);
|
|
179
|
+
const xField = lineChart.x;
|
|
180
|
+
const yFields = lineChart.y;
|
|
181
|
+
const colorField = lineChart.color ?? null;
|
|
88
182
|
|
|
89
183
|
// Sort the rows by the X field
|
|
90
184
|
data.sort((row1, row2) => {
|
|
91
|
-
const x1 =
|
|
92
|
-
const x2 =
|
|
185
|
+
const x1 = row1[xField] ?? null;
|
|
186
|
+
const x2 = row2[xField] ?? null;
|
|
93
187
|
return compareValues(x1, x2);
|
|
94
188
|
});
|
|
95
189
|
|
|
@@ -105,20 +199,20 @@ export async function lineChartElements(lineChart, options = null) {
|
|
|
105
199
|
const pointsMap = {};
|
|
106
200
|
for (const row of data) {
|
|
107
201
|
for (const yField of yFields) {
|
|
108
|
-
const colorValue = formatValue(
|
|
202
|
+
const colorValue = formatValue(row[colorField] ?? null, lineChart.precision, lineChart.datetime);
|
|
109
203
|
const rowKey = (yFields.length === 1 ? colorValue : `${yField}, ${colorValue}`);
|
|
110
204
|
colorValueSet.add(rowKey);
|
|
111
|
-
const xRow =
|
|
112
|
-
const yRow =
|
|
205
|
+
const xRow = row[xField] ?? null;
|
|
206
|
+
const yRow = row[yField] ?? null;
|
|
113
207
|
if (xRow !== null && yRow !== null) {
|
|
114
208
|
if (!(rowKey in pointsMap)) {
|
|
115
209
|
pointsMap[rowKey] = [];
|
|
116
210
|
}
|
|
117
211
|
pointsMap[rowKey].push([xRow, yRow]);
|
|
118
|
-
xMin = xMin === null ? xRow :
|
|
119
|
-
yMin = yMin === null ? yRow : yRow < yMin ? yRow : yMin;
|
|
120
|
-
xMax =
|
|
121
|
-
yMax = yMax === null ? yRow : yRow > yMax ? yRow : yMax;
|
|
212
|
+
xMin = (xMin === null ? xRow : xMin);
|
|
213
|
+
yMin = (yMin === null ? yRow : (yRow < yMin ? yRow : yMin));
|
|
214
|
+
xMax = xRow;
|
|
215
|
+
yMax = (yMax === null ? yRow : (yRow > yMax ? yRow : yMax));
|
|
122
216
|
}
|
|
123
217
|
}
|
|
124
218
|
}
|
|
@@ -143,16 +237,14 @@ export async function lineChartElements(lineChart, options = null) {
|
|
|
143
237
|
|
|
144
238
|
// Add the points
|
|
145
239
|
for (const row of data) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
yMax = yMax === null ? yRow : yRow > yMax ? yRow : yMax;
|
|
155
|
-
}
|
|
240
|
+
const xRow = row[xField] ?? null;
|
|
241
|
+
const yRow = row[yField] ?? null;
|
|
242
|
+
if (xRow !== null && yRow !== null) {
|
|
243
|
+
points.push([xRow, yRow]);
|
|
244
|
+
xMin = (xMin === null ? xRow : xMin);
|
|
245
|
+
yMin = (yMin === null ? yRow : (yRow < yMin ? yRow : yMin));
|
|
246
|
+
xMax = xRow;
|
|
247
|
+
yMax = (yMax === null ? yRow : (yRow > yMax ? yRow : yMax));
|
|
156
248
|
}
|
|
157
249
|
}
|
|
158
250
|
}
|
|
@@ -163,106 +255,77 @@ export async function lineChartElements(lineChart, options = null) {
|
|
|
163
255
|
throw new Error('No data');
|
|
164
256
|
}
|
|
165
257
|
|
|
166
|
-
// Calculated expression helper function
|
|
167
|
-
const evalOptions = {'globals': variables};
|
|
168
|
-
const computeExpr = (exprText, expectedType = null, desc = null) => {
|
|
169
|
-
const exprValue = evaluateExpression(parseExpression(exprText), evalOptions);
|
|
170
|
-
if (exprValue !== null && expectedType !== null) {
|
|
171
|
-
const exprType = getCalculatedValueType(exprValue);
|
|
172
|
-
if (exprType !== expectedType) {
|
|
173
|
-
throw new Error(`Invalid ${desc} ${JSON.stringify(exprValue)} (type "${exprType}"), expected type "${expectedType}"`);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return exprValue;
|
|
177
|
-
};
|
|
178
|
-
|
|
179
258
|
// Compute the chart title, width, and height
|
|
180
|
-
const chartTitle =
|
|
181
|
-
const chartWidth =
|
|
182
|
-
const chartHeight =
|
|
259
|
+
const chartTitle = lineChart.title ?? null;
|
|
260
|
+
const chartWidth = lineChart.width ?? defaultWidth;
|
|
261
|
+
const chartHeight = lineChart.height ?? defaultHeight;
|
|
183
262
|
|
|
184
263
|
// Compute Y-axis tick values
|
|
185
264
|
const yAxisTicks = [];
|
|
186
|
-
const yTickCount = '
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const
|
|
190
|
-
const yTickStart = 'ytick' in lineChart && 'start' in lineChart.ytick
|
|
191
|
-
? computeExpr(lineChart.ytick.start, yFieldType, 'Y-axis start value')
|
|
192
|
-
: yMin;
|
|
193
|
-
const yTickEnd = 'ytick' in lineChart && 'end' in lineChart.ytick
|
|
194
|
-
? computeExpr(lineChart.ytick.end, yFieldType, 'Y-axis end value')
|
|
195
|
-
: yMax;
|
|
265
|
+
const yTickCount = ('yTicks' in lineChart && 'count' in lineChart.yTicks ? lineChart.yTicks.count : defaultYAxisTickCount);
|
|
266
|
+
const yTickSkip = ('yTicks' in lineChart && 'skip' in lineChart.yTicks ? lineChart.yTicks.skip + 1 : 1);
|
|
267
|
+
const yTickStart = ('yTicks' in lineChart && 'start' in lineChart.yTicks ? lineChart.yTicks.start : yMin);
|
|
268
|
+
const yTickEnd = ('yTicks' in lineChart && 'end' in lineChart.yTicks ? lineChart.yTicks.end : yMax);
|
|
196
269
|
for (let ixTick = 0; ixTick < yTickCount; ixTick++) {
|
|
197
270
|
const yTickParam = yTickCount === 1 ? 0 : ixTick / (yTickCount - 1);
|
|
198
271
|
const yTickValue = parameterValue(yTickParam, yTickStart, yTickEnd);
|
|
199
|
-
yAxisTicks.push([yTickValue, (ixTick % yTickSkip) !== 0 ? '' : formatValue(yTickValue, lineChart)]);
|
|
200
|
-
yMin = yTickValue < yMin ? yTickValue : yMin;
|
|
201
|
-
yMax = yTickValue > yMax ? yTickValue : yMax;
|
|
272
|
+
yAxisTicks.push([yTickValue, (ixTick % yTickSkip) !== 0 ? '' : formatValue(yTickValue, lineChart.precision, lineChart.datetime)]);
|
|
273
|
+
yMin = (yTickValue < yMin ? yTickValue : yMin);
|
|
274
|
+
yMax = (yTickValue > yMax ? yTickValue : yMax);
|
|
202
275
|
}
|
|
203
276
|
|
|
204
277
|
// Compute X-axis tick values
|
|
205
278
|
const xAxisTicks = [];
|
|
206
|
-
const xTickCount = '
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const
|
|
210
|
-
const xTickStart = 'xtick' in lineChart && 'start' in lineChart.xtick
|
|
211
|
-
? computeExpr(lineChart.xtick.start, xFieldType, 'X-axis start value')
|
|
212
|
-
: xMin;
|
|
213
|
-
const xTickEnd = 'xtick' in lineChart && 'end' in lineChart.xtick
|
|
214
|
-
? computeExpr(lineChart.xtick.end, xFieldType, 'X-axis end value')
|
|
215
|
-
: xMax;
|
|
279
|
+
const xTickCount = ('xTicks' in lineChart && 'count' in lineChart.xTicks ? lineChart.xTicks.count : defaultXAxisTickCount);
|
|
280
|
+
const xTickSkip = ('xTicks' in lineChart && 'skip' in lineChart.xTicks ? lineChart.xTicks.skip + 1 : 1);
|
|
281
|
+
const xTickStart = ('xTicks' in lineChart && 'start' in lineChart.xTicks ? lineChart.xTicks.start : xMin);
|
|
282
|
+
const xTickEnd = ('xTicks' in lineChart && 'end' in lineChart.xTicks ? lineChart.xTicks.end : xMax);
|
|
216
283
|
for (let ixTick = 0; ixTick < xTickCount; ixTick++) {
|
|
217
284
|
const xTickParam = xTickCount === 1 ? 0 : ixTick / (xTickCount - 1);
|
|
218
285
|
const xTickValue = parameterValue(xTickParam, xTickStart, xTickEnd);
|
|
219
|
-
xAxisTicks.push([xTickValue, (ixTick % xTickSkip) !== 0 ? '' : formatValue(xTickValue, lineChart)]);
|
|
220
|
-
xMin = xTickValue < xMin ? xTickValue : xMin;
|
|
221
|
-
xMax = xTickValue > xMax ? xTickValue : xMax;
|
|
286
|
+
xAxisTicks.push([xTickValue, (ixTick % xTickSkip) !== 0 ? '' : formatValue(xTickValue, lineChart.precision, lineChart.datetime)]);
|
|
287
|
+
xMin = (xTickValue < xMin ? xTickValue : xMin);
|
|
288
|
+
xMax = (xTickValue > xMax ? xTickValue : xMax);
|
|
222
289
|
}
|
|
223
290
|
|
|
224
291
|
// Compute Y-axis annotations
|
|
225
292
|
const yAxisAnnotations = [];
|
|
226
|
-
if ('
|
|
227
|
-
for (const annotation of lineChart.
|
|
228
|
-
const yAnnotationValue =
|
|
293
|
+
if ('yLines' in lineChart) {
|
|
294
|
+
for (const annotation of lineChart.yLines) {
|
|
295
|
+
const yAnnotationValue = annotation.value;
|
|
229
296
|
yAxisAnnotations.push([
|
|
230
297
|
yAnnotationValue,
|
|
231
|
-
|
|
232
|
-
? formatValue(computeExpr(annotation.label), lineChart)
|
|
233
|
-
: formatValue(yAnnotationValue, lineChart)
|
|
298
|
+
annotation.label ?? formatValue(yAnnotationValue, lineChart.precision, lineChart.datetime)
|
|
234
299
|
]);
|
|
235
|
-
yMin = yAnnotationValue < yMin ? yAnnotationValue : yMin;
|
|
236
|
-
yMax = yAnnotationValue > yMax ? yAnnotationValue : yMax;
|
|
300
|
+
yMin = (yAnnotationValue < yMin ? yAnnotationValue : yMin);
|
|
301
|
+
yMax = (yAnnotationValue > yMax ? yAnnotationValue : yMax);
|
|
237
302
|
}
|
|
238
303
|
}
|
|
239
304
|
|
|
240
305
|
// Compute X-axis annotations
|
|
241
306
|
const xAxisAnnotations = [];
|
|
242
|
-
if ('
|
|
243
|
-
for (const annotation of lineChart.
|
|
244
|
-
const xAnnotationValue =
|
|
307
|
+
if ('xLines' in lineChart) {
|
|
308
|
+
for (const annotation of lineChart.xLines) {
|
|
309
|
+
const xAnnotationValue = annotation.value;
|
|
245
310
|
xAxisAnnotations.push([
|
|
246
311
|
xAnnotationValue,
|
|
247
|
-
|
|
248
|
-
? formatValue(computeExpr(annotation.label), lineChart)
|
|
249
|
-
: formatValue(xAnnotationValue, lineChart)
|
|
312
|
+
annotation.label ?? formatValue(xAnnotationValue, lineChart.precision, lineChart.datetime)
|
|
250
313
|
]);
|
|
251
|
-
xMin = xAnnotationValue < xMin ? xAnnotationValue : xMin;
|
|
252
|
-
xMax = xAnnotationValue > xMax ? xAnnotationValue : xMax;
|
|
314
|
+
xMin = (xAnnotationValue < xMin ? xAnnotationValue : xMin);
|
|
315
|
+
xMax = (xAnnotationValue > xMax ? xAnnotationValue : xMax);
|
|
253
316
|
}
|
|
254
317
|
}
|
|
255
318
|
|
|
256
319
|
// Chart title calculations
|
|
257
320
|
const chartBorderSize = chartFontSize;
|
|
258
321
|
const chartTitleFontSize = 1.1 * chartFontSize;
|
|
259
|
-
const chartTitleHeight = chartTitle !== null ? 1.5 * chartTitleFontSize : 0;
|
|
322
|
+
const chartTitleHeight = (chartTitle !== null ? 1.5 * chartTitleFontSize : 0);
|
|
260
323
|
|
|
261
324
|
// Y-axis calculations
|
|
262
325
|
const axisTitleFontSize = 1 * chartFontSize;
|
|
263
326
|
const axisLabelFontSize = chartFontSize;
|
|
264
|
-
const yAxisTitle = yFields.length === 1 ? yFields[0] : null;
|
|
265
|
-
const yAxisTitleWidth = yAxisTitle !== null ? 1.8 * axisTitleFontSize : 0;
|
|
327
|
+
const yAxisTitle = (yFields.length === 1 ? yFields[0] : null);
|
|
328
|
+
const yAxisTitleWidth = (yAxisTitle !== null ? 1.8 * axisTitleFontSize : 0);
|
|
266
329
|
const yAxisLabelWidth = yAxisTicks.reduce((labelMax, [, label]) => {
|
|
267
330
|
const labelWidth = label.length * chartFontWidthRatio * axisLabelFontSize;
|
|
268
331
|
return labelWidth > labelMax ? labelWidth : labelMax;
|
|
@@ -277,7 +340,7 @@ export async function lineChartElements(lineChart, options = null) {
|
|
|
277
340
|
const xAxisTitleHeight = 1.8 * axisTitleFontSize;
|
|
278
341
|
const xAxisTickGap = 0.75 * axisTickLength;
|
|
279
342
|
const xAxisY = chartHeight - chartBorderSize - xAxisTitleHeight -
|
|
280
|
-
|
|
343
|
+
(xAxisTicks.length === 0 ? 0 : axisLabelFontSize - xAxisTickGap + axisTickLength);
|
|
281
344
|
|
|
282
345
|
// Y-axis annotation calculations
|
|
283
346
|
const annotationLabelFontSize = axisLabelFontSize;
|
|
@@ -309,7 +372,7 @@ export async function lineChartElements(lineChart, options = null) {
|
|
|
309
372
|
const chartTop = chartBorderSize + chartTitleHeight + 0.5 * chartLineWidth;
|
|
310
373
|
const chartLeft = yAxisX + 0.5 * axisLineWidth + 0.5 * chartLineWidth;
|
|
311
374
|
const chartBottom = xAxisY - 0.5 * axisLineWidth - 0.5 * chartLineWidth;
|
|
312
|
-
const chartRight = colorLegendX !== null ? colorLegendX - colorLegendGap : chartWidth - chartBorderSize;
|
|
375
|
+
const chartRight = (colorLegendX !== null ? colorLegendX - colorLegendGap : chartWidth - chartBorderSize);
|
|
313
376
|
|
|
314
377
|
// Axis label limits
|
|
315
378
|
const xAxisLabelLeft = chartLeft + 0.5 * axisLabelFontSize;
|
|
@@ -422,7 +485,7 @@ export async function lineChartElements(lineChart, options = null) {
|
|
|
422
485
|
'x': svgValue(yAxisX - axisTickLength - yAxisTickGap),
|
|
423
486
|
'y': svgValue(yPoint),
|
|
424
487
|
'text-anchor': 'end',
|
|
425
|
-
'dominant-baseline': yPoint > yAxisLabelBottom ? 'auto' : (yPoint < yAxisLabelTop ? 'hanging' : 'middle')
|
|
488
|
+
'dominant-baseline': (yPoint > yAxisLabelBottom ? 'auto' : (yPoint < yAxisLabelTop ? 'hanging' : 'middle'))
|
|
426
489
|
},
|
|
427
490
|
'elem': {'text': yLabel}
|
|
428
491
|
};
|
|
@@ -483,7 +546,7 @@ export async function lineChartElements(lineChart, options = null) {
|
|
|
483
546
|
'fill': axisColor,
|
|
484
547
|
'x': svgValue(xPoint),
|
|
485
548
|
'y': svgValue(xAxisY + axisTickLength + xAxisTickGap),
|
|
486
|
-
'text-anchor': xPoint < xAxisLabelLeft ? 'start' : (xPoint > xAxisLabelRight ? 'end' : 'middle'),
|
|
549
|
+
'text-anchor': (xPoint < xAxisLabelLeft ? 'start' : (xPoint > xAxisLabelRight ? 'end' : 'middle')),
|
|
487
550
|
'dominant-baseline': 'hanging'
|
|
488
551
|
},
|
|
489
552
|
'elem': {'text': xLabel}
|
|
@@ -586,7 +649,7 @@ export async function lineChartElements(lineChart, options = null) {
|
|
|
586
649
|
? xPoint - 0.5 * annotationLineWidth - xAnnotationLabelOffsetX - annotationLabelMargin
|
|
587
650
|
: xPoint + 0.5 * annotationLineWidth + xAnnotationLabelOffsetX + annotationLabelMargin),
|
|
588
651
|
'y': svgValue(labelY + annotationLabelMargin + 0.5 * annotationLabelFontSize),
|
|
589
|
-
'text-anchor': isLeft ? 'end' : 'start',
|
|
652
|
+
'text-anchor': (isLeft ? 'end' : 'start'),
|
|
590
653
|
'dominant-baseline': 'middle'
|
|
591
654
|
},
|
|
592
655
|
'elem': {'text': xLabel}
|