@redvars/peacock 3.16.0 → 3.17.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.
Files changed (147) hide show
  1. package/dist/assets/styles.css +1 -1
  2. package/dist/assets/styles.css.map +1 -1
  3. package/dist/assets/theme.js +19 -19
  4. package/dist/assets/tokens.css +1 -1
  5. package/dist/assets/tokens.css.map +1 -1
  6. package/dist/card-content.js +8 -7
  7. package/dist/card-content.js.map +1 -1
  8. package/dist/card.js +142 -98
  9. package/dist/card.js.map +1 -1
  10. package/dist/{chart-bar-D5dGFJfK.js → cartesian-chart-DgE4USyC.js} +529 -312
  11. package/dist/cartesian-chart-DgE4USyC.js.map +1 -0
  12. package/dist/chart-area.js +78 -0
  13. package/dist/chart-area.js.map +1 -0
  14. package/dist/chart-bar.js +283 -213
  15. package/dist/chart-bar.js.map +1 -1
  16. package/dist/{transform-DSwFSqzD.js → chart-base-C2qRMF48.js} +771 -102
  17. package/dist/chart-base-C2qRMF48.js.map +1 -0
  18. package/dist/chart-doughnut.js +55 -287
  19. package/dist/chart-doughnut.js.map +1 -1
  20. package/dist/chart-line.js +626 -0
  21. package/dist/chart-line.js.map +1 -0
  22. package/dist/chart-pie.js +637 -195
  23. package/dist/chart-pie.js.map +1 -1
  24. package/dist/code-editor.js +704 -131
  25. package/dist/code-editor.js.map +1 -1
  26. package/dist/code-group.js +188 -0
  27. package/dist/code-group.js.map +1 -0
  28. package/dist/code-highlighter.js +895 -257
  29. package/dist/code-highlighter.js.map +1 -1
  30. package/dist/custom-elements-jsdocs.json +1675 -222
  31. package/dist/custom-elements.json +13351 -3970
  32. package/dist/image.js +5 -1
  33. package/dist/image.js.map +1 -1
  34. package/dist/index.js +8 -7
  35. package/dist/index.js.map +1 -1
  36. package/dist/loader-full.js +1 -1
  37. package/dist/loader.js +9 -3
  38. package/dist/loader.js.map +1 -1
  39. package/dist/{is-dark-mode-DOcaw4Yq.js → observe-theme-change-DCifz1uv.js} +2 -6
  40. package/dist/observe-theme-change-DCifz1uv.js.map +1 -0
  41. package/dist/path-CJ0Arqcy.js +166 -0
  42. package/dist/path-CJ0Arqcy.js.map +1 -0
  43. package/dist/snackbar.js +89 -34
  44. package/dist/snackbar.js.map +1 -1
  45. package/dist/src/__internal/chart/cartesian-chart.d.ts +80 -0
  46. package/dist/src/__internal/chart/chart-base.d.ts +113 -0
  47. package/dist/src/__internal/chart/paths.d.ts +5 -0
  48. package/dist/src/__internal/chart/types.d.ts +42 -0
  49. package/dist/src/card/card-content.d.ts +2 -3
  50. package/dist/src/card/card.d.ts +31 -17
  51. package/dist/src/chart-area/chart-area.d.ts +54 -0
  52. package/dist/src/chart-area/index.d.ts +1 -0
  53. package/dist/src/chart-bar/chart-bar.d.ts +44 -33
  54. package/dist/src/chart-bar/index.d.ts +0 -1
  55. package/dist/src/chart-doughnut/chart-doughnut.d.ts +33 -39
  56. package/dist/src/chart-line/chart-line.d.ts +69 -0
  57. package/dist/src/chart-line/index.d.ts +1 -0
  58. package/dist/src/chart-pie/chart-pie.d.ts +63 -28
  59. package/dist/src/code-editor/code-editor.d.ts +52 -13
  60. package/dist/src/code-editor/editor-theme.d.ts +60 -0
  61. package/dist/src/code-highlighter/code-group.d.ts +52 -0
  62. package/dist/src/code-highlighter/code-highlighter.d.ts +122 -15
  63. package/dist/src/code-highlighter/index.d.ts +1 -0
  64. package/dist/src/code-highlighter/line-ranges.d.ts +24 -0
  65. package/dist/src/code-highlighter/local.d.ts +4 -1
  66. package/dist/src/code-highlighter/token-colors.d.ts +8 -0
  67. package/dist/src/index.d.ts +4 -2
  68. package/dist/src/snackbar/snackbar.d.ts +13 -1
  69. package/dist/src/toolbar/toolbar.d.ts +56 -51
  70. package/dist/table.js +9 -4
  71. package/dist/table.js.map +1 -1
  72. package/dist/test/chart.test.d.ts +1 -0
  73. package/dist/test/code-highlighter.test.d.ts +1 -0
  74. package/dist/token-colors-0TFHWbcq.js +81 -0
  75. package/dist/token-colors-0TFHWbcq.js.map +1 -0
  76. package/dist/toolbar.js +195 -184
  77. package/dist/toolbar.js.map +1 -1
  78. package/dist/tsconfig.tsbuildinfo +1 -1
  79. package/package.json +1 -2
  80. package/readme.md +7 -6
  81. package/scss/_code-block.scss +139 -0
  82. package/scss/tokens.css +24 -0
  83. package/src/__internal/chart/cartesian-chart.ts +404 -0
  84. package/src/__internal/chart/chart-base.scss +252 -0
  85. package/src/__internal/chart/chart-base.ts +474 -0
  86. package/src/__internal/chart/paths.ts +28 -0
  87. package/src/__internal/chart/types.ts +49 -0
  88. package/src/card/card-content.ts +12 -9
  89. package/src/card/card.scss +66 -54
  90. package/src/card/card.ts +109 -73
  91. package/src/chart-area/chart-area.ts +82 -0
  92. package/src/chart-area/index.ts +1 -0
  93. package/src/chart-bar/chart-bar.ts +336 -250
  94. package/src/chart-bar/index.ts +0 -1
  95. package/src/chart-doughnut/chart-doughnut.ts +62 -262
  96. package/src/chart-doughnut/demo/index.html +14 -22
  97. package/src/chart-line/chart-line.scss +25 -0
  98. package/src/chart-line/chart-line.ts +346 -0
  99. package/src/chart-line/index.ts +1 -0
  100. package/src/chart-pie/chart-pie.scss +35 -16
  101. package/src/chart-pie/chart-pie.ts +282 -188
  102. package/src/chart-pie/demo/index.html +13 -21
  103. package/src/code-editor/code-editor.scss +132 -36
  104. package/src/code-editor/code-editor.ts +308 -88
  105. package/src/code-editor/demo/index.html +39 -12
  106. package/src/code-editor/editor-theme.ts +213 -0
  107. package/src/code-highlighter/code-group.scss +34 -0
  108. package/src/code-highlighter/code-group.ts +173 -0
  109. package/src/code-highlighter/code-highlighter.scss +226 -111
  110. package/src/code-highlighter/code-highlighter.ts +554 -199
  111. package/src/code-highlighter/demo/index.html +68 -4
  112. package/src/code-highlighter/index.ts +1 -0
  113. package/src/code-highlighter/line-ranges.ts +81 -0
  114. package/src/code-highlighter/local.ts +9 -3
  115. package/src/code-highlighter/token-colors.ts +42 -0
  116. package/src/index.ts +4 -2
  117. package/src/loader.ts +8 -2
  118. package/src/snackbar/demo/index.html +39 -4
  119. package/src/snackbar/snackbar.scss +27 -10
  120. package/src/snackbar/snackbar.ts +80 -37
  121. package/src/table/table.scss +7 -1
  122. package/src/table/table.ts +4 -3
  123. package/src/toolbar/toolbar-colors.scss +6 -7
  124. package/src/toolbar/toolbar.scss +35 -106
  125. package/src/toolbar/toolbar.ts +180 -80
  126. package/dist/array-D5vjT2Xm.js +0 -14
  127. package/dist/array-D5vjT2Xm.js.map +0 -1
  128. package/dist/chart-bar-D5dGFJfK.js.map +0 -1
  129. package/dist/chart-stacked-bar.js +0 -403
  130. package/dist/chart-stacked-bar.js.map +0 -1
  131. package/dist/copy-to-clipboard-upGx-Ueo.js +0 -39
  132. package/dist/copy-to-clipboard-upGx-Ueo.js.map +0 -1
  133. package/dist/is-dark-mode-DOcaw4Yq.js.map +0 -1
  134. package/dist/pie-Dz0IDiPt.js +0 -537
  135. package/dist/pie-Dz0IDiPt.js.map +0 -1
  136. package/dist/pierre-dark-DFWl0m-C.js +0 -4
  137. package/dist/pierre-dark-DFWl0m-C.js.map +0 -1
  138. package/dist/pierre-light-BEkAPImt.js +0 -4
  139. package/dist/pierre-light-BEkAPImt.js.map +0 -1
  140. package/dist/src/chart-bar/chart-stacked-bar.d.ts +0 -78
  141. package/dist/src/code-highlighter/constants.d.ts +0 -1
  142. package/dist/transform-DSwFSqzD.js.map +0 -1
  143. package/src/chart-bar/chart-bar.scss +0 -58
  144. package/src/chart-bar/chart-stacked-bar.ts +0 -398
  145. package/src/chart-doughnut/chart-donut.scss +0 -37
  146. package/src/code-highlighter/constants.ts +0 -117
  147. /package/dist/test/{chart-bar.test.d.ts → card.test.d.ts} +0 -0
@@ -0,0 +1,252 @@
1
+ @use '../../../scss/mixin';
2
+
3
+ @include mixin.base-styles;
4
+
5
+ // Charts fill their container. The explicit width keeps them from shrinking to their
6
+ // (not yet measured) content inside flex and grid parents.
7
+ :host {
8
+ display: block;
9
+ width: 100%;
10
+ min-width: 0;
11
+ color: var(--color-on-surface);
12
+
13
+ --_chart-grid-color: var(--chart-grid-color, var(--color-outline-variant));
14
+ --_chart-axis-color: var(--chart-axis-color, var(--color-on-surface-variant));
15
+ --_chart-dim-opacity: var(--chart-dim-opacity, 0.4);
16
+ --_chart-cursor-color: var(--chart-cursor-color, var(--color-on-surface));
17
+ }
18
+
19
+ .chart {
20
+ display: flex;
21
+ flex-direction: column;
22
+ gap: var(--spacing-200);
23
+ width: 100%;
24
+ }
25
+
26
+ .plot {
27
+ position: relative;
28
+ width: 100%;
29
+ outline: none;
30
+ touch-action: pan-y;
31
+ border-radius: var(--shape-corner-small);
32
+
33
+ &:focus-visible {
34
+ @include mixin.focus-ring(var(--color-primary));
35
+ }
36
+ }
37
+
38
+ .chart-svg {
39
+ display: block;
40
+ overflow: visible;
41
+ }
42
+
43
+ // ── Axes and grid
44
+
45
+ .gridline {
46
+ stroke: var(--_chart-grid-color);
47
+ stroke-dasharray: 3 3;
48
+ shape-rendering: crispEdges;
49
+ }
50
+
51
+ .baseline {
52
+ stroke: var(--_chart-grid-color);
53
+ shape-rendering: crispEdges;
54
+ }
55
+
56
+ .axis-label {
57
+ @include mixin.get-typography('label-medium');
58
+ fill: var(--_chart-axis-color);
59
+ }
60
+
61
+ .value-label {
62
+ @include mixin.get-typography('label-small');
63
+ fill: var(--color-on-surface);
64
+ }
65
+
66
+ .cursor {
67
+ fill: var(--_chart-cursor-color);
68
+ stroke: var(--_chart-cursor-color);
69
+ opacity: 0;
70
+ pointer-events: none;
71
+ transition: opacity var(--duration-short2) var(--easing-standard);
72
+
73
+ &.is-visible {
74
+ opacity: 0.08;
75
+ }
76
+ }
77
+
78
+ .cursor-line.is-visible {
79
+ opacity: 0.4;
80
+ }
81
+
82
+ .mark {
83
+ transition: opacity var(--duration-short3) var(--easing-standard);
84
+
85
+ &.is-dimmed {
86
+ opacity: var(--_chart-dim-opacity);
87
+ }
88
+ }
89
+
90
+ // ── Tooltip
91
+
92
+ .tooltip {
93
+ position: absolute;
94
+ z-index: 1;
95
+ min-width: 8rem;
96
+ padding: var(--spacing-150) var(--spacing-200);
97
+ border-radius: var(--shape-corner-small);
98
+ background: var(--color-surface-container-high);
99
+ color: var(--color-on-surface);
100
+ box-shadow: var(--elevation-level2);
101
+ pointer-events: none;
102
+ transform: translateY(-50%);
103
+ white-space: nowrap;
104
+
105
+ &.flip {
106
+ transform: translate(-100%, -50%);
107
+ }
108
+ }
109
+
110
+ .tooltip-title {
111
+ @include mixin.get-typography('label-large');
112
+ margin-bottom: var(--spacing-100);
113
+ }
114
+
115
+ .tooltip-row {
116
+ display: flex;
117
+ align-items: center;
118
+ gap: var(--spacing-150);
119
+ @include mixin.get-typography('body-small');
120
+
121
+ & + & {
122
+ margin-top: var(--spacing-050);
123
+ }
124
+ }
125
+
126
+ .tooltip-label {
127
+ flex: 1;
128
+ color: var(--color-on-surface-variant);
129
+ }
130
+
131
+ .tooltip-value {
132
+ font-variant-numeric: tabular-nums;
133
+ font-weight: 500;
134
+ }
135
+
136
+ .swatch {
137
+ flex: none;
138
+ width: 0.625rem;
139
+ height: 0.625rem;
140
+ border-radius: var(--shape-corner-full);
141
+ }
142
+
143
+ // ── Legend
144
+
145
+ .legend {
146
+ display: flex;
147
+ flex-wrap: wrap;
148
+ justify-content: center;
149
+ gap: var(--spacing-050) var(--spacing-200);
150
+ }
151
+
152
+ .legend-item {
153
+ @include mixin.reset-button-styles;
154
+ @include mixin.get-typography('label-medium');
155
+ display: inline-flex;
156
+ align-items: center;
157
+ gap: var(--spacing-100);
158
+ padding: var(--spacing-050) var(--spacing-100);
159
+ border-radius: var(--shape-corner-small);
160
+ color: var(--color-on-surface);
161
+ cursor: pointer;
162
+
163
+ &:hover {
164
+ background: var(--color-surface-container-high);
165
+ }
166
+
167
+ &:focus-visible {
168
+ @include mixin.focus-ring(var(--color-primary));
169
+ }
170
+
171
+ &.is-hidden {
172
+ color: var(--color-on-surface-variant);
173
+ text-decoration: line-through;
174
+
175
+ .swatch {
176
+ opacity: var(--_chart-dim-opacity);
177
+ }
178
+ }
179
+ }
180
+
181
+ // ── States
182
+
183
+ .empty {
184
+ display: flex;
185
+ align-items: center;
186
+ justify-content: center;
187
+ @include mixin.get-typography('body-medium');
188
+ color: var(--color-on-surface-variant);
189
+ border: 1px dashed var(--color-outline-variant);
190
+ border-radius: var(--shape-corner-medium);
191
+ }
192
+
193
+ .skeleton {
194
+ position: relative;
195
+ overflow: hidden;
196
+ }
197
+
198
+ .skeleton-bars {
199
+ display: flex;
200
+ align-items: flex-end;
201
+ gap: var(--spacing-200);
202
+ padding: var(--spacing-200) 0 0;
203
+ border-bottom: 1px solid var(--color-outline-variant);
204
+
205
+ span {
206
+ flex: 1;
207
+ border-radius: var(--shape-corner-extra-small) var(--shape-corner-extra-small) 0 0;
208
+ background: var(--color-surface-container-highest);
209
+ animation: chart-pulse 1.6s var(--easing-standard) infinite;
210
+ }
211
+ }
212
+
213
+ .skeleton-circle {
214
+ display: flex;
215
+ align-items: center;
216
+ justify-content: center;
217
+
218
+ span {
219
+ height: 100%;
220
+ aspect-ratio: 1;
221
+ border-radius: 50%;
222
+ background: var(--color-surface-container-highest);
223
+ animation: chart-pulse 1.6s var(--easing-standard) infinite;
224
+ }
225
+ }
226
+
227
+ @keyframes chart-pulse {
228
+ 50% {
229
+ opacity: 0.5;
230
+ }
231
+ }
232
+
233
+ .sr-only {
234
+ position: absolute;
235
+ width: 1px;
236
+ height: 1px;
237
+ margin: -1px;
238
+ padding: 0;
239
+ overflow: hidden;
240
+ clip: rect(0 0 0 0);
241
+ white-space: nowrap;
242
+ border: 0;
243
+ }
244
+
245
+ @media (prefers-reduced-motion: reduce) {
246
+ .skeleton span,
247
+ .mark,
248
+ .cursor {
249
+ animation: none;
250
+ transition: none;
251
+ }
252
+ }
@@ -0,0 +1,474 @@
1
+ import {
2
+ html,
3
+ LitElement,
4
+ nothing,
5
+ PropertyValues,
6
+ SVGTemplateResult,
7
+ TemplateResult,
8
+ } from 'lit';
9
+ import { property, query, state } from 'lit/decorators.js';
10
+ import { classMap } from 'lit/directives/class-map.js';
11
+ import { styleMap } from 'lit/directives/style-map.js';
12
+ import { BaseType, easeCubicOut, Selection } from 'd3';
13
+ import type {
14
+ ChartSelectDetail,
15
+ ChartSeries,
16
+ ChartTooltipModel,
17
+ ChartValueFormatter,
18
+ } from './types.js';
19
+
20
+ /** Number of `--color-chart-n` tokens series cycle through. */
21
+ export const CHART_PALETTE_SIZE = 8;
22
+
23
+ /** Default motion duration for enter and data-change animations. */
24
+ const DURATION = 450;
25
+
26
+ /** Tooltip offset from the pointer or anchor. */
27
+ const TOOLTIP_OFFSET = 12;
28
+
29
+ /** The palette color for the series or slice at `index`. */
30
+ export function chartColor(index: number): string {
31
+ return `var(--color-chart-${(index % CHART_PALETTE_SIZE) + 1})`;
32
+ }
33
+
34
+ const compactFormat = new Intl.NumberFormat(undefined, {
35
+ notation: 'compact',
36
+ maximumFractionDigits: 1,
37
+ });
38
+
39
+ /**
40
+ * Shared behaviour for every Peacock chart: container sizing, palette, value formatting,
41
+ * tooltip, legend, keyboard navigation, the accessible data table, and loading and empty states.
42
+ * Subclasses draw the marks with d3 into the SVG returned by `renderSvgContent()`.
43
+ */
44
+ export abstract class ChartBase extends LitElement {
45
+ // ── Properties
46
+
47
+ /** Height of the plot in pixels. The width follows the container. */
48
+ @property({ type: Number, reflect: true }) height: number = 300;
49
+
50
+ /** Accessible name of the chart, read by screen readers. */
51
+ @property({ type: String }) label: string = '';
52
+
53
+ /** Shows a skeleton while data loads. */
54
+ @property({ type: Boolean, reflect: true }) loading: boolean = false;
55
+
56
+ /** Shows the legend even for a single series. */
57
+ @property({ type: Boolean, attribute: 'show-legend' }) showLegend: boolean =
58
+ false;
59
+
60
+ /** Hides the legend. */
61
+ @property({ type: Boolean, attribute: 'hide-legend' }) hideLegend: boolean =
62
+ false;
63
+
64
+ /** Hides the tooltip. */
65
+ @property({ type: Boolean, attribute: 'hide-tooltip' }) hideTooltip: boolean =
66
+ false;
67
+
68
+ /** Turns off enter and update animations. They are also off when the user prefers reduced motion. */
69
+ @property({ type: Boolean, attribute: 'disable-animation' })
70
+ disableAnimation: boolean = false;
71
+
72
+ /** Formats values in tooltips, value labels and axis ticks. Defaults to the locale number format, compact on axes. */
73
+ @property({ attribute: false }) valueFormatter?: ChartValueFormatter;
74
+
75
+ // ── State
76
+
77
+ /** Keys of series or slices switched off in the legend. */
78
+ @state() protected hiddenKeys: Set<string> = new Set();
79
+
80
+ /** Index of the hovered or focused category or slice, -1 for none. */
81
+ @state() protected activeIndex: number = -1;
82
+
83
+ /** Key of the series highlighted from the legend. */
84
+ @state() protected activeSeries?: string;
85
+
86
+ /** Pointer position the tooltip follows; undefined places it at the item's anchor. */
87
+ @state() protected pointer?: { x: number; y: number };
88
+
89
+ /** Width of the plot, measured from the container. */
90
+ @state() protected plotWidth: number = 0;
91
+
92
+ /** Text for the polite live region, set during keyboard navigation. */
93
+ @state() private announcement: string = '';
94
+
95
+ // ── Queries
96
+
97
+ @query('svg.chart-svg') protected svgElement?: SVGSVGElement;
98
+
99
+ @query('.plot') protected plotElement?: HTMLElement;
100
+
101
+ // ── Private fields
102
+
103
+ /** Watches the host width. */
104
+ private resizeObserver?: ResizeObserver;
105
+
106
+ /** Whether the marks have been drawn once. */
107
+ private hasDrawn = false;
108
+
109
+ // ── Lifecycle
110
+
111
+ connectedCallback() {
112
+ super.connectedCallback();
113
+ this.resizeObserver = new ResizeObserver(entries => {
114
+ const width = Math.floor(entries[0]?.contentRect.width ?? 0);
115
+ if (width !== this.plotWidth) this.plotWidth = width;
116
+ });
117
+ this.resizeObserver.observe(this);
118
+ }
119
+
120
+ disconnectedCallback() {
121
+ super.disconnectedCallback();
122
+ this.resizeObserver?.disconnect();
123
+ this.resizeObserver = undefined;
124
+ }
125
+
126
+ updated(changed: PropertyValues) {
127
+ super.updated(changed);
128
+ if (!this.svgElement || this.plotWidth <= 0) return;
129
+
130
+ const drawKeys = this.drawProperties();
131
+ const needsDraw = drawKeys.some(key => changed.has(key));
132
+ if (needsDraw) {
133
+ const resizeOnly = [...changed.keys()].every(
134
+ key => key === 'plotWidth' || !drawKeys.includes(key as string),
135
+ );
136
+ // The first draw animates the marks in; later resizes snap.
137
+ this.draw((!resizeOnly || !this.hasDrawn) && this.motionEnabled());
138
+ this.hasDrawn = true;
139
+ this.drawHighlight();
140
+ } else if (changed.has('activeIndex') || changed.has('activeSeries')) {
141
+ this.drawHighlight();
142
+ }
143
+ }
144
+
145
+ // ── Protected API for subclasses
146
+
147
+ /** True when there is something to plot. */
148
+ protected abstract hasData(): boolean;
149
+
150
+ /** Every series or slice, including hidden ones, in legend order. */
151
+ protected abstract getSeries(): ChartSeries[];
152
+
153
+ /** Number of categories or slices keyboard navigation moves through. */
154
+ protected abstract getItemCount(): number;
155
+
156
+ /** Draws the marks; `animate` is false for resizes and when motion is off. */
157
+ protected abstract draw(animate: boolean): void;
158
+
159
+ /** Applies hover and focus state (dimming, cursor, active dots) without redrawing. */
160
+ protected abstract drawHighlight(): void;
161
+
162
+ /** The item under a point in plot coordinates. */
163
+ protected abstract itemAtPoint(x: number, y: number): number;
164
+
165
+ /** Tooltip content and default anchor for an item. */
166
+ protected abstract getTooltip(index: number): ChartTooltipModel | undefined;
167
+
168
+ /** Detail of `chart-select` for an item. */
169
+ protected abstract getSelectDetail(index: number): ChartSelectDetail;
170
+
171
+ /** The static SVG layers d3 draws into. */
172
+ protected abstract renderSvgContent(): SVGTemplateResult;
173
+
174
+ /** Rows of the visually hidden data table. */
175
+ protected abstract renderTable(): TemplateResult;
176
+
177
+ /** Property names that trigger a redraw when they change. */
178
+ protected drawProperties(): string[] {
179
+ return ['height', 'plotWidth', 'hiddenKeys', 'loading', 'valueFormatter'];
180
+ }
181
+
182
+ /** Whether the legend shows. */
183
+ protected legendVisible(): boolean {
184
+ if (this.hideLegend) return false;
185
+ return this.showLegend || this.getSeries().length > 1;
186
+ }
187
+
188
+ /** Width and height of the SVG. */
189
+ protected svgSize(): { width: number; height: number } {
190
+ return { width: this.plotWidth, height: this.height };
191
+ }
192
+
193
+ /** Motion duration in ms, 0 when animation is off. */
194
+ protected motionEnabled(): boolean {
195
+ if (this.disableAnimation) return false;
196
+ return !window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
197
+ }
198
+
199
+ /**
200
+ * Returns a transition of `selection` when `animate` is true, else the selection itself, so
201
+ * callers set end values once. Transitions share the attr/style API used here.
202
+ */
203
+ protected motion<E extends BaseType, D>(
204
+ selection: Selection<E, D, BaseType, unknown>,
205
+ animate: boolean,
206
+ ): Selection<E, D, BaseType, unknown> {
207
+ if (!animate) {
208
+ selection.interrupt();
209
+ return selection;
210
+ }
211
+ return selection
212
+ .transition()
213
+ .duration(DURATION)
214
+ .ease(easeCubicOut) as unknown as Selection<E, D, BaseType, unknown>;
215
+ }
216
+
217
+ /** Formats a value for tooltips and value labels. */
218
+ protected formatValue(value: number): string {
219
+ return this.valueFormatter
220
+ ? this.valueFormatter(value)
221
+ : value.toLocaleString();
222
+ }
223
+
224
+ /** Formats an axis tick. */
225
+ protected formatTick(value: number): string {
226
+ return this.valueFormatter
227
+ ? this.valueFormatter(value)
228
+ : compactFormat.format(value);
229
+ }
230
+
231
+ /** Whether a series or slice is switched off in the legend. */
232
+ protected isHidden(key: string): boolean {
233
+ return this.hiddenKeys.has(key);
234
+ }
235
+
236
+ /** Extra content layered over the plot, such as the doughnut centre. */
237
+ protected renderOverlay(): TemplateResult | typeof nothing {
238
+ return nothing;
239
+ }
240
+
241
+ /** Skeleton shown while `loading`. */
242
+ protected renderSkeleton(): TemplateResult {
243
+ const bars = [55, 80, 40, 95, 65, 75, 50];
244
+ return html`<div
245
+ class="skeleton skeleton-bars"
246
+ style=${styleMap({ height: `${this.height}px` })}
247
+ aria-hidden="true"
248
+ >
249
+ ${bars.map(
250
+ h => html`<span style=${styleMap({ height: `${h}%` })}></span>`,
251
+ )}
252
+ </div>`;
253
+ }
254
+
255
+ // ── Private methods
256
+
257
+ private pointFromEvent(event: PointerEvent | MouseEvent) {
258
+ const rect = this.plotElement?.getBoundingClientRect();
259
+ if (!rect) return undefined;
260
+ return { x: event.clientX - rect.left, y: event.clientY - rect.top };
261
+ }
262
+
263
+ private handlePointerMove = (event: PointerEvent) => {
264
+ const point = this.pointFromEvent(event);
265
+ if (!point) return;
266
+ const index = this.itemAtPoint(point.x, point.y);
267
+ this.activeIndex = index;
268
+ this.pointer = index >= 0 ? point : undefined;
269
+ };
270
+
271
+ private handlePointerLeave = () => {
272
+ this.activeIndex = -1;
273
+ this.pointer = undefined;
274
+ };
275
+
276
+ private handleClick = (event: MouseEvent) => {
277
+ const point = this.pointFromEvent(event);
278
+ if (!point) return;
279
+ const index = this.itemAtPoint(point.x, point.y);
280
+ if (index >= 0) this.select(index);
281
+ };
282
+
283
+ private handleKeyDown = (event: KeyboardEvent) => {
284
+ const count = this.getItemCount();
285
+ if (!count) return;
286
+ let next = this.activeIndex;
287
+ switch (event.key) {
288
+ case 'ArrowRight':
289
+ case 'ArrowDown':
290
+ next = next < 0 ? 0 : Math.min(next + 1, count - 1);
291
+ break;
292
+ case 'ArrowLeft':
293
+ case 'ArrowUp':
294
+ next = next < 0 ? count - 1 : Math.max(next - 1, 0);
295
+ break;
296
+ case 'Home':
297
+ next = 0;
298
+ break;
299
+ case 'End':
300
+ next = count - 1;
301
+ break;
302
+ case 'Escape':
303
+ next = -1;
304
+ break;
305
+ case 'Enter':
306
+ case ' ':
307
+ if (this.activeIndex >= 0) {
308
+ event.preventDefault();
309
+ this.select(this.activeIndex);
310
+ }
311
+ return;
312
+ default:
313
+ return;
314
+ }
315
+ event.preventDefault();
316
+ this.pointer = undefined;
317
+ this.activeIndex = next;
318
+ this.announce(next);
319
+ };
320
+
321
+ private handleBlur = () => {
322
+ this.activeIndex = -1;
323
+ this.pointer = undefined;
324
+ };
325
+
326
+ private announce(index: number) {
327
+ const model = index >= 0 ? this.getTooltip(index) : undefined;
328
+ this.announcement = model
329
+ ? `${model.title}. ${model.rows.map(row => `${row.label} ${row.value}`).join(', ')}`
330
+ : '';
331
+ }
332
+
333
+ private select(index: number) {
334
+ this.dispatchEvent(
335
+ new CustomEvent<ChartSelectDetail>('chart-select', {
336
+ bubbles: true,
337
+ composed: true,
338
+ detail: this.getSelectDetail(index),
339
+ }),
340
+ );
341
+ }
342
+
343
+ private toggleSeries(key: string) {
344
+ const next = new Set(this.hiddenKeys);
345
+ if (next.has(key)) next.delete(key);
346
+ else next.add(key);
347
+ this.hiddenKeys = next;
348
+ }
349
+
350
+ // ── Render helpers
351
+
352
+ private renderTooltip() {
353
+ if (this.hideTooltip || this.activeIndex < 0) return nothing;
354
+ const model = this.getTooltip(this.activeIndex);
355
+ if (!model || !model.rows.length) return nothing;
356
+ const x = this.pointer?.x ?? model.x;
357
+ const y = this.pointer?.y ?? model.y;
358
+ const flip = x > this.plotWidth * 0.6;
359
+ return html`<div
360
+ class=${classMap({ tooltip: true, flip })}
361
+ part="tooltip"
362
+ aria-hidden="true"
363
+ style=${styleMap({
364
+ left: `${flip ? x - TOOLTIP_OFFSET : x + TOOLTIP_OFFSET}px`,
365
+ top: `${y}px`,
366
+ })}
367
+ >
368
+ ${model.title
369
+ ? html`<div class="tooltip-title">${model.title}</div>`
370
+ : nothing}
371
+ ${model.rows.map(
372
+ row =>
373
+ html`<div class="tooltip-row">
374
+ <span
375
+ class="swatch"
376
+ style=${styleMap({ background: row.color })}
377
+ ></span>
378
+ <span class="tooltip-label">${row.label}</span>
379
+ <span class="tooltip-value">${row.value}</span>
380
+ </div>`,
381
+ )}
382
+ </div>`;
383
+ }
384
+
385
+ private renderLegend() {
386
+ if (!this.legendVisible()) return nothing;
387
+ return html`<div class="legend" part="legend">
388
+ ${this.getSeries().map(
389
+ series =>
390
+ html`<button
391
+ type="button"
392
+ class=${classMap({
393
+ 'legend-item': true,
394
+ 'is-hidden': this.isHidden(series.key),
395
+ })}
396
+ aria-pressed=${this.isHidden(series.key) ? 'false' : 'true'}
397
+ @click=${() => this.toggleSeries(series.key)}
398
+ @pointerenter=${() => {
399
+ this.activeSeries = series.key;
400
+ }}
401
+ @pointerleave=${() => {
402
+ this.activeSeries = undefined;
403
+ }}
404
+ >
405
+ <span
406
+ class="swatch"
407
+ style=${styleMap({ background: series.color })}
408
+ ></span>
409
+ <span>${series.label}</span>
410
+ </button>`,
411
+ )}
412
+ </div>`;
413
+ }
414
+
415
+ private renderPlot() {
416
+ const { width, height } = this.svgSize();
417
+ const name = this.label || 'Chart';
418
+ return html`<div
419
+ class="plot"
420
+ part="plot"
421
+ style=${styleMap({ height: `${this.height}px` })}
422
+ tabindex="0"
423
+ role="group"
424
+ aria-roledescription="chart"
425
+ aria-label="${name}. Use the arrow keys to explore the data."
426
+ @pointermove=${this.handlePointerMove}
427
+ @pointerleave=${this.handlePointerLeave}
428
+ @click=${this.handleClick}
429
+ @keydown=${this.handleKeyDown}
430
+ @blur=${this.handleBlur}
431
+ >
432
+ <svg class="chart-svg" aria-hidden="true" width=${width} height=${height}>
433
+ ${this.renderSvgContent()}
434
+ </svg>
435
+ ${this.renderOverlay()} ${this.renderTooltip()}
436
+ </div>`;
437
+ }
438
+
439
+ // ── Render
440
+
441
+ render() {
442
+ let body: TemplateResult;
443
+ if (this.loading) {
444
+ body = this.renderSkeleton();
445
+ } else if (!this.hasData()) {
446
+ body = html`<div
447
+ class="empty"
448
+ style=${styleMap({ height: `${this.height}px` })}
449
+ >
450
+ <slot name="empty">No data</slot>
451
+ </div>`;
452
+ } else {
453
+ body = this.renderPlot();
454
+ }
455
+
456
+ const hasContent = !this.loading && this.hasData();
457
+ return html`<div
458
+ class="chart"
459
+ part="container"
460
+ aria-busy=${this.loading ? 'true' : 'false'}
461
+ >
462
+ ${body} ${hasContent ? this.renderLegend() : nothing}
463
+ ${hasContent
464
+ ? html`<table class="sr-only">
465
+ <caption>
466
+ ${this.label || 'Chart data'}
467
+ </caption>
468
+ ${this.renderTable()}
469
+ </table>`
470
+ : nothing}
471
+ <div class="sr-only" aria-live="polite">${this.announcement}</div>
472
+ </div>`;
473
+ }
474
+ }