@aquera/nile-visualization 2.9.9 → 2.9.11

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.
@@ -140,11 +140,18 @@ let NileKpiChart = class NileKpiChart extends NileElement {
140
140
  * Set by nile-chart: skip host border/shadow (variant card/gauge) so the parent chart-card is the only frame.
141
141
  */
142
142
  this.embedInNileChart = false;
143
+ // ── Chart sizing ─────────────────────────────────────────────────────────
144
+ this._lastSparkSize = { w: 0, h: 0 };
145
+ this._lastGaugeSize = { w: 0, h: 0 };
146
+ this._gaugeRafScheduled = false;
143
147
  // ── Sparkline mousemove tooltip ──────────────────────────────────────────
144
148
  this._onSparklineMouseMove = (e) => {
145
149
  const chart = this.sparklineChart;
146
150
  if (!chart || !this.sparklineContainer)
147
151
  return;
152
+ // Pie uses Highcharts' native tooltip — the x-snap logic below is meaningless for radial layouts.
153
+ if (this.sparklineType === 'pie')
154
+ return;
148
155
  const series = chart.series[0];
149
156
  if (!series?.points?.length)
150
157
  return;
@@ -192,10 +199,29 @@ let NileKpiChart = class NileKpiChart extends NileElement {
192
199
  const rect = el.getBoundingClientRect();
193
200
  this._showTip(this.label ?? '', rect.left + rect.width / 2, rect.top);
194
201
  };
202
+ this._onTrendEnter = (e) => {
203
+ const el = e.currentTarget;
204
+ // Only show a tooltip when the trend is actually clipped/ellipsized.
205
+ const hasEllipsis = getComputedStyle(el).textOverflow === 'ellipsis';
206
+ if (!hasEllipsis)
207
+ return;
208
+ if (el.scrollWidth <= el.clientWidth + 2)
209
+ return;
210
+ const rect = el.getBoundingClientRect();
211
+ const dir = this.trendDirection;
212
+ const arrow = dir === 'up' ? '▲' : dir === 'down' ? '▼' : '–';
213
+ const pct = this.trendValue !== null ? `${Math.abs(this.trendValue).toFixed(1)}%` : '';
214
+ const text = `${arrow} ${pct}${this.trendLabel ? ' ' + this.trendLabel : ''}`.trim();
215
+ this._showTip(text, rect.left + rect.width / 2, rect.top);
216
+ };
195
217
  this._onGaugeEnter = (e) => {
196
218
  const rect = e.currentTarget.getBoundingClientRect();
197
219
  this._showTip(this.getTooltipContent(), rect.left + rect.width / 2, rect.top + rect.height / 2);
198
220
  };
221
+ this._onTrendKeydown = (e) => {
222
+ if (e.key === 'Escape')
223
+ this._hideTip();
224
+ };
199
225
  this._onTipLeave = () => {
200
226
  this._hideTip();
201
227
  };
@@ -623,29 +649,83 @@ let NileKpiChart = class NileKpiChart extends NileElement {
623
649
  if (gaugeProps.some(p => changedProperties.has(p))) {
624
650
  if (this.gaugeChart) {
625
651
  this.gaugeChart.update(this.buildGaugeOptions(), true, true);
652
+ requestAnimationFrame(() => this.syncGaugeChartSize());
626
653
  }
627
654
  else {
628
655
  this.initGauge();
629
656
  }
630
657
  }
631
658
  }
632
- // ── Chart sizing ─────────────────────────────────────────────────────────
659
+ _scheduleGaugeSync() {
660
+ if (this._gaugeRafScheduled)
661
+ return;
662
+ this._gaugeRafScheduled = true;
663
+ requestAnimationFrame(() => {
664
+ this._gaugeRafScheduled = false;
665
+ this._syncGaugeNow();
666
+ });
667
+ }
668
+ _syncGaugeNow() {
669
+ if (!this.gaugeContainer || !this.gaugeSlot || this.variant !== 'gauge')
670
+ return;
671
+ // Hide the gauge container while measuring the slot
672
+ const prevDisplay = this.gaugeContainer.style.display;
673
+ this.gaugeContainer.style.display = 'none';
674
+ const rect = this.gaugeSlot.getBoundingClientRect();
675
+ this.gaugeContainer.style.display = prevDisplay;
676
+ const w = Math.max(1, Math.round(rect.width));
677
+ const h = Math.max(1, Math.round(rect.height));
678
+ const size = Math.max(1, Math.min(w, h));
679
+ // writing its size re-fires the observer.
680
+ if (size === this._lastGaugeSize.w && this.gaugeChart)
681
+ return;
682
+ this._lastGaugeSize = { w: size, h: size };
683
+ this.gaugeContainer.style.width = `${size}px`;
684
+ this.gaugeContainer.style.height = `${size}px`;
685
+ if (!this._hc)
686
+ return;
687
+ if (this.gaugeChart) {
688
+ this.gaugeChart.setSize(size, size, false);
689
+ }
690
+ else {
691
+ this.gaugeChart = this._hc.chart(this.gaugeContainer, this.buildGaugeOptions());
692
+ this.gaugeChart.setSize(size, size, false);
693
+ }
694
+ }
695
+ syncGaugeChartSize() {
696
+ // always measure after the layout
697
+ this._scheduleGaugeSync();
698
+ }
633
699
  syncSparklineChartSize() {
634
700
  if (!this.sparklineChart || !this.sparklineContainer)
635
701
  return;
636
702
  const rect = this.sparklineContainer.getBoundingClientRect();
703
+ const w = Math.max(1, Math.round(rect.width));
637
704
  const h = Math.max(22, Math.round(rect.height));
638
- this.sparklineChart.setSize(null, h, false);
705
+ if (w === this._lastSparkSize.w && h === this._lastSparkSize.h)
706
+ return;
707
+ this._lastSparkSize = { w, h };
708
+ // Rebuild the chart
709
+ this.destroySparkline();
710
+ if (!this._hc)
711
+ return;
712
+ this.sparklineChart = this._hc.chart(this.sparklineContainer, this.buildSparklineOptions());
713
+ this.sparklineChart.setSize(w, h, false);
714
+ this.sparklineContainer.addEventListener('mousemove', this._onSparklineMouseMove);
715
+ this.sparklineContainer.addEventListener('mouseleave', this._onSparklineMouseLeave);
639
716
  }
640
717
  setupResizeObserver() {
641
718
  this.resizeObserver = new ResizeObserver(() => {
642
719
  this.syncSparklineChartSize();
643
- this.gaugeChart?.reflow();
720
+ this.syncGaugeChartSize();
644
721
  });
722
+ this.resizeObserver.observe(this);
645
723
  if (this.sparklineContainer)
646
724
  this.resizeObserver.observe(this.sparklineContainer);
647
725
  if (this.gaugeContainer)
648
726
  this.resizeObserver.observe(this.gaugeContainer);
727
+ if (this.gaugeSlot)
728
+ this.resizeObserver.observe(this.gaugeSlot);
649
729
  }
650
730
  // ── Chart options ────────────────────────────────────────────────────────
651
731
  buildSparklineOptions() {
@@ -653,8 +733,37 @@ let NileKpiChart = class NileKpiChart extends NileElement {
653
733
  const lineWidth = this.sparklineLineWidth;
654
734
  const seriesType = this.sparklineType;
655
735
  const topAlpha = Math.round(Math.min(1, Math.max(0, this.sparklineFillOpacity)) * 255).toString(16).padStart(2, '0');
736
+ // Pie variant — a compact pie with no slice labels / connectors.
737
+ if (seriesType === 'pie') {
738
+ const defaults = {
739
+ chart: { type: 'pie', height: this.sparklineHeight, margin: [0, 0, 0, 0], backgroundColor: 'transparent' },
740
+ title: { text: undefined },
741
+ subtitle: { text: undefined },
742
+ legend: { enabled: false },
743
+ tooltip: {
744
+ enabled: true,
745
+ outside: true,
746
+ pointFormat: '<b>{point.y}</b> ({point.percentage:.1f}%)',
747
+ },
748
+ plotOptions: {
749
+ pie: {
750
+ size: '100%',
751
+ borderWidth: 0,
752
+ dataLabels: { enabled: false },
753
+ states: { hover: { enabled: true } },
754
+ allowPointSelect: false,
755
+ },
756
+ },
757
+ series: [{ type: 'pie', data: this.sparkline.map((v, i) => ({ y: v, name: `Slice ${i + 1}` })) }],
758
+ credits: { enabled: false },
759
+ };
760
+ return { ...defaults, ...this.options };
761
+ }
656
762
  const plotKey = seriesType;
657
- const fillColor = seriesType === 'area'
763
+ const isAreaLike = seriesType === 'area' || seriesType === 'areaspline';
764
+ const isBarLike = seriesType === 'column' || seriesType === 'bar';
765
+ const isScatter = seriesType === 'scatter';
766
+ const fillColor = isAreaLike
658
767
  ? {
659
768
  fillColor: {
660
769
  linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1 },
@@ -665,6 +774,28 @@ let NileKpiChart = class NileKpiChart extends NileElement {
665
774
  },
666
775
  }
667
776
  : {};
777
+ const seriesPlotOptions = isBarLike
778
+ ? {
779
+ color: brandColor,
780
+ borderWidth: 0,
781
+ pointPadding: 0.05,
782
+ groupPadding: 0.05,
783
+ states: { hover: { enabled: false } },
784
+ }
785
+ : isScatter
786
+ ? {
787
+ color: brandColor,
788
+ marker: { enabled: true, radius: Math.max(2, lineWidth) },
789
+ states: { hover: { enabled: false } },
790
+ }
791
+ : {
792
+ marker: { enabled: this.sparklineMarkers },
793
+ lineWidth,
794
+ lineColor: brandColor,
795
+ color: brandColor,
796
+ ...fillColor,
797
+ states: { hover: { lineWidth } },
798
+ };
668
799
  const defaults = {
669
800
  chart: { type: seriesType, height: this.sparklineHeight, margin: [2, 0, 2, 0], backgroundColor: 'transparent' },
670
801
  title: { text: undefined },
@@ -674,13 +805,7 @@ let NileKpiChart = class NileKpiChart extends NileElement {
674
805
  legend: { enabled: false },
675
806
  tooltip: { enabled: false },
676
807
  plotOptions: {
677
- [plotKey]: {
678
- marker: { enabled: this.sparklineMarkers },
679
- lineWidth,
680
- lineColor: brandColor,
681
- ...fillColor,
682
- states: { hover: { lineWidth } },
683
- },
808
+ [plotKey]: seriesPlotOptions,
684
809
  },
685
810
  series: [{ type: seriesType, data: this.sparkline }],
686
811
  credits: { enabled: false },
@@ -692,17 +817,22 @@ let NileKpiChart = class NileKpiChart extends NileElement {
692
817
  const trackColor = this.gaugeTrackColor || '#E5E7EB';
693
818
  const innerRadius = this.gaugeInnerRadius || '80%';
694
819
  const outerRadius = this.gaugeOuterRadius || '100%';
695
- const labelFontSize = this.formatCssLength(this.gaugeLabelFontSize) ?? '28px';
820
+ const maxLabelFontPx = (() => {
821
+ const raw = this.gaugeLabelFontSize;
822
+ if (typeof raw === 'number' && Number.isFinite(raw))
823
+ return raw;
824
+ const m = String(raw).match(/^(\d+(?:\.\d+)?)/);
825
+ return m ? Number(m[1]) : 28;
826
+ })();
696
827
  const labelColor = this.gaugeLabelColor || '#101828';
697
828
  const labelFontWeight = this.gaugeLabelFontWeight ?? 600;
698
- const displayValue = typeof this.value !== 'undefined' && this.value !== ''
699
- ? (typeof this.value === 'number' ? this.value : this.gaugeValue)
700
- : this.gaugeValue;
829
+ const parsedValue = this.parseNumericValue(this.value);
830
+ const displayValue = parsedValue != null ? parsedValue : this.gaugeValue;
701
831
  return {
702
832
  chart: {
703
833
  type: 'solidgauge',
704
- height: this.gaugeHeight,
705
834
  margin: [0, 0, 0, 0],
835
+ spacing: [0, 0, 0, 0],
706
836
  backgroundColor: 'transparent',
707
837
  },
708
838
  title: { text: undefined },
@@ -735,13 +865,17 @@ let NileKpiChart = class NileKpiChart extends NileElement {
735
865
  dataLabels: {
736
866
  enabled: true,
737
867
  borderWidth: 0,
738
- y: this.gaugeLabelYOffset,
868
+ y: 0,
869
+ verticalAlign: 'middle',
739
870
  useHTML: true,
740
871
  formatter: (() => {
741
872
  const self = this;
742
873
  return function () {
743
874
  const fmtResult = self.formatValue(this.y ?? 0);
744
- return `<div style="text-align:center"><span style="font-size:${labelFontSize};font-weight:${labelFontWeight};color:${labelColor}">${self.prefix}${fmtResult.display}${self.suffix}</span></div>`;
875
+ const chart = this.series?.chart;
876
+ const size = chart ? Math.min(chart.plotWidth || chart.chartWidth || 160, chart.plotHeight || chart.chartHeight || 160) : 160;
877
+ const fontPx = Math.max(10, Math.min(maxLabelFontPx, Math.round(size * 0.18)));
878
+ return `<div style="text-align:center;line-height:1;"><span style="font-size:${fontPx}px;font-weight:${labelFontWeight};color:${labelColor}">${self.prefix}${fmtResult.display}${self.suffix}</span></div>`;
745
879
  };
746
880
  })(),
747
881
  },
@@ -771,7 +905,11 @@ let NileKpiChart = class NileKpiChart extends NileElement {
771
905
  this._hc = await getHighcharts();
772
906
  this.destroySparkline();
773
907
  this.sparklineChart = this._hc.chart(this.sparklineContainer, this.buildSparklineOptions());
774
- requestAnimationFrame(() => this.syncSparklineChartSize());
908
+ // Force a reflow after the next layout pass
909
+ requestAnimationFrame(() => {
910
+ this.syncSparklineChartSize();
911
+ requestAnimationFrame(() => this.syncSparklineChartSize());
912
+ });
775
913
  this.sparklineContainer.addEventListener('mousemove', this._onSparklineMouseMove);
776
914
  this.sparklineContainer.addEventListener('mouseleave', this._onSparklineMouseLeave);
777
915
  this.emit('nile-chart-ready', { chart: this.sparklineChart });
@@ -783,6 +921,13 @@ let NileKpiChart = class NileKpiChart extends NileElement {
783
921
  this._hc = await getHighcharts();
784
922
  this.destroyGauge();
785
923
  this.gaugeChart = this._hc.chart(this.gaugeContainer, this.buildGaugeOptions());
924
+ // Force the chart to match the container's measured size
925
+ this._lastGaugeSize = { w: 0, h: 0 };
926
+ this.syncGaugeChartSize();
927
+ requestAnimationFrame(() => {
928
+ this.syncGaugeChartSize();
929
+ requestAnimationFrame(() => this.syncGaugeChartSize());
930
+ });
786
931
  this.emit('nile-chart-ready', { chart: this.gaugeChart });
787
932
  }
788
933
  destroySparkline() {
@@ -811,8 +956,19 @@ let NileKpiChart = class NileKpiChart extends NileElement {
811
956
  return undefined;
812
957
  const dir = this.trendDirection;
813
958
  const formatted = Math.abs(this.trendValue).toFixed(1) + '%';
959
+ const trendAriaLabel = `Trend ${dir === 'up' ? 'up' : dir === 'down' ? 'down' : 'neutral'} ${formatted}${this.trendLabel ? ' ' + this.trendLabel : ''}`.trim();
814
960
  return html `
815
- <span class="kpi-trend kpi-trend--${dir}">
961
+ <span
962
+ class="kpi-trend kpi-trend--${dir}"
963
+ tabindex="0"
964
+ role="button"
965
+ aria-label=${trendAriaLabel}
966
+ @mouseenter=${this._onTrendEnter}
967
+ @mouseleave=${this._onTipLeave}
968
+ @focus=${this._onTrendEnter}
969
+ @blur=${this._onTipLeave}
970
+ @keydown=${this._onTrendKeydown}
971
+ >
816
972
  <span class="kpi-trend-arrow">
817
973
  ${dir === 'up'
818
974
  ? html `<svg viewBox="0 0 12 12"><path d="M6 2.5l4 5H2z"/></svg>`
@@ -820,7 +976,7 @@ let NileKpiChart = class NileKpiChart extends NileElement {
820
976
  ? html `<svg viewBox="0 0 12 12"><path d="M6 9.5l4-5H2z"/></svg>`
821
977
  : html `<svg viewBox="0 0 12 12"><path d="M2 6.5h8" stroke="currentColor" stroke-width="1.5" fill="none"/></svg>`}
822
978
  </span>
823
- ${formatted}${this.trendLabel ? html ` <span>${this.trendLabel}</span>` : nothing}
979
+ <span class="kpi-trend-value">${formatted}</span>${this.trendLabel ? html ` <span class="kpi-trend-label">${this.trendLabel}</span>` : nothing}
824
980
  </span>
825
981
  `;
826
982
  }
@@ -837,24 +993,40 @@ let NileKpiChart = class NileKpiChart extends NileElement {
837
993
  <div class="kpi ${isGauge ? 'kpi--gauge' : ''}">
838
994
  ${this.label ? html `<p
839
995
  class="kpi-label"
996
+ tabindex="0"
840
997
  @mouseenter=${this._onLabelEnter}
841
998
  @mouseleave=${this._onTipLeave}
999
+ @focus=${this._onLabelEnter}
1000
+ @blur=${this._onTipLeave}
1001
+ @keydown=${this._onTrendKeydown}
842
1002
  >${this.label}</p>` : nothing}
843
1003
 
844
1004
  ${isGauge ? html `
845
- <div
846
- class="kpi-gauge-container"
847
- @mouseenter=${this._onGaugeEnter}
848
- @mouseleave=${this._onTipLeave}
849
- ></div>
1005
+ <div class="kpi-gauge-slot">
1006
+ <div
1007
+ class="kpi-gauge-container"
1008
+ tabindex="0"
1009
+ role="img"
1010
+ aria-label=${this.getTooltipContent()}
1011
+ @mouseenter=${this._onGaugeEnter}
1012
+ @mouseleave=${this._onTipLeave}
1013
+ @focus=${this._onGaugeEnter}
1014
+ @blur=${this._onTipLeave}
1015
+ @keydown=${this._onTrendKeydown}
1016
+ ></div>
1017
+ </div>
850
1018
  ` : nothing}
851
1019
 
852
1020
  <div class="kpi-value-row">
853
1021
  ${!isGauge ? html `
854
1022
  <h2
855
1023
  class="kpi-value"
1024
+ tabindex=${showTooltip ? '0' : nothing}
856
1025
  @mouseenter=${showTooltip ? this._onValueEnter : nothing}
857
1026
  @mouseleave=${showTooltip ? this._onTipLeave : nothing}
1027
+ @focus=${showTooltip ? this._onValueEnter : nothing}
1028
+ @blur=${showTooltip ? this._onTipLeave : nothing}
1029
+ @keydown=${showTooltip ? this._onTrendKeydown : nothing}
858
1030
  >
859
1031
  ${this.prefix ? html `<span class="kpi-prefix">${this.prefix}</span>` : nothing}${displayValue}${this.suffix ? html `<span class="kpi-suffix">${this.suffix}</span>` : nothing}
860
1032
  </h2>
@@ -866,8 +1038,12 @@ let NileKpiChart = class NileKpiChart extends NileElement {
866
1038
 
867
1039
  ${this.description ? html `<p
868
1040
  class="kpi-description"
1041
+ tabindex="0"
869
1042
  @mouseenter=${this._onDescEnter}
870
1043
  @mouseleave=${this._onTipLeave}
1044
+ @focus=${this._onDescEnter}
1045
+ @blur=${this._onTipLeave}
1046
+ @keydown=${this._onTrendKeydown}
871
1047
  >${this.description}</p>` : nothing}
872
1048
 
873
1049
  ${this.sparkline.length && !isGauge ? html `<div class="kpi-sparkline"></div>` : nothing}
@@ -882,6 +1058,9 @@ __decorate([
882
1058
  __decorate([
883
1059
  query('.kpi-gauge-container')
884
1060
  ], NileKpiChart.prototype, "gaugeContainer", void 0);
1061
+ __decorate([
1062
+ query('.kpi-gauge-slot')
1063
+ ], NileKpiChart.prototype, "gaugeSlot", void 0);
885
1064
  __decorate([
886
1065
  property({ type: Object })
887
1066
  ], NileKpiChart.prototype, "config", void 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aquera/nile-visualization",
3
- "version": "2.9.9",
3
+ "version": "2.9.11",
4
4
  "description": "A visualization Library for the Nile Design System",
5
5
  "license": "MIT",
6
6
  "author": "Aquera Inc",