@nova-energy/openamx 0.0.0-stage → 0.10.1

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.
Files changed (42) hide show
  1. package/COMMERCIAL-LICENSE.md +74 -0
  2. package/LICENSE.md +660 -0
  3. package/README.md +513 -2
  4. package/dist/ast/types.js +2 -0
  5. package/dist/cli.js +186 -0
  6. package/dist/diagnostics/errors.js +94 -0
  7. package/dist/editor/completion.js +313 -0
  8. package/dist/editor/highlighting.js +224 -0
  9. package/dist/editor/moduleAnalysis.js +206 -0
  10. package/dist/editor/refactoring.js +46 -0
  11. package/dist/editor/sourceRanges.js +50 -0
  12. package/dist/editor/symbols.js +203 -0
  13. package/dist/formatter/formatAmx.js +79 -0
  14. package/dist/index.js +10 -0
  15. package/dist/parser/parseDocument.js +114 -0
  16. package/dist/parser/parseExpression.js +768 -0
  17. package/dist/parser/parseFor.js +53 -0
  18. package/dist/parser/parseFrontMatter.js +65 -0
  19. package/dist/parser/parseStatements.js +667 -0
  20. package/dist/parser/stringScanner.js +71 -0
  21. package/dist/renderer/chartModel.js +476 -0
  22. package/dist/renderer/renderHtml.js +383 -0
  23. package/dist/renderer/reportDocx.js +163 -0
  24. package/dist/renderer/reportPdf.js +202 -0
  25. package/dist/renderer/reportPreparation.js +208 -0
  26. package/dist/runtime/csvTextSerialization.js +8 -0
  27. package/dist/runtime/dataText.js +191 -0
  28. package/dist/runtime/docxDestination.js +55 -0
  29. package/dist/runtime/environment.js +105 -0
  30. package/dist/runtime/evaluateDocument.js +32 -0
  31. package/dist/runtime/evaluateExpression.js +888 -0
  32. package/dist/runtime/externalUnits.js +91 -0
  33. package/dist/runtime/inputData.js +508 -0
  34. package/dist/runtime/measurement.js +110 -0
  35. package/dist/runtime/moduleLoader.js +279 -0
  36. package/dist/runtime/outputData.js +303 -0
  37. package/dist/runtime/pdfDestination.js +57 -0
  38. package/dist/runtime/standardLibrary.js +145 -0
  39. package/dist/typechecker/checkDocument.js +1282 -0
  40. package/dist/typechecker/declarationRegistry.js +7 -0
  41. package/dist/typechecker/dimensionTypes.js +1 -0
  42. package/package.json +33 -4
@@ -0,0 +1,71 @@
1
+ export function findStringLiteralEnd(text, start) {
2
+ const quote = text[start];
3
+ if (quote !== '"' && quote !== "'")
4
+ return undefined;
5
+ for (let index = start + 1; index < text.length; index++) {
6
+ const character = text[index];
7
+ if (character === '\r' || character === '\n')
8
+ return undefined;
9
+ if (character === '\\') {
10
+ if (index + 1 >= text.length || text[index + 1] === '\r' || text[index + 1] === '\n')
11
+ return undefined;
12
+ index++;
13
+ continue;
14
+ }
15
+ if (character === quote)
16
+ return index;
17
+ if (quote === '"' && text.startsWith('${', index)) {
18
+ const close = findInterpolationEnd(text, index + 2);
19
+ if (close === undefined)
20
+ return undefined;
21
+ index = close;
22
+ }
23
+ }
24
+ return undefined;
25
+ }
26
+ export function findInterpolationEnd(text, start) {
27
+ const delimiters = [];
28
+ let quote;
29
+ let escaped = false;
30
+ for (let index = start; index < text.length; index++) {
31
+ const character = text[index];
32
+ if (character === '\r' || character === '\n')
33
+ return undefined;
34
+ if (quote) {
35
+ if (escaped)
36
+ escaped = false;
37
+ else if (character === '\\')
38
+ escaped = true;
39
+ else if (character === quote)
40
+ quote = undefined;
41
+ continue;
42
+ }
43
+ if (character === '"' || character === "'")
44
+ quote = character;
45
+ else if (character === '(' || character === '[' || character === '{')
46
+ delimiters.push(character);
47
+ else if (character === '}' && delimiters[delimiters.length - 1] === '{')
48
+ delimiters.pop();
49
+ else if (character === '}' && delimiters.length === 0)
50
+ return index;
51
+ else if ((character === ')' && delimiters[delimiters.length - 1] === '(')
52
+ || (character === ']' && delimiters[delimiters.length - 1] === '['))
53
+ delimiters.pop();
54
+ }
55
+ return undefined;
56
+ }
57
+ export function withoutStringLiterals(text) {
58
+ let result = '';
59
+ for (let index = 0; index < text.length; index++) {
60
+ if (text[index] === '"' || text[index] === "'") {
61
+ const end = findStringLiteralEnd(text, index);
62
+ if (end === undefined)
63
+ break;
64
+ index = end;
65
+ }
66
+ else {
67
+ result += text[index];
68
+ }
69
+ }
70
+ return result;
71
+ }
@@ -0,0 +1,476 @@
1
+ const SERIES_PALETTE = ['#146c94', '#d97706', '#15803d', '#b42318'];
2
+ const NO_DATA_LABEL = 'No data';
3
+ export function createChartViewModel(emission, identity) {
4
+ const declaration = emission.declaration;
5
+ const title = optionText(declaration, 'viewTitleOption') ?? emission.name;
6
+ const description = optionText(declaration, 'viewDescriptionOption') ?? `${declaration.kind} chart`;
7
+ const id = `openamx-chart-${emission.documentNodeIndex}-${emission.statementIndex}`;
8
+ const table = buildTable(emission, title, id);
9
+ const rowInputs = buildRows(emission, id);
10
+ const seriesOptions = declaration.options.filter(isChartSeries);
11
+ const valueAxisRole = declaration.kind === 'bar' ? 'x' : 'y';
12
+ const accent = identity.accent;
13
+ const groups = declaration.kind === 'scatter' ? buildGroups(emission, rowInputs) : [];
14
+ const seriesNames = declaration.kind === 'scatter'
15
+ ? groups.map(group => group.name)
16
+ : seriesOptions.map(option => option.label);
17
+ const series = seriesNames.map((name, index) => {
18
+ const descriptor = declaration.kind === 'scatter'
19
+ ? undefined
20
+ : emission.measurementDescriptors?.find(item => item.role === 'series' && item.field === seriesOptions[index]?.field);
21
+ const axisId = descriptor ? axisIdentity(valueAxisRole, descriptor) : `${valueAxisRole}:unitless`;
22
+ return Object.freeze({
23
+ id: declaration.kind === 'scatter' ? groups[index].id : seriesIdentity(seriesOptions[index], index),
24
+ name,
25
+ color: chartColor(index),
26
+ ...(declaration.kind === 'scatter' ? {} : { axisId }),
27
+ ...(descriptor ? { unit: copyUnit(descriptor.unit) } : {})
28
+ });
29
+ });
30
+ const axes = buildAxes(emission, seriesOptions);
31
+ const xOption = fieldOption(declaration, 'x');
32
+ const xDescriptor = emission.measurementDescriptors?.find(item => item.role === 'x');
33
+ const dateTimeX = declaration.kind === 'line' && !!xOption
34
+ && rowInputs.some(row => isDateTimeValue(row.x))
35
+ && rowInputs.every(row => row.x === null || isDateTimeValue(row.x));
36
+ const categoryLineX = declaration.kind === 'line' && !!xOption && !dateTimeX
37
+ && rowInputs.some(row => typeof row.x === 'string');
38
+ const scalarLine = declaration.kind === 'line' && !xOption;
39
+ let optionSeries;
40
+ let plottedPointCount = 0;
41
+ let xAxis;
42
+ let yAxis;
43
+ let grid;
44
+ const categoryLabels = new Map(table.rows.map(row => [row.id, valueText(row.values[0] ?? null)]));
45
+ if (declaration.kind === 'scatter') {
46
+ optionSeries = groups.map((group, index) => {
47
+ const points = group.points.filter(point => typeof point[0] === 'number' && typeof point[1] === 'number');
48
+ plottedPointCount += points.length;
49
+ return {
50
+ id: group.id,
51
+ name: group.name,
52
+ type: 'scatter',
53
+ data: points,
54
+ xAxisIndex: 0,
55
+ yAxisIndex: 0,
56
+ itemStyle: { color: chartColor(index) },
57
+ emphasis: { itemStyle: { color: accent } }
58
+ };
59
+ });
60
+ xAxis = valueAxis(axes.find(axis => axis.role === 'x'), 'bottom');
61
+ yAxis = valueAxis(axes.find(axis => axis.role === 'y'), 'left');
62
+ grid = { left: 88, right: 32, top: 56, bottom: 62, containLabel: true };
63
+ }
64
+ else if (declaration.kind === 'bar' || declaration.kind === 'column') {
65
+ const categoryRows = rowInputs;
66
+ const valueAxes = axes.filter(axis => axis.role === (declaration.kind === 'bar' ? 'x' : 'y'));
67
+ optionSeries = seriesOptions.map((seriesOption, index) => {
68
+ const values = categoryRows.map(row => seriesValue(row.source, seriesOption));
69
+ plottedPointCount += values.filter(value => typeof value === 'number').length;
70
+ const axisIndex = valueAxes.findIndex(axis => axis.id === series[index].axisId);
71
+ return {
72
+ id: series[index].id,
73
+ name: series[index].name,
74
+ type: 'bar',
75
+ data: values,
76
+ ...(declaration.kind === 'bar' ? { xAxisIndex: Math.max(0, axisIndex), yAxisIndex: 0 } : { xAxisIndex: 0, yAxisIndex: Math.max(0, axisIndex) }),
77
+ barMaxWidth: 28,
78
+ itemStyle: { color: series[index].color },
79
+ emphasis: { itemStyle: { color: accent } }
80
+ };
81
+ });
82
+ const categoryData = categoryRows.map(row => row.id);
83
+ const categoryLabels = Object.fromEntries(categoryRows.map(row => [row.id, valueText(row.label)]));
84
+ const categoryAxis = {
85
+ type: 'category',
86
+ data: categoryData,
87
+ axisLabel: { hideOverlap: true, formatter: (value) => categoryLabels[value] ?? value },
88
+ axisTick: { alignWithLabel: true }
89
+ };
90
+ const axesForValues = valueAxes.map((axis, index) => valueAxis(axis, 'bottom', index * 34));
91
+ if (declaration.kind === 'bar') {
92
+ xAxis = axesForValues;
93
+ yAxis = { ...categoryAxis, inverse: true };
94
+ grid = { left: 112, right: 36, top: 56, bottom: Math.max(62, axesForValues.length * 36 + 24), containLabel: true };
95
+ }
96
+ else {
97
+ xAxis = categoryAxis;
98
+ yAxis = axesForValues;
99
+ grid = {
100
+ left: 56 + Math.ceil(valueAxes.length / 2) * 44,
101
+ right: 32 + Math.floor(valueAxes.length / 2) * 44,
102
+ top: 56,
103
+ bottom: 72,
104
+ containLabel: true
105
+ };
106
+ }
107
+ }
108
+ else {
109
+ const xValues = scalarLine
110
+ ? rowInputs.map(row => row.label)
111
+ : rowInputs.map(row => row.x);
112
+ const xLabels = Object.fromEntries(rowInputs.map((row, index) => [row.id, valueText(xValues[index])]));
113
+ const xAxisType = scalarLine || categoryLineX ? 'category' : dateTimeX ? 'time' : 'value';
114
+ const valueAxes = axes.filter(axis => axis.role === 'y');
115
+ optionSeries = seriesOptions.map((seriesOption, index) => {
116
+ const points = rowInputs.map(row => [scalarLine || categoryLineX ? row.id : row.x, seriesValue(row.source, seriesOption)]);
117
+ plottedPointCount += points.filter(point => typeof point[1] === 'number').length;
118
+ const axisIndex = valueAxes.findIndex(axis => axis.id === series[index].axisId);
119
+ return {
120
+ id: series[index].id,
121
+ name: series[index].name,
122
+ type: 'line',
123
+ data: points,
124
+ encode: { x: 0, y: 1 },
125
+ yAxisIndex: Math.max(0, axisIndex),
126
+ connectNulls: false,
127
+ showSymbol: true,
128
+ smooth: false,
129
+ itemStyle: { color: series[index].color },
130
+ lineStyle: { color: series[index].color },
131
+ emphasis: { itemStyle: { color: accent }, lineStyle: { color: accent } }
132
+ };
133
+ });
134
+ xAxis = xAxisType === 'category'
135
+ ? {
136
+ type: 'category',
137
+ data: rowInputs.map(row => row.id),
138
+ axisLabel: { hideOverlap: true, formatter: (value) => categoryLineX ? categoryLabels.get(value) ?? value : xLabels[value] ?? value }
139
+ }
140
+ : {
141
+ type: xAxisType,
142
+ ...(xDescriptor ? { name: xDescriptor.unit.text } : {}),
143
+ ...(dateTimeX ? {
144
+ axisLabel: { hideOverlap: true, formatter: utcIsoAxisLabel },
145
+ axisPointer: { label: { formatter: (params) => utcIsoAxisLabel(params.value) } }
146
+ } : {})
147
+ };
148
+ yAxis = valueAxes.map((axis, index) => valueAxis(axis, index % 2 === 0 ? 'left' : 'right', Math.floor(index / 2) * 40));
149
+ grid = {
150
+ left: 56 + Math.ceil(valueAxes.length / 2) * 40,
151
+ right: 32 + Math.floor(valueAxes.length / 2) * 40,
152
+ top: 56,
153
+ bottom: 62,
154
+ containLabel: true
155
+ };
156
+ }
157
+ const empty = plottedPointCount === 0;
158
+ const option = {
159
+ animation: false,
160
+ aria: { enabled: false },
161
+ color: series.map(item => item.color),
162
+ textStyle: { fontFamily: 'sans-serif', fontSize: 12, color: '#263238' },
163
+ title: { text: title, left: 'center', textStyle: { color: accent, fontSize: 16, fontWeight: 600 } },
164
+ tooltip: {
165
+ trigger: declaration.kind === 'scatter' ? 'item' : 'axis',
166
+ axisPointer: { type: 'cross' },
167
+ formatter: (params) => formatTooltip(params, declaration.kind, series, axes, categoryLabels, dateTimeX)
168
+ },
169
+ legend: { show: series.length > 1, top: 30, data: series.map(item => item.name) },
170
+ grid,
171
+ xAxis,
172
+ yAxis,
173
+ series: optionSeries,
174
+ ...(empty ? {
175
+ graphic: {
176
+ type: 'text',
177
+ left: 'center',
178
+ top: 'middle',
179
+ style: { text: NO_DATA_LABEL, fill: '#52606d', fontSize: 14 }
180
+ }
181
+ } : {})
182
+ };
183
+ const accessibility = Object.freeze({
184
+ role: 'img',
185
+ title,
186
+ description,
187
+ summary: `${table.rows.length} source rows; ${plottedPointCount} plotted points. Complete chart data is in table ${table.id}.${dateTimeX ? ' DateTime values are presented in UTC ISO 8601 format; source order is preserved.' : ''}`,
188
+ tableId: table.id
189
+ });
190
+ const tooltip = Object.freeze({
191
+ trigger: declaration.kind === 'scatter' ? 'item' : 'axis',
192
+ includesCategoryOrCoordinates: true,
193
+ includesSeriesOrGroup: true,
194
+ includesDisplayUnits: true,
195
+ ...(dateTimeX ? { dateTimePresentation: 'UTC ISO 8601' } : {})
196
+ });
197
+ const model = {
198
+ kind: declaration.kind,
199
+ title,
200
+ description,
201
+ dimensions: Object.freeze({ width: 720, height: 360 }),
202
+ option,
203
+ table,
204
+ series: Object.freeze(series),
205
+ axes,
206
+ tooltip,
207
+ plottedPointCount,
208
+ accessibility,
209
+ ...(empty ? { emptyState: Object.freeze({ label: NO_DATA_LABEL }) } : {}),
210
+ ...(dateTimeX ? { dateTimePresentation: 'UTC ISO 8601' } : {})
211
+ };
212
+ return deepFreeze(model);
213
+ }
214
+ function buildTable(emission, title, id) {
215
+ const declaration = emission.declaration;
216
+ const category = fieldOption(declaration, 'category');
217
+ const x = fieldOption(declaration, 'x');
218
+ const group = fieldOption(declaration, 'group');
219
+ const seriesOptions = declaration.options.filter(isChartSeries);
220
+ let headings;
221
+ const rows = emission.data.map((value, index) => {
222
+ let values;
223
+ if (isRecord(value)) {
224
+ if (declaration.kind === 'scatter') {
225
+ values = [value[x?.field ?? 'x'] ?? null, value[fieldOption(declaration, 'y')?.field ?? 'y'] ?? null];
226
+ if (emission.headings?.length === 3)
227
+ values.push(group ? value[group.field] ?? null : '');
228
+ }
229
+ else if (declaration.kind === 'line') {
230
+ values = [value[x?.field ?? 'x'] ?? null, ...seriesOptions.map(option => option.field ? value[option.field] ?? null : null)];
231
+ }
232
+ else {
233
+ values = [value[category?.field ?? 'category'] ?? null, ...seriesOptions.map(option => option.field ? value[option.field] ?? null : null)];
234
+ }
235
+ }
236
+ else {
237
+ const labels = emission.labels ?? emission.data.map((_item, labelIndex) => String(labelIndex + 1));
238
+ values = [labels[index] ?? String(index + 1), value];
239
+ }
240
+ return Object.freeze({
241
+ id: `${id}-row-${index + 1}`,
242
+ values: Object.freeze(values.map(cloneValue))
243
+ });
244
+ });
245
+ if (declaration.kind === 'scatter') {
246
+ headings = emission.headings ? [...emission.headings] : ['x', 'y', ...(group ? ['group'] : [])];
247
+ }
248
+ else if (declaration.kind === 'line' && x) {
249
+ const xDescriptor = emission.measurementDescriptors?.find(item => item.role === 'x');
250
+ headings = [xDescriptor ? `x (${xDescriptor.unit.text})` : 'x', ...seriesOptions.map((option, index) => {
251
+ const seriesHeading = emission.headings?.[index + 1] ?? option.label;
252
+ return seriesHeading;
253
+ })];
254
+ }
255
+ else {
256
+ headings = emission.headings ? [...emission.headings] : ['label', ...seriesOptions.map(option => option.label)];
257
+ }
258
+ return Object.freeze({
259
+ id: `${id}-data`,
260
+ caption: `Data for ${title}`,
261
+ headings: Object.freeze(headings),
262
+ rows: Object.freeze(rows)
263
+ });
264
+ }
265
+ function buildRows(emission, id) {
266
+ const declaration = emission.declaration;
267
+ const category = fieldOption(declaration, 'category');
268
+ const x = fieldOption(declaration, 'x');
269
+ const group = fieldOption(declaration, 'group');
270
+ return emission.data.map((source, index) => {
271
+ const label = isRecord(source)
272
+ ? declaration.kind === 'scatter' ? source[group?.field ?? ''] ?? null
273
+ : declaration.kind === 'line' ? source[x?.field ?? 'x'] ?? null
274
+ : source[category?.field ?? 'category'] ?? null
275
+ : emission.labels?.[index] ?? String(index + 1);
276
+ return Object.freeze({
277
+ id: `${id}-row-${index + 1}`,
278
+ source,
279
+ label,
280
+ x: isRecord(source) ? source[x?.field ?? 'x'] ?? null : String(index + 1),
281
+ group: isRecord(source) ? source[group?.field ?? ''] ?? null : null
282
+ });
283
+ });
284
+ }
285
+ function buildGroups(emission, rows) {
286
+ const x = fieldOption(emission.declaration, 'x');
287
+ const y = fieldOption(emission.declaration, 'y');
288
+ const groupOption = fieldOption(emission.declaration, 'group');
289
+ const groups = new Map();
290
+ for (const row of rows) {
291
+ const source = isRecord(row.source) ? row.source : {};
292
+ const groupValue = groupOption ? source[groupOption.field] ?? null : null;
293
+ const key = groupValue === null ? '__ungrouped__' : `${typeof groupValue}:${String(groupValue)}`;
294
+ let group = groups.get(key);
295
+ if (!group) {
296
+ const index = groups.size;
297
+ group = {
298
+ id: groupOption ? `group-${index + 1}` : 'group-all',
299
+ name: groupOption ? valueText(groupValue) : 'All points',
300
+ points: []
301
+ };
302
+ groups.set(key, group);
303
+ }
304
+ group.points.push([source[x?.field ?? 'x'] ?? null, source[y?.field ?? 'y'] ?? null]);
305
+ }
306
+ return [...groups.values()];
307
+ }
308
+ function buildAxes(emission, seriesOptions) {
309
+ const descriptors = emission.measurementDescriptors ?? [];
310
+ if (emission.declaration.kind === 'scatter') {
311
+ return ['x', 'y'].map(role => {
312
+ const descriptor = descriptors.find(item => item.role === role);
313
+ return Object.freeze({
314
+ id: descriptor ? axisIdentity(role, descriptor) : `${role}:unitless`,
315
+ role,
316
+ ...(descriptor ? { name: descriptor.unit.text, unit: copyUnit(descriptor.unit) } : {})
317
+ });
318
+ });
319
+ }
320
+ const role = emission.declaration.kind === 'bar' ? 'x' : 'y';
321
+ const axes = new Map();
322
+ for (const option of seriesOptions) {
323
+ const descriptor = descriptors.find(item => item.role === 'series' && item.field === option.field);
324
+ const id = descriptor ? axisIdentity(role, descriptor) : `${role}:unitless`;
325
+ if (!axes.has(id))
326
+ axes.set(id, Object.freeze({
327
+ id,
328
+ role,
329
+ ...(descriptor ? { name: descriptor.unit.text, unit: copyUnit(descriptor.unit) } : {})
330
+ }));
331
+ }
332
+ if (!axes.size)
333
+ axes.set(`${role}:unitless`, Object.freeze({ id: `${role}:unitless`, role }));
334
+ if (emission.declaration.kind === 'line') {
335
+ const descriptor = descriptors.find(item => item.role === 'x');
336
+ if (descriptor)
337
+ axes.set(axisIdentity('x', descriptor), Object.freeze({
338
+ id: axisIdentity('x', descriptor), role: 'x', name: descriptor.unit.text, unit: copyUnit(descriptor.unit)
339
+ }));
340
+ }
341
+ return [...axes.values()];
342
+ }
343
+ function seriesValue(source, option) {
344
+ const value = option.field && isRecord(source) ? source[option.field] : source;
345
+ return typeof value === 'number' ? value : null;
346
+ }
347
+ function valueAxis(axis, position, offset = 0) {
348
+ return {
349
+ type: 'value',
350
+ scale: false,
351
+ ...(axis?.name ? { name: axis.name } : {}),
352
+ ...(position === 'bottom' ? { position: 'bottom', offset } : { position, offset }),
353
+ axisLabel: { hideOverlap: true },
354
+ splitLine: { show: position !== 'bottom' }
355
+ };
356
+ }
357
+ function formatTooltip(input, kind, series, axes, categoryLabels, dateTimeX) {
358
+ const entries = (Array.isArray(input) ? input : [input]).filter(isUnknownRecord);
359
+ return entries.map(entry => {
360
+ const name = typeof entry.seriesName === 'string' ? entry.seriesName : '';
361
+ const seriesModel = typeof entry.seriesId === 'string'
362
+ ? series.find(item => item.id === entry.seriesId) ?? series.find(item => item.name === name)
363
+ : series.find(item => item.name === name);
364
+ const seriesIndex = seriesModel ? series.indexOf(seriesModel) : -1;
365
+ const rawValue = entry.value;
366
+ const values = Array.isArray(rawValue) ? rawValue : Array.isArray(entry.data) ? entry.data : [];
367
+ if (kind === 'scatter') {
368
+ const xValue = values[0] ?? null;
369
+ const yValue = values[1] ?? null;
370
+ const xUnit = axes.find(axis => axis.role === 'x')?.name;
371
+ const yUnit = axes.find(axis => axis.role === 'y')?.name;
372
+ return `<div><strong>${escapeTooltipText(name)}</strong><br>x: ${escapeTooltipText(tooltipValue(xValue, false))}${xUnit ? ` ${escapeTooltipText(xUnit)}` : ''}<br>y: ${escapeTooltipText(tooltipValue(yValue, false))}${yUnit ? ` ${escapeTooltipText(yUnit)}` : ''}</div>`;
373
+ }
374
+ const rawCategory = entry.axisValue ?? values[0] ?? entry.name;
375
+ const category = typeof rawCategory === 'string' && categoryLabels.has(rawCategory)
376
+ ? categoryLabels.get(rawCategory)
377
+ : dateTimeX ? tooltipValue(rawCategory, true) : tooltipValue(rawCategory, false);
378
+ const rawData = Array.isArray(rawValue) ? rawValue : [];
379
+ const value = kind === 'line' ? rawData[1] : rawValue;
380
+ const unit = seriesModel?.unit?.text;
381
+ const xUnit = kind === 'line' ? axes.find(axis => axis.role === 'x')?.name : undefined;
382
+ const seriesLabel = escapeTooltipText(name || `Series ${seriesIndex + 1}`);
383
+ const coordinate = `${escapeTooltipText(category)}${xUnit ? ` ${escapeTooltipText(xUnit)}` : ''}`;
384
+ const detail = `${coordinate}: ${escapeTooltipText(tooltipValue(value, false))}${unit ? ` ${escapeTooltipText(unit)}` : ''}`;
385
+ return `<div><strong>${seriesLabel}</strong><br>${detail}</div>`;
386
+ }).join('');
387
+ }
388
+ function tooltipValue(value, dateTime) {
389
+ if (value === null || value === undefined)
390
+ return 'No value';
391
+ if (dateTime && (typeof value === 'string' || typeof value === 'number'))
392
+ return utcIsoAxisLabel(value) || String(value);
393
+ return String(value);
394
+ }
395
+ function escapeTooltipText(value) {
396
+ return value.replace(/[&<>"']/g, character => ({
397
+ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;'
398
+ })[character]);
399
+ }
400
+ function axisIdentity(role, descriptor) {
401
+ const dimensions = Object.entries(descriptor.unit.vector).sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0);
402
+ return `${role}:${descriptor.unit.text}:${descriptor.unit.scale}:${JSON.stringify(dimensions)}`;
403
+ }
404
+ function copyUnit(unit) {
405
+ return Object.freeze({
406
+ text: unit.text,
407
+ scale: unit.scale,
408
+ vector: Object.freeze({ ...unit.vector }),
409
+ factors: Object.freeze(unit.factors.map(factor => Object.freeze({ ...factor })))
410
+ });
411
+ }
412
+ function seriesIdentity(option, index) {
413
+ return option?.field ? `series:${option.field}` : `series:${index + 1}`;
414
+ }
415
+ function chartColor(index) {
416
+ if (index < SERIES_PALETTE.length)
417
+ return SERIES_PALETTE[index];
418
+ return hslToHex((index * 137.508 + 21) % 360, 68, 34);
419
+ }
420
+ function hslToHex(hue, saturation, lightness) {
421
+ const s = saturation / 100;
422
+ const l = lightness / 100;
423
+ const chroma = (1 - Math.abs(2 * l - 1)) * s;
424
+ const segment = hue / 60;
425
+ const x = chroma * (1 - Math.abs(segment % 2 - 1));
426
+ const [red, green, blue] = segment < 1 ? [chroma, x, 0]
427
+ : segment < 2 ? [x, chroma, 0]
428
+ : segment < 3 ? [0, chroma, x]
429
+ : segment < 4 ? [0, x, chroma]
430
+ : segment < 5 ? [x, 0, chroma] : [chroma, 0, x];
431
+ const match = l - chroma / 2;
432
+ return `#${[red, green, blue].map(channel => Math.round((channel + match) * 255).toString(16).padStart(2, '0')).join('')}`;
433
+ }
434
+ function utcIsoAxisLabel(value) {
435
+ const date = new Date(typeof value === 'number' ? value : Date.parse(value));
436
+ return Number.isNaN(date.getTime()) ? '' : date.toISOString();
437
+ }
438
+ function isDateTimeValue(value) {
439
+ return typeof value === 'string'
440
+ && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:\d{2})$/.test(value)
441
+ && !Number.isNaN(Date.parse(value));
442
+ }
443
+ function fieldOption(declaration, role) {
444
+ return declaration.options.find((option) => option.type === 'chartFieldOption' && option.role === role);
445
+ }
446
+ function optionText(declaration, type) {
447
+ const option = declaration.options.find(item => item.type === type);
448
+ return option && option.type === type ? option.value : undefined;
449
+ }
450
+ function isChartSeries(option) {
451
+ return option.type === 'chartSeriesOption';
452
+ }
453
+ function isRecord(value) {
454
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
455
+ }
456
+ function isUnknownRecord(value) {
457
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
458
+ }
459
+ function valueText(value) {
460
+ return value === null ? '' : String(value);
461
+ }
462
+ function cloneValue(value) {
463
+ if (Array.isArray(value))
464
+ return value.map(cloneValue);
465
+ if (isRecord(value))
466
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, cloneValue(item)]));
467
+ return value;
468
+ }
469
+ function deepFreeze(value) {
470
+ if (value && typeof value === 'object' && !Object.isFrozen(value)) {
471
+ for (const child of Object.values(value))
472
+ deepFreeze(child);
473
+ Object.freeze(value);
474
+ }
475
+ return value;
476
+ }