@aquera/nile-visualization 2.9.46 → 2.9.48

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.
@@ -21,6 +21,21 @@ export declare const MASTER_DETAIL_SERIES_TYPES: readonly ["area", "line", "spli
21
21
  export type MasterDetailSeriesType = (typeof MASTER_DETAIL_SERIES_TYPES)[number];
22
22
  export declare const MASTER_DETAIL_X_AXIS_TYPES: readonly ["datetime", "linear", "logarithmic", "category"];
23
23
  export type MasterDetailXAxisType = (typeof MASTER_DETAIL_X_AXIS_TYPES)[number];
24
+ /**
25
+ * @tag nile-master-detail-chart
26
+ * @summary A detail chart above an overview (master) strip; dragging on the strip zooms the detail.
27
+ *
28
+ * @event nile-chart-ready - Charts are created. `detail: { chart }`
29
+ * @event nile-chart-selection - The visible window changed. `detail: { min, max }`
30
+ * @event nile-master-detail-zoom - Zoomed in or back to the full range. `detail: { zoomed }`
31
+ *
32
+ * @csspart detail-container - The detail chart.
33
+ * @csspart master-container - The master (overview) strip.
34
+ * @csspart master-toggle - The show/hide button for the master strip (`master-collapsible`).
35
+ * @csspart navigator - The control bar under the master strip (`navigator-controls`).
36
+ * @csspart navigator-caption - The range readout in the control bar.
37
+ * @csspart navigator-hint - The hint text after the range readout.
38
+ */
24
39
  export declare class NileMasterDetailChart extends NileElement {
25
40
  static styles: CSSResultGroup;
26
41
  private _hc;
@@ -50,6 +65,17 @@ export declare class NileMasterDetailChart extends NileElement {
50
65
  masterType: MasterDetailSeriesType;
51
66
  masterOnly: boolean;
52
67
  masterCollapsible: boolean;
68
+ navigatorControls: boolean;
69
+ navigatorHint: string;
70
+ /**
71
+ * Opening window when `navigator-controls` is on and no `selection-min` / `selection-max` is set:
72
+ * a fraction of the range (`0.25`) or, on a datetime axis, a duration (`30m`, `12h`, `7d`, `2w`),
73
+ * ending at the latest data. Empty opens on the full range.
74
+ */
75
+ navigatorDefaultWindow: string;
76
+ private _window;
77
+ /** Re-renders only when the control bar is shown, so plain charts skip the extra update. */
78
+ private _trackWindow;
53
79
  height: string;
54
80
  masterHeight: string | number;
55
81
  xAxisType: MasterDetailXAxisType;
@@ -101,11 +127,34 @@ export declare class NileMasterDetailChart extends NileElement {
101
127
  private _zoomWindow;
102
128
  private _masterMinRange;
103
129
  private snapSelection;
130
+ /** Opening window from `navigatorDefaultWindow`, snapped to data points; `null` means use the full range. */
131
+ private _defaultWindow;
104
132
  private defaultSelection;
105
133
  private buildDetailOptions;
106
134
  private buildMasterOptions;
107
135
  resetZoom(): void;
108
136
  private applySelection;
137
+ /** Pan and zoom work on the log scale when the x-axis is logarithmic. */
138
+ private get _isLogX();
139
+ private _toScale;
140
+ private _fromScale;
141
+ /** Index of the data x nearest to `v` on the axis scale; `xs` is sorted. */
142
+ private _nearestIndex;
143
+ /** Clamps `[min, max]` into the data range keeping its width, then snaps both edges to data points. */
144
+ private _snapWindow;
145
+ private _windowIndices;
146
+ private _applyWindow;
147
+ private _setWindow;
148
+ /** Move the window by 25% of its width toward the start (`-1`) or end (`1`), at least one data point. */
149
+ panBy(direction: -1 | 1): void;
150
+ /** Scale the window around its centre: `0.5` zooms in, `2` zooms out; at least one data point per step. */
151
+ zoomBy(factor: number): void;
152
+ private get _navState();
153
+ private _onSliderChange;
154
+ private _formatX;
155
+ /** `title` names the inner button for assistive tech and shows the hover tooltip. */
156
+ private _navButton;
157
+ private _renderNavigator;
109
158
  private initCharts;
110
159
  private destroyCharts;
111
160
  render(): TemplateResult | typeof nothing;
@@ -1,13 +1,13 @@
1
1
  import { __decorate } from "tslib";
2
2
  import { customElement, property, query, state } from 'lit/decorators.js';
3
3
  import { html, nothing } from 'lit';
4
+ import { keyed } from 'lit/directives/keyed.js';
4
5
  import { getHighcharts } from '../internal/utils/highcharts-provider.js';
5
6
  import NileElement from '../internal/base/nile-element.js';
6
7
  import { applySeparatedChartConfig } from '../internal/utils/separated-chart-config.js';
7
8
  import { deepMerge } from '../internal/utils/deep-merge.js';
8
9
  import { withResponsiveDefaults } from '../internal/utils/responsive-defaults.js';
9
10
  import { styles } from './nile-master-detail-chart.css.js';
10
- import { LITE_TOOLTIP_MAX_WIDTH, limitLiteTooltipHeight } from '../internal/utils/lite-tooltip.js';
11
11
  export const MASTER_DETAIL_STACKING = ['normal', 'percent'];
12
12
  export const MASTER_DETAIL_SERIES_TYPES = [
13
13
  'area',
@@ -18,6 +18,21 @@ export const MASTER_DETAIL_SERIES_TYPES = [
18
18
  'bar',
19
19
  ];
20
20
  export const MASTER_DETAIL_X_AXIS_TYPES = ['datetime', 'linear', 'logarithmic', 'category'];
21
+ /**
22
+ * @tag nile-master-detail-chart
23
+ * @summary A detail chart above an overview (master) strip; dragging on the strip zooms the detail.
24
+ *
25
+ * @event nile-chart-ready - Charts are created. `detail: { chart }`
26
+ * @event nile-chart-selection - The visible window changed. `detail: { min, max }`
27
+ * @event nile-master-detail-zoom - Zoomed in or back to the full range. `detail: { zoomed }`
28
+ *
29
+ * @csspart detail-container - The detail chart.
30
+ * @csspart master-container - The master (overview) strip.
31
+ * @csspart master-toggle - The show/hide button for the master strip (`master-collapsible`).
32
+ * @csspart navigator - The control bar under the master strip (`navigator-controls`).
33
+ * @csspart navigator-caption - The range readout in the control bar.
34
+ * @csspart navigator-hint - The hint text after the range readout.
35
+ */
21
36
  let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
22
37
  constructor() {
23
38
  super(...arguments);
@@ -44,6 +59,15 @@ let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
44
59
  this.masterType = 'area';
45
60
  this.masterOnly = false;
46
61
  this.masterCollapsible = false;
62
+ this.navigatorControls = false;
63
+ this.navigatorHint = '';
64
+ /**
65
+ * Opening window when `navigator-controls` is on and no `selection-min` / `selection-max` is set:
66
+ * a fraction of the range (`0.25`) or, on a datetime axis, a duration (`30m`, `12h`, `7d`, `2w`),
67
+ * ending at the latest data. Empty opens on the full range.
68
+ */
69
+ this.navigatorDefaultWindow = '';
70
+ this._window = null;
47
71
  this.height = '600px';
48
72
  this.masterHeight = 100;
49
73
  this.xAxisType = 'datetime';
@@ -86,6 +110,12 @@ let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
86
110
  this.masterChart?.reflow();
87
111
  }
88
112
  }
113
+ /** Re-renders only when the control bar is shown, so plain charts skip the extra update. */
114
+ _trackWindow(min, max) {
115
+ this._window = { min, max };
116
+ if (this.navigatorControls)
117
+ this.requestUpdate();
118
+ }
89
119
  _cssLength(val) {
90
120
  return typeof val === 'number'
91
121
  ? (Number.isFinite(val) ? `${val}px` : '')
@@ -169,6 +199,7 @@ let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
169
199
  'yAxisMax',
170
200
  'selectionMin',
171
201
  'selectionMax',
202
+ 'navigatorDefaultWindow',
172
203
  'detailTitle',
173
204
  'masterTitle',
174
205
  'maskFill',
@@ -307,8 +338,44 @@ let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
307
338
  const hi = nearest(max);
308
339
  return { min: Math.min(lo, hi), max: Math.max(lo, hi) };
309
340
  }
341
+ /** Opening window from `navigatorDefaultWindow`, snapped to data points; `null` means use the full range. */
342
+ _defaultWindow() {
343
+ const v = String(this.navigatorDefaultWindow ?? '').trim();
344
+ const xs = this._allXs();
345
+ if (!v || xs.length < 2)
346
+ return null;
347
+ const dataMin = xs[0];
348
+ const dataMax = xs[xs.length - 1];
349
+ let min = null;
350
+ const fraction = Number(v);
351
+ if (Number.isFinite(fraction)) {
352
+ if (fraction > 0 && fraction < 1) {
353
+ const sMin = this._toScale(dataMin);
354
+ const sMax = this._toScale(dataMax);
355
+ min = this._fromScale(sMax - (sMax - sMin) * fraction);
356
+ }
357
+ }
358
+ else {
359
+ const m = /^(\d+(?:\.\d+)?)\s*(m|h|d|w)$/i.exec(v);
360
+ if (m && this.xAxisType === 'datetime') {
361
+ const unit = { m: 6e4, h: 36e5, d: 864e5, w: 6048e5 };
362
+ const width = Number(m[1]) * unit[m[2].toLowerCase()];
363
+ if (width > 0 && width < dataMax - dataMin)
364
+ min = dataMax - width;
365
+ }
366
+ }
367
+ if (min == null)
368
+ return null;
369
+ const w = this._snapWindow(min, dataMax);
370
+ return w ? { min: xs[w.lo], max: xs[w.hi] } : null;
371
+ }
310
372
  defaultSelection() {
311
373
  const { dataMin, dataMax } = this.dataExtremes();
374
+ if (this.navigatorControls && this.selectionMin == null && this.selectionMax == null) {
375
+ const win = this._defaultWindow();
376
+ if (win)
377
+ return win;
378
+ }
312
379
  return {
313
380
  min: this.selectionMin ?? dataMin,
314
381
  max: this.selectionMax ?? dataMax,
@@ -394,6 +461,7 @@ let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
394
461
  : { min: ext.min, max: ext.max };
395
462
  const win = self._zoomWindow(sel.min, sel.max);
396
463
  self.masterChart?.xAxis[0].setExtremes(win.min, win.max);
464
+ self._trackWindow(sel.min, sel.max);
397
465
  self.setZoomed(true);
398
466
  self.emit('nile-chart-selection', sel);
399
467
  return false;
@@ -467,6 +535,7 @@ let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
467
535
  if (!this.masterChart)
468
536
  return;
469
537
  const { dataMin, dataMax } = this.dataExtremes();
538
+ this._trackWindow(dataMin, dataMax);
470
539
  if (this.masterOnly) {
471
540
  this.masterChart.xAxis[0].setExtremes(dataMin, dataMax);
472
541
  }
@@ -481,6 +550,7 @@ let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
481
550
  this.emit('nile-chart-selection', { min: dataMin, max: dataMax });
482
551
  }
483
552
  applySelection(min, max) {
553
+ this._trackWindow(min, max);
484
554
  if (!this.masterChart)
485
555
  return;
486
556
  if (this.masterOnly) {
@@ -506,6 +576,199 @@ let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
506
576
  if (max < dataMax)
507
577
  xAxis.addPlotBand({ id: 'mask-after', from: max, to: dataMax, color: this.maskFill });
508
578
  }
579
+ /** Pan and zoom work on the log scale when the x-axis is logarithmic. */
580
+ get _isLogX() {
581
+ const xs = this._allXs();
582
+ return this.xAxisType === 'logarithmic' && xs.length > 0 && xs[0] > 0;
583
+ }
584
+ _toScale(v) {
585
+ return this._isLogX ? Math.log10(v) : v;
586
+ }
587
+ _fromScale(v) {
588
+ return this._isLogX ? Math.pow(10, v) : v;
589
+ }
590
+ /** Index of the data x nearest to `v` on the axis scale; `xs` is sorted. */
591
+ _nearestIndex(xs, v) {
592
+ let lo = 0;
593
+ let hi = xs.length - 1;
594
+ while (lo < hi) {
595
+ const mid = (lo + hi) >> 1;
596
+ if (xs[mid] < v)
597
+ lo = mid + 1;
598
+ else
599
+ hi = mid;
600
+ }
601
+ const sv = this._toScale(v);
602
+ if (lo > 0 && Math.abs(this._toScale(xs[lo - 1]) - sv) <= Math.abs(this._toScale(xs[lo]) - sv))
603
+ return lo - 1;
604
+ return lo;
605
+ }
606
+ /** Clamps `[min, max]` into the data range keeping its width, then snaps both edges to data points. */
607
+ _snapWindow(min, max) {
608
+ const xs = this._allXs();
609
+ if (xs.length < 2)
610
+ return null;
611
+ const sMin = this._toScale(xs[0]);
612
+ const sMax = this._toScale(xs[xs.length - 1]);
613
+ let a = this._toScale(min);
614
+ const width = Math.min(Math.max(this._toScale(max) - a, 0), sMax - sMin);
615
+ if (a < sMin)
616
+ a = sMin;
617
+ if (a + width > sMax)
618
+ a = sMax - width;
619
+ let lo = this._nearestIndex(xs, this._fromScale(a));
620
+ let hi = this._nearestIndex(xs, this._fromScale(a + width));
621
+ if (hi <= lo) {
622
+ if (lo < xs.length - 1)
623
+ hi = lo + 1;
624
+ else
625
+ lo = hi - 1;
626
+ }
627
+ return { lo, hi };
628
+ }
629
+ _windowIndices() {
630
+ const xs = this._allXs();
631
+ if (!this._window || xs.length < 2)
632
+ return null;
633
+ return { lo: this._nearestIndex(xs, this._window.min), hi: this._nearestIndex(xs, this._window.max) };
634
+ }
635
+ _applyWindow(lo, hi) {
636
+ const xs = this._allXs();
637
+ const min = xs[lo];
638
+ const max = xs[hi];
639
+ if (this.masterOnly) {
640
+ this._trackWindow(min, max);
641
+ this.masterChart?.xAxis[0].setExtremes(min, max);
642
+ }
643
+ else {
644
+ this.applySelection(min, max);
645
+ }
646
+ this.setZoomed(lo > 0 || hi < xs.length - 1);
647
+ this.emit('nile-chart-selection', { min, max });
648
+ }
649
+ _setWindow(min, max) {
650
+ const w = this._snapWindow(min, max);
651
+ if (w)
652
+ this._applyWindow(w.lo, w.hi);
653
+ }
654
+ /** Move the window by 25% of its width toward the start (`-1`) or end (`1`), at least one data point. */
655
+ panBy(direction) {
656
+ const cur = this._windowIndices();
657
+ if (!cur)
658
+ return;
659
+ const xs = this._allXs();
660
+ const a = this._toScale(xs[cur.lo]);
661
+ const b = this._toScale(xs[cur.hi]);
662
+ const d = (b - a) * 0.25 * direction;
663
+ let w = this._snapWindow(this._fromScale(a + d), this._fromScale(b + d));
664
+ if (w.lo === cur.lo && w.hi === cur.hi && cur.lo + direction >= 0 && cur.hi + direction <= xs.length - 1) {
665
+ w = { lo: cur.lo + direction, hi: cur.hi + direction };
666
+ }
667
+ this._applyWindow(w.lo, w.hi);
668
+ }
669
+ /** Scale the window around its centre: `0.5` zooms in, `2` zooms out; at least one data point per step. */
670
+ zoomBy(factor) {
671
+ const cur = this._windowIndices();
672
+ if (!cur)
673
+ return;
674
+ const xs = this._allXs();
675
+ const a = this._toScale(xs[cur.lo]);
676
+ const b = this._toScale(xs[cur.hi]);
677
+ const c = (a + b) / 2;
678
+ const half = ((b - a) * factor) / 2;
679
+ let w = this._snapWindow(this._fromScale(c - half), this._fromScale(c + half));
680
+ if (w.lo === cur.lo && w.hi === cur.hi) {
681
+ if (factor < 1 && cur.hi - cur.lo > 1)
682
+ w = { lo: cur.hi - cur.lo > 2 ? cur.lo + 1 : cur.lo, hi: cur.hi - 1 };
683
+ else if (factor > 1)
684
+ w = { lo: Math.max(0, cur.lo - 1), hi: Math.min(xs.length - 1, cur.hi + 1) };
685
+ }
686
+ this._applyWindow(w.lo, w.hi);
687
+ }
688
+ get _navState() {
689
+ const xs = this._allXs();
690
+ const last = xs.length - 1;
691
+ const cur = this._windowIndices() ?? { lo: 0, hi: last };
692
+ const sMin = this._toScale(xs[0]);
693
+ const room = this._toScale(xs[last]) - sMin - (this._toScale(xs[cur.hi]) - this._toScale(xs[cur.lo]));
694
+ const atStart = cur.lo <= 0;
695
+ const atEnd = cur.hi >= last;
696
+ return {
697
+ full: atStart && atEnd,
698
+ atStart,
699
+ atEnd,
700
+ atMinWidth: cur.hi - cur.lo <= 1,
701
+ sliderValue: room > 0 ? Math.round(((this._toScale(xs[cur.lo]) - sMin) / room) * 1000) : 0,
702
+ };
703
+ }
704
+ _onSliderChange(event) {
705
+ const cur = this._windowIndices();
706
+ if (!cur)
707
+ return;
708
+ const xs = this._allXs();
709
+ const sMin = this._toScale(xs[0]);
710
+ const width = this._toScale(xs[cur.hi]) - this._toScale(xs[cur.lo]);
711
+ const room = this._toScale(xs[xs.length - 1]) - sMin - width;
712
+ if (room <= 0)
713
+ return;
714
+ const value = Number(event.detail.value) || 0;
715
+ const a = sMin + room * (value / 1000);
716
+ this._setWindow(this._fromScale(a), this._fromScale(a + width));
717
+ }
718
+ _formatX(value) {
719
+ if (this.xAxisType === 'datetime' && this._hc) {
720
+ return this._hc.dateFormat('%Y-%m-%d %H:%M:%S', value);
721
+ }
722
+ if (this.xAxisType === 'category') {
723
+ const axis = (this.detailChart ?? this.masterChart)?.xAxis[0];
724
+ const name = axis?.categories?.[value] ?? axis?.names?.[value];
725
+ if (name != null && name !== '')
726
+ return String(name);
727
+ }
728
+ return String(value);
729
+ }
730
+ /** `title` names the inner button for assistive tech and shows the hover tooltip. */
731
+ _navButton(icon, label, disabled, onClick) {
732
+ return html `<nile-button
733
+ class="navigator-button"
734
+ variant="secondary"
735
+ size="sm"
736
+ aria-label=${label}
737
+ title=${label}
738
+ ?disabled=${disabled}
739
+ @click=${onClick}
740
+ ><nile-glyph name=${icon} method="stroke" size="16"></nile-glyph></nile-button>`;
741
+ }
742
+ _renderNavigator() {
743
+ const nav = this._navState;
744
+ const { dataMin, dataMax } = this.dataExtremes();
745
+ const w = this._window ?? { min: dataMin, max: dataMax };
746
+ const caption = `${this._formatX(w.min)} → ${this._formatX(w.max)}`;
747
+ return html `<div class="navigator" part="navigator" role="toolbar" aria-label="Chart navigation">
748
+ <div class="navigator-controls">
749
+ ${this._navButton('ng-chevron-left', 'Pan back', nav.atStart, () => this.panBy(-1))}
750
+ ${keyed(`${nav.sliderValue}:${nav.full}`, html `<nile-slider
751
+ class="navigator-slider"
752
+ aria-label="Visible range position"
753
+ .minValue=${0}
754
+ .maxValue=${1000}
755
+ .value=${nav.sliderValue}
756
+ ?disabled=${nav.full}
757
+ @nile-change-end=${this._onSliderChange}
758
+ ></nile-slider>`)}
759
+ ${this._navButton('ng-chevron-right', 'Pan forward', nav.atEnd, () => this.panBy(1))}
760
+ <div class="navigator-zoom">
761
+ ${this._navButton('ng-minus', 'Zoom out', nav.full, () => this.zoomBy(2))}
762
+ ${this._navButton('ng-plus', 'Zoom in', nav.atMinWidth, () => this.zoomBy(0.5))}
763
+ </div>
764
+ </div>
765
+ <div class="navigator-caption" part="navigator-caption" aria-live="polite">
766
+ ${caption}${this.navigatorHint
767
+ ? html ` · <span class="navigator-hint" part="navigator-hint">${this.navigatorHint}</span>`
768
+ : nothing}
769
+ </div>
770
+ </div>`;
771
+ }
509
772
  async initCharts() {
510
773
  if (this.loading)
511
774
  return;
@@ -556,7 +819,7 @@ let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
556
819
  ? null
557
820
  : html `<div class="detail-container ${masterHidden ? 'detail-container--grow' : ''}" part="detail-container"></div>
558
821
  ${this.masterCollapsible
559
- ? html `<nile-lite-tooltip .maxWidth=${LITE_TOOLTIP_MAX_WIDTH} @nile-show=${limitLiteTooltipHeight} content=${this.masterCollapsed ? 'Show master' : 'Hide master'} placement="top">
822
+ ? html `<nile-lite-tooltip content=${this.masterCollapsed ? 'Show master' : 'Hide master'} placement="top">
560
823
  <button
561
824
  type="button"
562
825
  class="master-toggle"
@@ -570,6 +833,7 @@ let NileMasterDetailChart = class NileMasterDetailChart extends NileElement {
570
833
  </nile-lite-tooltip>`
571
834
  : null}`}
572
835
  <div class="master-container ${masterHidden ? 'master-container--collapsed' : ''}" part="master-container"></div>
836
+ ${this.navigatorControls && this._allXs().length >= 2 ? this._renderNavigator() : nothing}
573
837
  </div>`;
574
838
  }
575
839
  };
@@ -619,6 +883,15 @@ __decorate([
619
883
  __decorate([
620
884
  property({ type: Boolean, attribute: 'master-collapsible' })
621
885
  ], NileMasterDetailChart.prototype, "masterCollapsible", void 0);
886
+ __decorate([
887
+ property({ type: Boolean, reflect: true, attribute: 'navigator-controls' })
888
+ ], NileMasterDetailChart.prototype, "navigatorControls", void 0);
889
+ __decorate([
890
+ property({ type: String, attribute: 'navigator-hint' })
891
+ ], NileMasterDetailChart.prototype, "navigatorHint", void 0);
892
+ __decorate([
893
+ property({ type: String, attribute: 'navigator-default-window' })
894
+ ], NileMasterDetailChart.prototype, "navigatorDefaultWindow", void 0);
622
895
  __decorate([
623
896
  property({ type: String })
624
897
  ], NileMasterDetailChart.prototype, "height", void 0);