@aquera/nile-visualization 2.9.29 → 2.9.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,551 @@
1
+ import { __decorate } from "tslib";
2
+ import { customElement, property, query } from 'lit/decorators.js';
3
+ import { html } from 'lit';
4
+ import { getHighcharts } from '../internal/highcharts-provider.js';
5
+ import NileElement from '../internal/nile-element.js';
6
+ import { applySeparatedChartConfig } from '../internal/separated-chart-config.js';
7
+ import { deepMerge } from '../internal/utils.js';
8
+ import { resolveColorPaletteColors } from '../internal/color-palettes/index.js';
9
+ import { styles } from './nile-forecast-chart.css.js';
10
+ export const FORECAST_NOW_LINE_COLOR = 'var(--nile-colors-primary-600, var(--ng-colors-fg-brand-primary-600))';
11
+ const AXIS_LABEL_COLOR = 'var(--nile-colors-neutral-500, var(--ng-colors-text-tertiary-600))';
12
+ const FORECAST_BAND_COLOR = 'color-mix(in srgb, var(--nile-colors-primary-600, var(--ng-componentcolors-utility-brand-600)) 6%, transparent)';
13
+ const AXIS_LABEL_FONT_SIZE = 'var(--nile-type-scale-2, var(--ng-font-size-text-xs))';
14
+ let NileForecastChart = class NileForecastChart extends NileElement {
15
+ constructor() {
16
+ super(...arguments);
17
+ this._hc = null;
18
+ this.chart = null;
19
+ this.resizeObserver = null;
20
+ this.config = null;
21
+ this.chartTitle = '';
22
+ this.chartSubtitle = '';
23
+ this.data = [];
24
+ this.forecastData = [];
25
+ this.options = {};
26
+ this.loading = false;
27
+ this.height = '400px';
28
+ this.forecastStart = null;
29
+ this.forecastDashStyle = 'Dot';
30
+ this.showNowLine = true;
31
+ this.nowLabel = 'Now';
32
+ this.nowLineColor = FORECAST_NOW_LINE_COLOR;
33
+ this.forecastBandLabel = '';
34
+ this.forecastBandColor = '';
35
+ this.xAxisTitle = '';
36
+ this.yAxisTitle = '';
37
+ this.secondaryAxisTitle = '';
38
+ this.secondaryAxisMin = null;
39
+ this.timezone = '';
40
+ this.zoom = true;
41
+ this.showLegend = true;
42
+ this.legend = null;
43
+ this.chartBg = '';
44
+ this.chartBorderRadius = '';
45
+ this.chartBorder = '';
46
+ this.chartShadow = '';
47
+ this.chartPadding = '';
48
+ this.chartMinHeight = '';
49
+ this.loadingText = 'Loading...';
50
+ this.loadingColor = '';
51
+ this.loadingFontSize = '';
52
+ this.loadingFontFamily = '';
53
+ this.loadingBg = '';
54
+ this.startFromSetForecast = false;
55
+ this.zoomed = false;
56
+ this.observedContainer = null;
57
+ this.initInFlight = false;
58
+ }
59
+ _syncCssVars() {
60
+ const set = (prop, val) => {
61
+ const s = typeof val === 'number'
62
+ ? (Number.isFinite(val) ? `${val}px` : '')
63
+ : String(val ?? '').trim();
64
+ if (s)
65
+ this.style.setProperty(prop, s);
66
+ else
67
+ this.style.removeProperty(prop);
68
+ };
69
+ set('--nile-chart-bg', this.chartBg);
70
+ set('--nile-chart-border-radius', this.chartBorderRadius);
71
+ set('--nile-chart-border', this.chartBorder);
72
+ set('--nile-chart-shadow', this.chartShadow);
73
+ set('--nile-chart-padding', this.chartPadding);
74
+ set('--nile-chart-min-height', this.chartMinHeight);
75
+ set('--nile-chart-loading-color', this.loadingColor);
76
+ set('--nile-chart-loading-font-size', this.loadingFontSize);
77
+ set('--nile-chart-loading-font-family', this.loadingFontFamily);
78
+ set('--nile-chart-loading-bg', this.loadingBg);
79
+ }
80
+ applyConfig(cfg) {
81
+ applySeparatedChartConfig(this, cfg);
82
+ }
83
+ connectedCallback() {
84
+ super.connectedCallback();
85
+ if (this.config)
86
+ this.applyConfig(this.config);
87
+ }
88
+ disconnectedCallback() {
89
+ super.disconnectedCallback();
90
+ this.destroyChart();
91
+ this.resizeObserver?.disconnect();
92
+ this.resizeObserver = null;
93
+ this.observedContainer = null;
94
+ }
95
+ firstUpdated() {
96
+ this._syncCssVars();
97
+ this.initChart();
98
+ this.syncResizeObserver();
99
+ }
100
+ updated(changedProperties) {
101
+ this.syncResizeObserver();
102
+ if (changedProperties.has('config') && this.config) {
103
+ this.applyConfig(this.config);
104
+ return;
105
+ }
106
+ const props = [
107
+ 'data',
108
+ 'forecastData',
109
+ 'chartTitle',
110
+ 'chartSubtitle',
111
+ 'options',
112
+ 'height',
113
+ 'forecastStart',
114
+ 'forecastDashStyle',
115
+ 'showNowLine',
116
+ 'nowLabel',
117
+ 'nowLineColor',
118
+ 'forecastBandLabel',
119
+ 'forecastBandColor',
120
+ 'xAxisTitle',
121
+ 'yAxisTitle',
122
+ 'secondaryAxisTitle',
123
+ 'secondaryAxisMin',
124
+ 'timezone',
125
+ 'showLegend',
126
+ 'legend',
127
+ 'zoom',
128
+ 'loading',
129
+ ];
130
+ const cssVarProps = ['chartBg', 'chartBorderRadius', 'chartBorder', 'chartShadow', 'chartPadding', 'chartMinHeight',
131
+ 'loadingText', 'loadingColor', 'loadingFontSize', 'loadingFontFamily', 'loadingBg'];
132
+ if (cssVarProps.some(p => changedProperties.has(p)))
133
+ this._syncCssVars();
134
+ const objectProps = new Set(['data', 'forecastData', 'options', 'legend']);
135
+ const reallyChanged = (p) => {
136
+ if (!changedProperties.has(p))
137
+ return false;
138
+ if (!objectProps.has(p))
139
+ return true;
140
+ const oldV = changedProperties.get(p);
141
+ const newV = this[p];
142
+ try {
143
+ return JSON.stringify(oldV) !== JSON.stringify(newV);
144
+ }
145
+ catch {
146
+ return oldV !== newV;
147
+ }
148
+ };
149
+ if (!props.some(reallyChanged))
150
+ return;
151
+ if (this.loading) {
152
+ this.destroyChart();
153
+ return;
154
+ }
155
+ if (this.chart) {
156
+ this.chart.update(this.buildOptions(), true, true);
157
+ }
158
+ else {
159
+ this.initChart();
160
+ }
161
+ }
162
+ setForecast(series, forecastStart) {
163
+ if (forecastStart !== undefined) {
164
+ this.forecastStart = forecastStart;
165
+ this.startFromSetForecast = forecastStart !== null;
166
+ }
167
+ this.forecastData = series;
168
+ }
169
+ clearForecast() {
170
+ this.forecastData = [];
171
+ if (this.startFromSetForecast) {
172
+ this.forecastStart = null;
173
+ this.startFromSetForecast = false;
174
+ }
175
+ }
176
+ setZoomed(value) {
177
+ if (this.zoomed === value)
178
+ return;
179
+ this.zoomed = value;
180
+ this.emit('nile-forecast-zoom', { zoomed: value });
181
+ }
182
+ xExtremes() {
183
+ let dataMin = Infinity;
184
+ let dataMax = -Infinity;
185
+ for (const { s } of this.resolveSeries().entries) {
186
+ for (const p of s.data ?? []) {
187
+ const x = p[0];
188
+ if (x < dataMin)
189
+ dataMin = x;
190
+ if (x > dataMax)
191
+ dataMax = x;
192
+ }
193
+ }
194
+ return dataMin === Infinity ? { dataMin: 0, dataMax: 0 } : { dataMin, dataMax };
195
+ }
196
+ resetZoom() {
197
+ if (!this.chart)
198
+ return;
199
+ this.chart.xAxis[0].setExtremes(undefined, undefined);
200
+ this.setZoomed(false);
201
+ const { dataMin, dataMax } = this.xExtremes();
202
+ this.emit('nile-chart-selection', { min: dataMin, max: dataMax });
203
+ }
204
+ syncResizeObserver() {
205
+ const target = this.chartContainer ?? null;
206
+ if (target === this.observedContainer)
207
+ return;
208
+ this.resizeObserver?.disconnect();
209
+ this.observedContainer = target;
210
+ if (!target)
211
+ return;
212
+ if (!this.resizeObserver) {
213
+ this.resizeObserver = new ResizeObserver(() => {
214
+ this.chart?.reflow();
215
+ });
216
+ }
217
+ this.resizeObserver.observe(target);
218
+ }
219
+ resolveSeries() {
220
+ const projected = this.forecastData ?? [];
221
+ const explicit = typeof this.forecastStart === 'number' && Number.isFinite(this.forecastStart)
222
+ ? this.forecastStart
223
+ : null;
224
+ if (!projected.length) {
225
+ const start = explicit ?? Date.now();
226
+ const hasForecast = this.data.some(s => s.data?.some(p => p[0] > start));
227
+ return {
228
+ entries: this.data.map(s => ({ s, projectedOnly: false })),
229
+ start,
230
+ hasForecast,
231
+ };
232
+ }
233
+ const byName = new Map(projected.map(s => [s.name, s]));
234
+ const entries = this.data.map(s => {
235
+ const add = byName.get(s.name);
236
+ if (!add?.data?.length)
237
+ return { s, projectedOnly: false };
238
+ byName.delete(s.name);
239
+ const { name: _name, data: _data, ...overrides } = add;
240
+ return {
241
+ s: { ...s, ...overrides, data: [...s.data, ...add.data].sort((a, b) => a[0] - b[0]) },
242
+ projectedOnly: false,
243
+ };
244
+ });
245
+ for (const s of byName.values())
246
+ entries.push({ s, projectedOnly: true });
247
+ let lastMeasured = null;
248
+ for (const s of this.data) {
249
+ if (!projected.some(p => p.name === s.name))
250
+ continue;
251
+ const point = s.data?.[s.data.length - 1];
252
+ if (point && (lastMeasured === null || point[0] > lastMeasured))
253
+ lastMeasured = point[0];
254
+ }
255
+ return { entries, start: explicit ?? lastMeasured ?? Date.now(), hasForecast: true };
256
+ }
257
+ lastTimestamp(all) {
258
+ let max = null;
259
+ for (const s of all) {
260
+ const point = s.data?.[s.data.length - 1];
261
+ if (point && (max === null || point[0] > max))
262
+ max = point[0];
263
+ }
264
+ return max;
265
+ }
266
+ buildOptions() {
267
+ const self = this;
268
+ const { entries, start, hasForecast } = this.resolveSeries();
269
+ const resolved = entries.map(e => e.s);
270
+ const hasSecondary = resolved.some(s => s.axis === 'secondary');
271
+ const bandEnd = this.lastTimestamp(resolved);
272
+ const showBand = hasForecast
273
+ && (this.forecastBandLabel || this.forecastBandColor)
274
+ && bandEnd !== null
275
+ && bandEnd > start;
276
+ const xAxis = {
277
+ type: 'datetime',
278
+ title: { text: this.xAxisTitle || undefined },
279
+ plotLines: this.showNowLine && hasForecast
280
+ ? [
281
+ {
282
+ value: start,
283
+ color: this.nowLineColor,
284
+ width: 2,
285
+ dashStyle: 'Dash',
286
+ zIndex: 5,
287
+ ...(this.nowLabel
288
+ ? { label: { text: this.nowLabel, rotation: 0, y: 20, style: { color: AXIS_LABEL_COLOR, fontSize: AXIS_LABEL_FONT_SIZE } } }
289
+ : {}),
290
+ },
291
+ ]
292
+ : [],
293
+ plotBands: showBand
294
+ ? [
295
+ {
296
+ from: start,
297
+ to: bandEnd,
298
+ color: this.forecastBandColor || FORECAST_BAND_COLOR,
299
+ ...(this.forecastBandLabel
300
+ ? { label: { text: this.forecastBandLabel, style: { color: AXIS_LABEL_COLOR, fontSize: AXIS_LABEL_FONT_SIZE } } }
301
+ : {}),
302
+ },
303
+ ]
304
+ : [],
305
+ };
306
+ const yAxis = [
307
+ { title: { text: this.yAxisTitle || undefined } },
308
+ ];
309
+ if (hasSecondary) {
310
+ yAxis.push({
311
+ title: { text: this.secondaryAxisTitle || undefined },
312
+ opposite: true,
313
+ gridLineWidth: 0,
314
+ ...(this.secondaryAxisMin !== null ? { min: this.secondaryAxisMin } : {}),
315
+ });
316
+ }
317
+ const series = entries.map(({ s, projectedOnly }) => {
318
+ const type = s.type ?? 'line';
319
+ const isColumn = type === 'column';
320
+ const isArea = type === 'area' || type === 'areaspline';
321
+ const forecastZone = isColumn
322
+ ? (s.forecastColor ? { color: s.forecastColor } : {})
323
+ : { dashStyle: s.forecastDashStyle ?? this.forecastDashStyle };
324
+ return {
325
+ type: type,
326
+ name: s.name,
327
+ data: [...(s.data ?? [])],
328
+ ...(s.color ? { color: s.color } : {}),
329
+ yAxis: s.axis === 'secondary' ? 1 : 0,
330
+ zIndex: isColumn ? 1 : 2,
331
+ zoneAxis: 'x',
332
+ zones: projectedOnly || !hasForecast ? [] : [{ value: start }, forecastZone],
333
+ ...(s.lineWidth !== undefined ? { lineWidth: s.lineWidth } : {}),
334
+ ...(s.valueSuffix ? { tooltip: { valueSuffix: s.valueSuffix } } : {}),
335
+ ...(isColumn
336
+ ? { borderWidth: 0 }
337
+ : { marker: { enabled: s.showMarkers ?? false } }),
338
+ ...(isArea ? { fillOpacity: s.fillOpacity ?? 0.2 } : {}),
339
+ ...(projectedOnly ? forecastZone : {}),
340
+ };
341
+ });
342
+ const paletteColors = resolveColorPaletteColors({ theme: 'default' }, entries.length);
343
+ return deepMerge({
344
+ ...(paletteColors ? { colors: paletteColors } : {}),
345
+ chart: {
346
+ ...(this.height ? { height: this.height } : {}),
347
+ ...(this.zoom
348
+ ? {
349
+ zoomType: 'x',
350
+ resetZoomButton: { theme: { display: 'none' } },
351
+ events: {
352
+ selection(event) {
353
+ const ext = event.xAxis?.[0];
354
+ if (!ext) {
355
+ self.setZoomed(false);
356
+ return false;
357
+ }
358
+ this.xAxis[0].setExtremes(ext.min, ext.max);
359
+ self.setZoomed(true);
360
+ self.emit('nile-chart-selection', { min: ext.min, max: ext.max });
361
+ return false;
362
+ },
363
+ },
364
+ }
365
+ : {}),
366
+ },
367
+ ...(this.timezone ? { time: { timezone: this.timezone } } : {}),
368
+ title: { text: this.chartTitle || undefined },
369
+ subtitle: { text: this.chartSubtitle || undefined },
370
+ legend: (() => { const { show, ...rest } = this.legend ?? {}; return { enabled: show ?? this.showLegend, ...rest }; })(),
371
+ tooltip: { shared: true, crosshairs: true },
372
+ xAxis,
373
+ yAxis,
374
+ plotOptions: {
375
+ series: {
376
+ cursor: 'pointer',
377
+ point: {
378
+ events: {
379
+ click() {
380
+ self.emit('nile-chart-click', {
381
+ point: this,
382
+ x: this.x,
383
+ value: this.y,
384
+ seriesName: this.series.name,
385
+ forecast: this.x > start,
386
+ });
387
+ },
388
+ },
389
+ },
390
+ },
391
+ },
392
+ series,
393
+ credits: { enabled: false },
394
+ }, this.options);
395
+ }
396
+ async initChart() {
397
+ if (this.loading || !this.chartContainer || this.chart || this.initInFlight)
398
+ return;
399
+ this.initInFlight = true;
400
+ try {
401
+ if (!this._hc) {
402
+ try {
403
+ this._hc = await getHighcharts();
404
+ }
405
+ catch (err) {
406
+ console.error('[nile-forecast-chart] Highcharts failed to load. Nothing rendered.', err);
407
+ this.emit('nile-chart-error', { error: err });
408
+ return;
409
+ }
410
+ }
411
+ if (!this.isConnected || this.loading || !this.chartContainer)
412
+ return;
413
+ this.chart = this._hc.chart(this.chartContainer, this.buildOptions());
414
+ this.emit('nile-chart-ready', { chart: this.chart });
415
+ }
416
+ finally {
417
+ this.initInFlight = false;
418
+ }
419
+ }
420
+ destroyChart() {
421
+ if (this.chart) {
422
+ this.chart.destroy();
423
+ this.chart = null;
424
+ }
425
+ this.setZoomed(false);
426
+ }
427
+ render() {
428
+ if (this.loading) {
429
+ return html `<div
430
+ class="chart-loading"
431
+ part="loading"
432
+ role="status"
433
+ aria-live="polite"
434
+ aria-busy="true"
435
+ style="height:${this.height}"
436
+ >${this.loadingText}</div>`;
437
+ }
438
+ return html `<div class="chart-container"></div>`;
439
+ }
440
+ };
441
+ NileForecastChart.styles = styles;
442
+ __decorate([
443
+ query('.chart-container')
444
+ ], NileForecastChart.prototype, "chartContainer", void 0);
445
+ __decorate([
446
+ property({ type: Object })
447
+ ], NileForecastChart.prototype, "config", void 0);
448
+ __decorate([
449
+ property({ type: String, attribute: 'chart-title' })
450
+ ], NileForecastChart.prototype, "chartTitle", void 0);
451
+ __decorate([
452
+ property({ type: String, attribute: 'chart-subtitle' })
453
+ ], NileForecastChart.prototype, "chartSubtitle", void 0);
454
+ __decorate([
455
+ property({ type: Array })
456
+ ], NileForecastChart.prototype, "data", void 0);
457
+ __decorate([
458
+ property({ type: Array, attribute: 'forecast-data' })
459
+ ], NileForecastChart.prototype, "forecastData", void 0);
460
+ __decorate([
461
+ property({ type: Object })
462
+ ], NileForecastChart.prototype, "options", void 0);
463
+ __decorate([
464
+ property({ type: Boolean, reflect: true })
465
+ ], NileForecastChart.prototype, "loading", void 0);
466
+ __decorate([
467
+ property({ type: String })
468
+ ], NileForecastChart.prototype, "height", void 0);
469
+ __decorate([
470
+ property({ type: Number, attribute: 'forecast-start' })
471
+ ], NileForecastChart.prototype, "forecastStart", void 0);
472
+ __decorate([
473
+ property({ type: String, attribute: 'forecast-dash-style' })
474
+ ], NileForecastChart.prototype, "forecastDashStyle", void 0);
475
+ __decorate([
476
+ property({ type: Boolean, attribute: 'show-now-line' })
477
+ ], NileForecastChart.prototype, "showNowLine", void 0);
478
+ __decorate([
479
+ property({ type: String, attribute: 'now-label' })
480
+ ], NileForecastChart.prototype, "nowLabel", void 0);
481
+ __decorate([
482
+ property({ type: String, attribute: 'now-line-color' })
483
+ ], NileForecastChart.prototype, "nowLineColor", void 0);
484
+ __decorate([
485
+ property({ type: String, attribute: 'forecast-band-label' })
486
+ ], NileForecastChart.prototype, "forecastBandLabel", void 0);
487
+ __decorate([
488
+ property({ type: String, attribute: 'forecast-band-color' })
489
+ ], NileForecastChart.prototype, "forecastBandColor", void 0);
490
+ __decorate([
491
+ property({ type: String, attribute: 'x-axis-title' })
492
+ ], NileForecastChart.prototype, "xAxisTitle", void 0);
493
+ __decorate([
494
+ property({ type: String, attribute: 'y-axis-title' })
495
+ ], NileForecastChart.prototype, "yAxisTitle", void 0);
496
+ __decorate([
497
+ property({ type: String, attribute: 'secondary-axis-title' })
498
+ ], NileForecastChart.prototype, "secondaryAxisTitle", void 0);
499
+ __decorate([
500
+ property({ type: Number, attribute: 'secondary-axis-min' })
501
+ ], NileForecastChart.prototype, "secondaryAxisMin", void 0);
502
+ __decorate([
503
+ property({ type: String })
504
+ ], NileForecastChart.prototype, "timezone", void 0);
505
+ __decorate([
506
+ property({ type: Boolean })
507
+ ], NileForecastChart.prototype, "zoom", void 0);
508
+ __decorate([
509
+ property({ type: Boolean, attribute: 'show-legend' })
510
+ ], NileForecastChart.prototype, "showLegend", void 0);
511
+ __decorate([
512
+ property({ type: Object })
513
+ ], NileForecastChart.prototype, "legend", void 0);
514
+ __decorate([
515
+ property({ type: String, attribute: 'chart-bg' })
516
+ ], NileForecastChart.prototype, "chartBg", void 0);
517
+ __decorate([
518
+ property({ type: String, attribute: 'chart-border-radius' })
519
+ ], NileForecastChart.prototype, "chartBorderRadius", void 0);
520
+ __decorate([
521
+ property({ type: String, attribute: 'chart-border' })
522
+ ], NileForecastChart.prototype, "chartBorder", void 0);
523
+ __decorate([
524
+ property({ type: String, attribute: 'chart-shadow' })
525
+ ], NileForecastChart.prototype, "chartShadow", void 0);
526
+ __decorate([
527
+ property({ type: String, attribute: 'chart-padding' })
528
+ ], NileForecastChart.prototype, "chartPadding", void 0);
529
+ __decorate([
530
+ property({ type: String, attribute: 'chart-min-height' })
531
+ ], NileForecastChart.prototype, "chartMinHeight", void 0);
532
+ __decorate([
533
+ property({ type: String, attribute: 'loading-text' })
534
+ ], NileForecastChart.prototype, "loadingText", void 0);
535
+ __decorate([
536
+ property({ type: String, attribute: 'loading-color' })
537
+ ], NileForecastChart.prototype, "loadingColor", void 0);
538
+ __decorate([
539
+ property({ type: String, attribute: 'loading-font-size' })
540
+ ], NileForecastChart.prototype, "loadingFontSize", void 0);
541
+ __decorate([
542
+ property({ type: String, attribute: 'loading-font-family' })
543
+ ], NileForecastChart.prototype, "loadingFontFamily", void 0);
544
+ __decorate([
545
+ property({ type: String, attribute: 'loading-bg' })
546
+ ], NileForecastChart.prototype, "loadingBg", void 0);
547
+ NileForecastChart = __decorate([
548
+ customElement('nile-forecast-chart')
549
+ ], NileForecastChart);
550
+ export { NileForecastChart };
551
+ //# sourceMappingURL=nile-forecast-chart.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aquera/nile-visualization",
3
- "version": "2.9.29",
3
+ "version": "2.9.30",
4
4
  "description": "A visualization Library for the Nile Design System",
5
5
  "license": "MIT",
6
6
  "author": "Aquera Inc",
@@ -23,6 +23,7 @@
23
23
  "./nile-master-detail-chart": "./dist/src/nile-master-detail-chart/index.js",
24
24
  "./nile-dumbbell-chart": "./dist/src/nile-dumbbell-chart/index.js",
25
25
  "./nile-fan-chart": "./dist/src/nile-fan-chart/index.js",
26
+ "./nile-forecast-chart": "./dist/src/nile-forecast-chart/index.js",
26
27
  "./nile-spline-chart": "./dist/src/nile-spline-chart/index.js",
27
28
  "./nile-donut-chart": "./dist/src/nile-donut-chart/index.js",
28
29
  "./nile-scatter-chart": "./dist/src/nile-scatter-chart/index.js",