@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.
- package/dist/assets/styles.css +1 -1
- package/dist/assets/styles.css.map +1 -1
- package/dist/assets/theme.js +19 -19
- package/dist/assets/tokens.css +1 -1
- package/dist/assets/tokens.css.map +1 -1
- package/dist/card-content.js +8 -7
- package/dist/card-content.js.map +1 -1
- package/dist/card.js +142 -98
- package/dist/card.js.map +1 -1
- package/dist/{chart-bar-D5dGFJfK.js → cartesian-chart-DgE4USyC.js} +529 -312
- package/dist/cartesian-chart-DgE4USyC.js.map +1 -0
- package/dist/chart-area.js +78 -0
- package/dist/chart-area.js.map +1 -0
- package/dist/chart-bar.js +283 -213
- package/dist/chart-bar.js.map +1 -1
- package/dist/{transform-DSwFSqzD.js → chart-base-C2qRMF48.js} +771 -102
- package/dist/chart-base-C2qRMF48.js.map +1 -0
- package/dist/chart-doughnut.js +55 -287
- package/dist/chart-doughnut.js.map +1 -1
- package/dist/chart-line.js +626 -0
- package/dist/chart-line.js.map +1 -0
- package/dist/chart-pie.js +637 -195
- package/dist/chart-pie.js.map +1 -1
- package/dist/code-editor.js +704 -131
- package/dist/code-editor.js.map +1 -1
- package/dist/code-group.js +188 -0
- package/dist/code-group.js.map +1 -0
- package/dist/code-highlighter.js +895 -257
- package/dist/code-highlighter.js.map +1 -1
- package/dist/custom-elements-jsdocs.json +1675 -222
- package/dist/custom-elements.json +13351 -3970
- package/dist/image.js +5 -1
- package/dist/image.js.map +1 -1
- package/dist/index.js +8 -7
- package/dist/index.js.map +1 -1
- package/dist/loader-full.js +1 -1
- package/dist/loader.js +9 -3
- package/dist/loader.js.map +1 -1
- package/dist/{is-dark-mode-DOcaw4Yq.js → observe-theme-change-DCifz1uv.js} +2 -6
- package/dist/observe-theme-change-DCifz1uv.js.map +1 -0
- package/dist/path-CJ0Arqcy.js +166 -0
- package/dist/path-CJ0Arqcy.js.map +1 -0
- package/dist/snackbar.js +89 -34
- package/dist/snackbar.js.map +1 -1
- package/dist/src/__internal/chart/cartesian-chart.d.ts +80 -0
- package/dist/src/__internal/chart/chart-base.d.ts +113 -0
- package/dist/src/__internal/chart/paths.d.ts +5 -0
- package/dist/src/__internal/chart/types.d.ts +42 -0
- package/dist/src/card/card-content.d.ts +2 -3
- package/dist/src/card/card.d.ts +31 -17
- package/dist/src/chart-area/chart-area.d.ts +54 -0
- package/dist/src/chart-area/index.d.ts +1 -0
- package/dist/src/chart-bar/chart-bar.d.ts +44 -33
- package/dist/src/chart-bar/index.d.ts +0 -1
- package/dist/src/chart-doughnut/chart-doughnut.d.ts +33 -39
- package/dist/src/chart-line/chart-line.d.ts +69 -0
- package/dist/src/chart-line/index.d.ts +1 -0
- package/dist/src/chart-pie/chart-pie.d.ts +63 -28
- package/dist/src/code-editor/code-editor.d.ts +52 -13
- package/dist/src/code-editor/editor-theme.d.ts +60 -0
- package/dist/src/code-highlighter/code-group.d.ts +52 -0
- package/dist/src/code-highlighter/code-highlighter.d.ts +122 -15
- package/dist/src/code-highlighter/index.d.ts +1 -0
- package/dist/src/code-highlighter/line-ranges.d.ts +24 -0
- package/dist/src/code-highlighter/local.d.ts +4 -1
- package/dist/src/code-highlighter/token-colors.d.ts +8 -0
- package/dist/src/index.d.ts +4 -2
- package/dist/src/snackbar/snackbar.d.ts +13 -1
- package/dist/src/toolbar/toolbar.d.ts +56 -51
- package/dist/table.js +9 -4
- package/dist/table.js.map +1 -1
- package/dist/test/chart.test.d.ts +1 -0
- package/dist/test/code-highlighter.test.d.ts +1 -0
- package/dist/token-colors-0TFHWbcq.js +81 -0
- package/dist/token-colors-0TFHWbcq.js.map +1 -0
- package/dist/toolbar.js +195 -184
- package/dist/toolbar.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -2
- package/readme.md +7 -6
- package/scss/_code-block.scss +139 -0
- package/scss/tokens.css +24 -0
- package/src/__internal/chart/cartesian-chart.ts +404 -0
- package/src/__internal/chart/chart-base.scss +252 -0
- package/src/__internal/chart/chart-base.ts +474 -0
- package/src/__internal/chart/paths.ts +28 -0
- package/src/__internal/chart/types.ts +49 -0
- package/src/card/card-content.ts +12 -9
- package/src/card/card.scss +66 -54
- package/src/card/card.ts +109 -73
- package/src/chart-area/chart-area.ts +82 -0
- package/src/chart-area/index.ts +1 -0
- package/src/chart-bar/chart-bar.ts +336 -250
- package/src/chart-bar/index.ts +0 -1
- package/src/chart-doughnut/chart-doughnut.ts +62 -262
- package/src/chart-doughnut/demo/index.html +14 -22
- package/src/chart-line/chart-line.scss +25 -0
- package/src/chart-line/chart-line.ts +346 -0
- package/src/chart-line/index.ts +1 -0
- package/src/chart-pie/chart-pie.scss +35 -16
- package/src/chart-pie/chart-pie.ts +282 -188
- package/src/chart-pie/demo/index.html +13 -21
- package/src/code-editor/code-editor.scss +132 -36
- package/src/code-editor/code-editor.ts +308 -88
- package/src/code-editor/demo/index.html +39 -12
- package/src/code-editor/editor-theme.ts +213 -0
- package/src/code-highlighter/code-group.scss +34 -0
- package/src/code-highlighter/code-group.ts +173 -0
- package/src/code-highlighter/code-highlighter.scss +226 -111
- package/src/code-highlighter/code-highlighter.ts +554 -199
- package/src/code-highlighter/demo/index.html +68 -4
- package/src/code-highlighter/index.ts +1 -0
- package/src/code-highlighter/line-ranges.ts +81 -0
- package/src/code-highlighter/local.ts +9 -3
- package/src/code-highlighter/token-colors.ts +42 -0
- package/src/index.ts +4 -2
- package/src/loader.ts +8 -2
- package/src/snackbar/demo/index.html +39 -4
- package/src/snackbar/snackbar.scss +27 -10
- package/src/snackbar/snackbar.ts +80 -37
- package/src/table/table.scss +7 -1
- package/src/table/table.ts +4 -3
- package/src/toolbar/toolbar-colors.scss +6 -7
- package/src/toolbar/toolbar.scss +35 -106
- package/src/toolbar/toolbar.ts +180 -80
- package/dist/array-D5vjT2Xm.js +0 -14
- package/dist/array-D5vjT2Xm.js.map +0 -1
- package/dist/chart-bar-D5dGFJfK.js.map +0 -1
- package/dist/chart-stacked-bar.js +0 -403
- package/dist/chart-stacked-bar.js.map +0 -1
- package/dist/copy-to-clipboard-upGx-Ueo.js +0 -39
- package/dist/copy-to-clipboard-upGx-Ueo.js.map +0 -1
- package/dist/is-dark-mode-DOcaw4Yq.js.map +0 -1
- package/dist/pie-Dz0IDiPt.js +0 -537
- package/dist/pie-Dz0IDiPt.js.map +0 -1
- package/dist/pierre-dark-DFWl0m-C.js +0 -4
- package/dist/pierre-dark-DFWl0m-C.js.map +0 -1
- package/dist/pierre-light-BEkAPImt.js +0 -4
- package/dist/pierre-light-BEkAPImt.js.map +0 -1
- package/dist/src/chart-bar/chart-stacked-bar.d.ts +0 -78
- package/dist/src/code-highlighter/constants.d.ts +0 -1
- package/dist/transform-DSwFSqzD.js.map +0 -1
- package/src/chart-bar/chart-bar.scss +0 -58
- package/src/chart-bar/chart-stacked-bar.ts +0 -398
- package/src/chart-doughnut/chart-donut.scss +0 -37
- package/src/code-highlighter/constants.ts +0 -117
- /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
|
|
1
|
+
import { _ as __decorate, I as IndividualComponent } from './IndividualComponent-Bdwyrvd6.js';
|
|
2
2
|
import { n } from './property-B49QQ8pS.js';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
import
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
|
55
|
+
* @label Bar Chart
|
|
27
56
|
* @tag wc-chart-bar
|
|
28
57
|
* @rawTag chart-bar
|
|
29
|
-
* @summary
|
|
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
|
|
63
|
+
* <wc-chart-bar index="month" label="Visitors by month"></wc-chart-bar>
|
|
35
64
|
* <script>
|
|
36
|
-
* document.querySelector('wc-chart-bar')
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
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
|
|
92
|
+
let ChartBar = class ChartBar extends CartesianChart {
|
|
45
93
|
constructor() {
|
|
94
|
+
// ── Static
|
|
46
95
|
super(...arguments);
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
this.
|
|
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
|
-
|
|
63
|
-
|
|
107
|
+
// ── Private methods
|
|
108
|
+
drawProperties() {
|
|
109
|
+
return [...super.drawProperties(), 'stacked', 'orientation', 'showValues'];
|
|
64
110
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
.
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
.
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
.
|
|
117
|
-
.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
.
|
|
141
|
-
.
|
|
142
|
-
.
|
|
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('
|
|
145
|
-
.attr('class', 'bar')
|
|
146
|
-
.attr('
|
|
147
|
-
.attr('
|
|
148
|
-
.attr('
|
|
149
|
-
.
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
.
|
|
164
|
-
|
|
165
|
-
|
|
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(
|
|
269
|
+
.data(labelModels, d => d.id)
|
|
180
270
|
.join(enter => enter
|
|
181
271
|
.append('text')
|
|
182
272
|
.attr('class', 'value-label')
|
|
183
|
-
.attr('
|
|
184
|
-
.attr('
|
|
185
|
-
.attr('
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
.
|
|
203
|
-
|
|
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
|
-
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
?
|
|
224
|
-
|
|
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
|
-
|
|
240
|
-
], ChartBar.prototype, "
|
|
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:
|
|
252
|
-
], ChartBar.prototype, "
|
|
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);
|