@redvars/peacock 3.15.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 (157) 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 +2278 -677
  31. package/dist/custom-elements.json +13962 -3690
  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/pagination.js +408 -110
  42. package/dist/pagination.js.map +1 -1
  43. package/dist/path-CJ0Arqcy.js +166 -0
  44. package/dist/path-CJ0Arqcy.js.map +1 -0
  45. package/dist/snackbar.js +89 -34
  46. package/dist/snackbar.js.map +1 -1
  47. package/dist/src/__internal/chart/cartesian-chart.d.ts +80 -0
  48. package/dist/src/__internal/chart/chart-base.d.ts +113 -0
  49. package/dist/src/__internal/chart/paths.d.ts +5 -0
  50. package/dist/src/__internal/chart/types.d.ts +42 -0
  51. package/dist/src/card/card-content.d.ts +2 -3
  52. package/dist/src/card/card.d.ts +31 -17
  53. package/dist/src/chart-area/chart-area.d.ts +54 -0
  54. package/dist/src/chart-area/index.d.ts +1 -0
  55. package/dist/src/chart-bar/chart-bar.d.ts +44 -33
  56. package/dist/src/chart-bar/index.d.ts +0 -1
  57. package/dist/src/chart-doughnut/chart-doughnut.d.ts +33 -39
  58. package/dist/src/chart-line/chart-line.d.ts +69 -0
  59. package/dist/src/chart-line/index.d.ts +1 -0
  60. package/dist/src/chart-pie/chart-pie.d.ts +63 -28
  61. package/dist/src/code-editor/code-editor.d.ts +52 -13
  62. package/dist/src/code-editor/editor-theme.d.ts +60 -0
  63. package/dist/src/code-highlighter/code-group.d.ts +52 -0
  64. package/dist/src/code-highlighter/code-highlighter.d.ts +122 -15
  65. package/dist/src/code-highlighter/index.d.ts +1 -0
  66. package/dist/src/code-highlighter/line-ranges.d.ts +24 -0
  67. package/dist/src/code-highlighter/local.d.ts +4 -1
  68. package/dist/src/code-highlighter/token-colors.d.ts +8 -0
  69. package/dist/src/index.d.ts +4 -2
  70. package/dist/src/pagination/page-items.d.ts +14 -0
  71. package/dist/src/pagination/pagination.d.ts +46 -7
  72. package/dist/src/snackbar/snackbar.d.ts +13 -1
  73. package/dist/src/table/table.d.ts +125 -27
  74. package/dist/src/toolbar/toolbar.d.ts +56 -51
  75. package/dist/table.js +1007 -338
  76. package/dist/table.js.map +1 -1
  77. package/dist/test/chart.test.d.ts +1 -0
  78. package/dist/test/code-highlighter.test.d.ts +1 -0
  79. package/dist/test/pagination.test.d.ts +1 -0
  80. package/dist/test/table.test.d.ts +1 -0
  81. package/dist/token-colors-0TFHWbcq.js +81 -0
  82. package/dist/token-colors-0TFHWbcq.js.map +1 -0
  83. package/dist/toolbar.js +195 -184
  84. package/dist/toolbar.js.map +1 -1
  85. package/dist/tsconfig.tsbuildinfo +1 -1
  86. package/package.json +1 -2
  87. package/readme.md +7 -6
  88. package/scss/_code-block.scss +139 -0
  89. package/scss/tokens.css +24 -0
  90. package/src/__internal/chart/cartesian-chart.ts +404 -0
  91. package/src/__internal/chart/chart-base.scss +252 -0
  92. package/src/__internal/chart/chart-base.ts +474 -0
  93. package/src/__internal/chart/paths.ts +28 -0
  94. package/src/__internal/chart/types.ts +49 -0
  95. package/src/card/card-content.ts +12 -9
  96. package/src/card/card.scss +66 -54
  97. package/src/card/card.ts +109 -73
  98. package/src/chart-area/chart-area.ts +82 -0
  99. package/src/chart-area/index.ts +1 -0
  100. package/src/chart-bar/chart-bar.ts +336 -250
  101. package/src/chart-bar/index.ts +0 -1
  102. package/src/chart-doughnut/chart-doughnut.ts +62 -262
  103. package/src/chart-doughnut/demo/index.html +14 -22
  104. package/src/chart-line/chart-line.scss +25 -0
  105. package/src/chart-line/chart-line.ts +346 -0
  106. package/src/chart-line/index.ts +1 -0
  107. package/src/chart-pie/chart-pie.scss +35 -16
  108. package/src/chart-pie/chart-pie.ts +282 -188
  109. package/src/chart-pie/demo/index.html +13 -21
  110. package/src/code-editor/code-editor.scss +132 -36
  111. package/src/code-editor/code-editor.ts +308 -88
  112. package/src/code-editor/demo/index.html +39 -12
  113. package/src/code-editor/editor-theme.ts +213 -0
  114. package/src/code-highlighter/code-group.scss +34 -0
  115. package/src/code-highlighter/code-group.ts +173 -0
  116. package/src/code-highlighter/code-highlighter.scss +226 -111
  117. package/src/code-highlighter/code-highlighter.ts +554 -199
  118. package/src/code-highlighter/demo/index.html +68 -4
  119. package/src/code-highlighter/index.ts +1 -0
  120. package/src/code-highlighter/line-ranges.ts +81 -0
  121. package/src/code-highlighter/local.ts +9 -3
  122. package/src/code-highlighter/token-colors.ts +42 -0
  123. package/src/index.ts +4 -2
  124. package/src/loader.ts +8 -2
  125. package/src/pagination/page-items.ts +50 -0
  126. package/src/pagination/pagination.scss +159 -44
  127. package/src/pagination/pagination.ts +275 -74
  128. package/src/snackbar/demo/index.html +39 -4
  129. package/src/snackbar/snackbar.scss +27 -10
  130. package/src/snackbar/snackbar.ts +80 -37
  131. package/src/table/table.scss +337 -109
  132. package/src/table/table.ts +812 -272
  133. package/src/toolbar/toolbar-colors.scss +6 -7
  134. package/src/toolbar/toolbar.scss +35 -106
  135. package/src/toolbar/toolbar.ts +180 -80
  136. package/dist/array-D5vjT2Xm.js +0 -14
  137. package/dist/array-D5vjT2Xm.js.map +0 -1
  138. package/dist/chart-bar-D5dGFJfK.js.map +0 -1
  139. package/dist/chart-stacked-bar.js +0 -403
  140. package/dist/chart-stacked-bar.js.map +0 -1
  141. package/dist/copy-to-clipboard-upGx-Ueo.js +0 -39
  142. package/dist/copy-to-clipboard-upGx-Ueo.js.map +0 -1
  143. package/dist/is-dark-mode-DOcaw4Yq.js.map +0 -1
  144. package/dist/pie-Dz0IDiPt.js +0 -537
  145. package/dist/pie-Dz0IDiPt.js.map +0 -1
  146. package/dist/pierre-dark-DFWl0m-C.js +0 -4
  147. package/dist/pierre-dark-DFWl0m-C.js.map +0 -1
  148. package/dist/pierre-light-BEkAPImt.js +0 -4
  149. package/dist/pierre-light-BEkAPImt.js.map +0 -1
  150. package/dist/src/chart-bar/chart-stacked-bar.d.ts +0 -78
  151. package/dist/src/code-highlighter/constants.d.ts +0 -1
  152. package/dist/transform-DSwFSqzD.js.map +0 -1
  153. package/src/chart-bar/chart-bar.scss +0 -58
  154. package/src/chart-bar/chart-stacked-bar.ts +0 -398
  155. package/src/chart-doughnut/chart-donut.scss +0 -37
  156. package/src/code-highlighter/constants.ts +0 -117
  157. /package/dist/test/{chart-bar.test.d.ts → card.test.d.ts} +0 -0
package/dist/chart-bar.js CHANGED
@@ -1,255 +1,325 @@
1
- import { _ as __decorate, I as IndividualComponent, a as i, b } from './IndividualComponent-Bdwyrvd6.js';
1
+ import { _ as __decorate, I as IndividualComponent } from './IndividualComponent-Bdwyrvd6.js';
2
2
  import { n } from './property-B49QQ8pS.js';
3
- import { e } from './query-CHb9Ft_d.js';
4
- import { o } from './style-map-BQD5Et1D.js';
5
- import { o as ordinal, s as select } from './transform-DSwFSqzD.js';
6
- import { c as css_248z, b as band, m as max, l as linear, a as axisLeft, d as axisBottom } from './chart-bar-D5dGFJfK.js';
3
+ import { c as css_248z } from './chart-base-C2qRMF48.js';
4
+ import { C as CartesianChart, s as stack, l as linear, m as min, a as max, b as band } from './cartesian-chart-DgE4USyC.js';
5
+ import './state-DkTK9EGF.js';
6
+ import './query-CHb9Ft_d.js';
7
7
  import './base-Cl6v8-BZ.js';
8
+ import './class-map-DG7CA1et.js';
8
9
  import './directive-ZPhl09Yt.js';
10
+ import './style-map-BQD5Et1D.js';
9
11
 
10
- const chartColors = [];
11
- ['purple', 'blue', 'red', 'green', 'yellow', 'orange'].forEach(colorName => {
12
- chartColors.push(`var(--color-${colorName})`);
13
- });
14
- const DEFAULT_WIDTH = 480;
15
- const DEFAULT_HEIGHT = 320;
16
- const BAR_RADIUS = 10;
17
- const DURATION = 450;
18
- function debounce(fn, wait) {
19
- let timer;
20
- return ((...args) => {
21
- clearTimeout(timer);
22
- timer = setTimeout(() => fn(...args), wait);
23
- });
12
+ function stackOffsetDiverging(series, order) {
13
+ if (!((n = series.length) > 0)) return;
14
+ for (var i, j = 0, d, dy, yp, yn, n, m = series[order[0]].length; j < m; ++j) {
15
+ for (yp = yn = 0, i = 0; i < n; ++i) {
16
+ if ((dy = (d = series[order[i]][j])[1] - d[0]) > 0) {
17
+ d[0] = yp, d[1] = yp += dy;
18
+ } else if (dy < 0) {
19
+ d[1] = yn, d[0] = yn += dy;
20
+ } else {
21
+ d[0] = 0, d[1] = dy;
22
+ }
23
+ }
24
+ }
25
+ }
26
+
27
+ /**
28
+ * SVG path for a rectangle with its own radius per corner, in the order top-left, top-right,
29
+ * bottom-right, bottom-left. Every path has the same commands, so d3 can tween between any two.
30
+ */
31
+ function roundedRectPath(x, y, width, height, radii) {
32
+ const w = Math.max(width, 0);
33
+ const h = Math.max(height, 0);
34
+ const limit = Math.min(w / 2, h / 2);
35
+ const [tl, tr, br, bl] = radii.map(r => Math.max(Math.min(r, limit), 0));
36
+ return [
37
+ `M${x + tl},${y}`,
38
+ `H${x + w - tr}`,
39
+ `A${tr},${tr} 0 0 1 ${x + w},${y + tr}`,
40
+ `V${y + h - br}`,
41
+ `A${br},${br} 0 0 1 ${x + w - br},${y + h}`,
42
+ `H${x + bl}`,
43
+ `A${bl},${bl} 0 0 1 ${x},${y + h - bl}`,
44
+ `V${y + tl}`,
45
+ `A${tl},${tl} 0 0 1 ${x + tl},${y}`,
46
+ 'Z',
47
+ ].join('');
24
48
  }
49
+
50
+ /** Gap between stacked segments, in pixels. */
51
+ const STACK_GAP = 2;
52
+ /** Room reserved for value labels at the end of horizontal bars, per character. */
53
+ const VALUE_CHAR_WIDTH = 7;
25
54
  /**
26
- * @label Chart Bar
55
+ * @label Bar Chart
27
56
  * @tag wc-chart-bar
28
57
  * @rawTag chart-bar
29
- * @summary A vertical bar chart that follows color and spacing tokens.
58
+ * @summary Compares values across categories with vertical or horizontal bars, grouped or stacked by series.
30
59
  * @tags charts
31
60
  *
32
61
  * @example
33
62
  * ```html
34
- * <wc-chart-bar width="520" height="320"></wc-chart-bar>
63
+ * <wc-chart-bar index="month" label="Visitors by month"></wc-chart-bar>
35
64
  * <script>
36
- * document.querySelector('wc-chart-bar').data = [
37
- * { name: 'apples', label: 'Apples', value: 20 },
38
- * { name: 'bananas', label: 'Bananas', value: 35 },
39
- * { name: 'cherries', label: 'Cherries', value: 15 },
65
+ * const chart = document.querySelector('wc-chart-bar');
66
+ * chart.config = {
67
+ * desktop: { label: 'Desktop' },
68
+ * mobile: { label: 'Mobile' },
69
+ * };
70
+ * chart.data = [
71
+ * { month: 'January', desktop: 186, mobile: 80 },
72
+ * { month: 'February', desktop: 305, mobile: 200 },
73
+ * { month: 'March', desktop: 237, mobile: 120 },
40
74
  * ];
41
75
  * </script>
42
76
  * ```
77
+ *
78
+ * @slot empty - Shown when there is no data. Defaults to "No data".
79
+ *
80
+ * @fires chart-select - A bar group was clicked or chosen with Enter. `detail` holds `index` and `datum`.
81
+ *
82
+ * @csspart container - The outer wrapper.
83
+ * @csspart plot - The focusable plot area holding the SVG.
84
+ * @csspart tooltip - The tooltip.
85
+ * @csspart legend - The legend.
86
+ *
87
+ * @cssprop --color-chart-1 - Color of the first series; series cycle through `--color-chart-1` to `--color-chart-8`.
88
+ * @cssprop --chart-grid-color - Gridline and baseline color. Defaults to `--color-outline-variant`.
89
+ * @cssprop --chart-axis-color - Axis label color. Defaults to `--color-on-surface-variant`.
90
+ * @cssprop --chart-dim-opacity - Opacity of marks that are not hovered. Defaults to `0.4`.
43
91
  */
44
- let ChartBar = class ChartBar extends i {
92
+ let ChartBar = class ChartBar extends CartesianChart {
45
93
  constructor() {
94
+ // ── Static
46
95
  super(...arguments);
47
- /** Width of the chart in pixels. */
48
- this.width = 0;
49
- /** Height of the chart in pixels. */
50
- this.height = DEFAULT_HEIGHT;
51
- /** Margin around the chart drawing area. */
52
- this.margin = 24;
53
- /** Chart data array. Each item should have name, value, and optional label and color. */
54
- this.data = [];
55
- /** Whether to render value labels above bars. */
56
- this.showValues = true;
57
- this._initialized = false;
58
- this._debouncedRenderChart = debounce(() => {
59
- this._renderChart(true);
60
- }, 200);
96
+ // ── Properties
97
+ /** Stacks the series in each category instead of placing them side by side. */
98
+ this.stacked = false;
99
+ /** `vertical` draws columns; `horizontal` draws bars from the left. */
100
+ this.orientation = 'vertical';
101
+ /** Prints each bar's value (each stack's total when stacked) at its end. */
102
+ this.showValues = false;
103
+ // ── Private fields
104
+ /** Bars from the last draw, for highlighting. */
105
+ this.bars = [];
61
106
  }
62
- firstUpdated() {
63
- this._renderChart(false);
107
+ // ── Private methods
108
+ drawProperties() {
109
+ return [...super.drawProperties(), 'stacked', 'orientation', 'showValues'];
64
110
  }
65
- updated(changedProperties) {
66
- if (!this._initialized) {
67
- this._initialized = true;
68
- return;
111
+ /** Corner radius of the rounded end, from `--shape-corner-extra-small`. */
112
+ cornerRadius() {
113
+ const value = parseFloat(getComputedStyle(this).getPropertyValue('--shape-corner-extra-small'));
114
+ return Number.isFinite(value) ? value : 4;
115
+ }
116
+ /** Bars and value labels in data space; positions are filled once scales exist. */
117
+ buildModels() {
118
+ const series = this.visibleSeries();
119
+ const keys = series.map(s => s.key);
120
+ const colors = new Map(series.map(s => [s.key, s.color]));
121
+ const bars = [];
122
+ const labels = [];
123
+ if (this.stacked) {
124
+ const layers = stack()
125
+ .keys(keys)
126
+ .value((d, key) => this.seriesValue(d, key))
127
+ .offset(stackOffsetDiverging)(this.data);
128
+ this.data.forEach((datum, index) => {
129
+ // Segments run from the zero side (`from`) outwards (`to`).
130
+ const segments = layers
131
+ .map(layer => {
132
+ const [a, b] = layer[index];
133
+ const positive = this.seriesValue(datum, layer.key) >= 0;
134
+ return {
135
+ key: layer.key,
136
+ from: positive ? Math.min(a, b) : Math.max(a, b),
137
+ to: positive ? Math.max(a, b) : Math.min(a, b),
138
+ };
139
+ })
140
+ .filter(segment => segment.from !== segment.to);
141
+ // The outermost segment on each side of zero gets the rounded end.
142
+ const top = segments
143
+ .filter(s => s.to > 0)
144
+ .sort((a, b) => b.to - a.to)[0]?.key;
145
+ const bottom = segments
146
+ .filter(s => s.to < 0)
147
+ .sort((a, b) => a.to - b.to)[0]?.key;
148
+ segments.forEach(segment => {
149
+ bars.push({
150
+ id: `${index}:${segment.key}`,
151
+ index,
152
+ key: segment.key,
153
+ color: colors.get(segment.key) ?? '',
154
+ from: segment.from,
155
+ to: segment.to,
156
+ rounded: segment.key === (segment.to > 0 ? top : bottom),
157
+ gap: segment.from !== 0 ? STACK_GAP : 0,
158
+ });
159
+ });
160
+ labels.push({
161
+ id: String(index),
162
+ index,
163
+ value: keys.reduce((sum, key) => sum + this.seriesValue(datum, key), 0),
164
+ });
165
+ });
69
166
  }
70
- const watchedProps = ['width', 'height', 'margin', 'data', 'showValues'];
71
- const hasChanged = watchedProps.some(prop => changedProperties.has(prop));
72
- if (hasChanged) {
73
- this._debouncedRenderChart();
167
+ else {
168
+ this.data.forEach((datum, index) => {
169
+ keys.forEach(key => {
170
+ const value = this.seriesValue(datum, key);
171
+ bars.push({
172
+ id: `${index}:${key}`,
173
+ index,
174
+ key,
175
+ color: colors.get(key) ?? '',
176
+ from: 0,
177
+ to: value,
178
+ rounded: true,
179
+ gap: 0,
180
+ });
181
+ labels.push({ id: `${index}:${key}`, index, value });
182
+ });
183
+ });
74
184
  }
185
+ return { keys, bars, labels };
75
186
  }
76
- _getPaletteScale() {
77
- return ordinal()
78
- .domain(this.data.map(d => d.name))
79
- .range(chartColors);
80
- }
81
- _resolveColor(name, override, scale) {
82
- return override || scale(name);
83
- }
84
- _renderChart(animate) {
85
- if (!this.svgElement)
86
- return;
87
- const width = this.width > 0 ? this.width : DEFAULT_WIDTH;
88
- const height = this.height > 0 ? this.height : DEFAULT_HEIGHT;
89
- const margin = Math.max(this.margin, 12);
90
- const data = this.data ?? [];
91
- const svg = select(this.svgElement);
92
- svg.attr('width', width).attr('height', height);
93
- const innerWidth = Math.max(width - margin * 2, 0);
94
- const innerHeight = Math.max(height - margin * 2, 0);
95
- const colorScale = this._getPaletteScale();
96
- const container = svg.select('.chart-container');
97
- container.attr('transform', `translate(${margin},${margin})`);
98
- if (!data.length || innerWidth === 0 || innerHeight === 0) {
99
- container.select('.bars').selectAll('*').remove();
100
- container.select('.x-axis').selectAll('*').remove();
101
- container.select('.y-grid').selectAll('*').remove();
102
- container.select('.value-labels').selectAll('*').remove();
187
+ draw(animate) {
188
+ const root = this.plotArea();
189
+ if (!root)
103
190
  return;
191
+ const vertical = this.orientation !== 'horizontal';
192
+ const { keys, bars, labels } = this.buildModels();
193
+ const ends = bars.flatMap(bar => [bar.from, bar.to]);
194
+ const valueScale = linear()
195
+ .domain([Math.min(min(ends) ?? 0, 0), Math.max(max(ends) ?? 0, 0) || 1])
196
+ .nice(5);
197
+ const longestValue = Math.max(...labels.map(l => this.formatValue(l.value).length), 1);
198
+ // Vertical charts only need a flag for headroom; horizontal ones need the label width.
199
+ let valueLabelSpace = 0;
200
+ if (this.showValues) {
201
+ valueLabelSpace = vertical ? 1 : longestValue * VALUE_CHAR_WIDTH + 8;
104
202
  }
105
- const xScale = band()
106
- .domain(data.map(d => d.name))
107
- .range([0, innerWidth])
108
- .padding(0.28);
109
- const maxValue = max(data, d => d.value) ?? 0;
110
- const yScale = linear()
111
- .domain([0, maxValue || 1])
112
- .nice()
113
- .range([innerHeight, 0]);
114
- const yGrid = container.select('.y-grid');
115
- yGrid
116
- .call(axisLeft(yScale)
117
- .ticks(5)
118
- .tickSize(-innerWidth)
119
- .tickFormat(() => ''))
120
- .selectAll('.tick text')
121
- .remove();
122
- yGrid.select('.domain').remove();
123
- yGrid.selectAll('.tick line').attr('class', 'gridline');
124
- const xAxis = container.select('.x-axis');
125
- xAxis
126
- .attr('transform', `translate(0,${innerHeight})`)
127
- .call(axisBottom(xScale)
128
- .tickSizeOuter(0)
129
- .tickFormat(name => {
130
- const entry = data.find(d => d.name === name);
131
- return entry?.label ?? name;
203
+ this.computeLayout(valueScale, valueLabelSpace);
204
+ const { left, top, innerWidth, innerHeight } = this.layout;
205
+ const categoryScale = band()
206
+ .domain(this.categoryDomain())
207
+ .range([0, vertical ? innerWidth : innerHeight])
208
+ .paddingInner(keys.length > 1 && !this.stacked ? 0.2 : 0.3)
209
+ .paddingOuter(0.15);
210
+ valueScale.range(vertical ? [innerHeight, 0] : [0, innerWidth]);
211
+ this.drawFrame(categoryScale, valueScale);
212
+ const groupScale = band()
213
+ .domain(this.stacked ? ['all'] : keys)
214
+ .range([0, categoryScale.bandwidth()])
215
+ .padding(this.stacked || keys.length < 2 ? 0 : 0.1);
216
+ const placed = bars.map(bar => ({
217
+ ...bar,
218
+ position: (categoryScale(String(bar.index)) ?? 0) +
219
+ (groupScale(this.stacked ? 'all' : bar.key) ?? 0),
220
+ thickness: groupScale.bandwidth(),
132
221
  }));
133
- xAxis.select('.domain').attr('stroke', 'var(--color-outline-variant)');
134
- xAxis.selectAll('.tick line').remove();
135
- xAxis
136
- .selectAll('.tick text')
137
- .attr('class', 'axis-label')
138
- .attr('dy', '1.1em');
139
- const bars = container
140
- .select('.bars')
141
- .selectAll('rect')
142
- .data(data, d => d.name)
222
+ this.bars = placed;
223
+ const radius = this.cornerRadius();
224
+ const path = (bar, grown) => {
225
+ const zero = valueScale(0);
226
+ const start = valueScale(grown ? bar.from : 0);
227
+ const end = valueScale(grown ? bar.to : 0);
228
+ const r = bar.rounded ? Math.min(radius, bar.thickness / 2) : 0;
229
+ // Positive bars end up/right; negative ones down/left.
230
+ const positive = bar.to >= bar.from;
231
+ const length = Math.max(Math.abs(end - start) - (grown ? bar.gap : 0), 0);
232
+ if (vertical) {
233
+ const y = positive ? end : start + (grown ? bar.gap : 0);
234
+ const base = grown ? y : zero;
235
+ return roundedRectPath(bar.position, base, bar.thickness, length, positive ? [r, r, 0, 0] : [0, 0, r, r]);
236
+ }
237
+ const x = positive ? start + (grown ? bar.gap : 0) : end;
238
+ return roundedRectPath(grown ? x : zero, bar.position, length, bar.thickness, positive ? [0, r, r, 0] : [r, 0, 0, r]);
239
+ };
240
+ const marks = root
241
+ .select('.marks')
242
+ .selectAll('path.bar')
243
+ .data(placed, d => d.id)
143
244
  .join(enter => enter
144
- .append('rect')
145
- .attr('class', 'bar')
146
- .attr('x', d => xScale(d.name) ?? 0)
147
- .attr('width', xScale.bandwidth())
148
- .attr('y', innerHeight)
149
- .attr('height', 0)
150
- .attr('rx', BAR_RADIUS)
151
- .attr('ry', BAR_RADIUS)
152
- .style('fill', d => this._resolveColor(d.name, d.color, colorScale)), update => update, exit => exit
153
- .transition()
154
- .duration(DURATION)
155
- .attr('height', 0)
156
- .attr('y', innerHeight)
157
- .remove());
158
- bars
159
- .attr('x', d => xScale(d.name) ?? 0)
160
- .attr('width', xScale.bandwidth())
161
- .attr('rx', BAR_RADIUS)
162
- .attr('ry', BAR_RADIUS)
163
- .style('fill', d => this._resolveColor(d.name, d.color, colorScale));
164
- if (animate) {
165
- bars
166
- .transition()
167
- .duration(DURATION)
168
- .attr('y', d => yScale(d.value))
169
- .attr('height', d => innerHeight - yScale(d.value));
170
- }
171
- else {
172
- bars
173
- .attr('y', d => yScale(d.value))
174
- .attr('height', d => innerHeight - yScale(d.value));
175
- }
176
- const valueLabels = container
245
+ .append('path')
246
+ .attr('class', 'mark bar')
247
+ .attr('d', d => path(d, false)))
248
+ .attr('data-index', d => d.index)
249
+ .attr('data-key', d => d.key)
250
+ .style('fill', d => d.color);
251
+ this.motion(marks, animate).attr('d', d => path(d, true));
252
+ // Value labels sit just past the end of each bar or stack.
253
+ const center = (label) => {
254
+ if (this.stacked) {
255
+ return ((categoryScale(String(label.index)) ?? 0) +
256
+ categoryScale.bandwidth() / 2);
257
+ }
258
+ const key = label.id.split(':').slice(1).join(':');
259
+ return ((categoryScale(String(label.index)) ?? 0) +
260
+ (groupScale(key) ?? 0) +
261
+ groupScale.bandwidth() / 2);
262
+ };
263
+ const labelModels = this.showValues
264
+ ? labels.map(label => ({ ...label, center: center(label) }))
265
+ : [];
266
+ const valueLabels = root
177
267
  .select('.value-labels')
178
268
  .selectAll('text')
179
- .data(this.showValues ? data : [], d => d.name)
269
+ .data(labelModels, d => d.id)
180
270
  .join(enter => enter
181
271
  .append('text')
182
272
  .attr('class', 'value-label')
183
- .attr('text-anchor', 'middle')
184
- .attr('x', d => (xScale(d.name) ?? 0) + xScale.bandwidth() / 2)
185
- .attr('y', innerHeight - 6)
186
- .text(d => d.value.toLocaleString()), update => update, exit => exit.remove());
187
- const resolveLabelY = (value) => {
188
- const offset = yScale(value) - 8;
189
- return Math.min(offset, innerHeight - 8);
190
- };
191
- if (animate) {
192
- valueLabels
193
- .transition()
194
- .duration(DURATION)
195
- .attr('x', d => (xScale(d.name) ?? 0) + xScale.bandwidth() / 2)
196
- .attr('y', d => resolveLabelY(d.value))
197
- .text(d => d.value.toLocaleString());
198
- }
199
- else {
200
- valueLabels
201
- .attr('x', d => (xScale(d.name) ?? 0) + xScale.bandwidth() / 2)
202
- .attr('y', d => resolveLabelY(d.value))
203
- .text(d => d.value.toLocaleString());
204
- }
273
+ .attr('x', d => (vertical ? d.center : valueScale(0)))
274
+ .attr('y', d => (vertical ? valueScale(0) : d.center)))
275
+ .attr('text-anchor', d => {
276
+ if (vertical)
277
+ return 'middle';
278
+ return d.value < 0 ? 'end' : 'start';
279
+ })
280
+ .attr('dominant-baseline', vertical ? 'auto' : 'middle')
281
+ .text(d => this.formatValue(d.value));
282
+ this.motion(valueLabels, animate)
283
+ .attr('x', d => vertical ? d.center : valueScale(d.value) + (d.value < 0 ? -6 : 6))
284
+ .attr('y', d => vertical ? valueScale(d.value) + (d.value < 0 ? 14 : -6) : d.center);
285
+ // Tooltip anchors at the far end of each category.
286
+ this.anchors = this.data.map((_, index) => {
287
+ const own = placed.filter(bar => bar.index === index);
288
+ const far = max(own, bar => bar.to) ?? 0;
289
+ const c = (categoryScale(String(index)) ?? 0) + categoryScale.bandwidth() / 2;
290
+ return vertical
291
+ ? { x: left + c, y: top + Math.max(valueScale(far), 24) }
292
+ : { x: left + Math.min(valueScale(far), innerWidth - 24), y: top + c };
293
+ });
205
294
  }
206
- render() {
207
- const paletteScale = this._getPaletteScale();
208
- const legendItems = this.data.map(item => ({
209
- name: item.label ?? item.name,
210
- color: this._resolveColor(item.name, item.color, paletteScale),
211
- }));
212
- return b `
213
- <div class="chart-frame">
214
- <svg role="img" aria-label="Bar chart">
215
- <g class="chart-container">
216
- <g class="y-grid"></g>
217
- <g class="bars"></g>
218
- <g class="x-axis"></g>
219
- <g class="value-labels"></g>
220
- </g>
221
- </svg>
222
- ${legendItems.length
223
- ? b `<div class="legend" role="list">
224
- ${legendItems.map(item => b `<span class="legend-item" role="listitem">
225
- <span
226
- class="swatch"
227
- style=${o({ background: item.color })}
228
- ></span>
229
- <span>${item.name}</span>
230
- </span>`)}
231
- </div>`
232
- : null}
233
- </div>
234
- `;
295
+ drawHighlight() {
296
+ super.drawHighlight();
297
+ const root = this.plotArea();
298
+ if (!root)
299
+ return;
300
+ const { activeIndex, activeSeries } = this;
301
+ root
302
+ .select('.marks')
303
+ .selectAll('path.bar')
304
+ .classed('is-dimmed', d => (activeIndex >= 0 && d.index !== activeIndex) ||
305
+ (activeSeries !== undefined && d.key !== activeSeries));
306
+ }
307
+ getSelectDetail(index) {
308
+ const detail = super.getSelectDetail(index);
309
+ // A single visible series identifies the bar exactly.
310
+ const visible = this.bars.filter(bar => bar.index === index);
311
+ return visible.length === 1
312
+ ? { ...detail, series: visible[0].key }
313
+ : detail;
235
314
  }
236
315
  };
237
316
  ChartBar.styles = [css_248z];
238
317
  __decorate([
239
- e('svg')
240
- ], ChartBar.prototype, "svgElement", void 0);
241
- __decorate([
242
- n({ type: Number, reflect: true })
243
- ], ChartBar.prototype, "width", void 0);
244
- __decorate([
245
- n({ type: Number, reflect: true })
246
- ], ChartBar.prototype, "height", void 0);
247
- __decorate([
248
- n({ type: Number })
249
- ], ChartBar.prototype, "margin", void 0);
318
+ n({ type: Boolean, reflect: true })
319
+ ], ChartBar.prototype, "stacked", void 0);
250
320
  __decorate([
251
- n({ type: Array })
252
- ], ChartBar.prototype, "data", void 0);
321
+ n({ type: String, reflect: true })
322
+ ], ChartBar.prototype, "orientation", void 0);
253
323
  __decorate([
254
324
  n({ type: Boolean, attribute: 'show-values' })
255
325
  ], ChartBar.prototype, "showValues", void 0);