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