@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
|
@@ -1,252 +1,346 @@
|
|
|
1
|
-
import { html,
|
|
2
|
-
import { property
|
|
3
|
-
import {
|
|
1
|
+
import { html, svg } from 'lit';
|
|
2
|
+
import { property } from 'lit/decorators.js';
|
|
3
|
+
import { styleMap } from 'lit/directives/style-map.js';
|
|
4
|
+
import { arc, easeCubicOut, interpolate, pie, PieArcDatum, select } from 'd3';
|
|
4
5
|
import IndividualComponent from '@/IndividualComponent.js';
|
|
6
|
+
import { ChartBase, chartColor } from '../__internal/chart/chart-base.js';
|
|
7
|
+
import type {
|
|
8
|
+
ChartSeries,
|
|
9
|
+
ChartTooltipModel,
|
|
10
|
+
} from '../__internal/chart/types.js';
|
|
11
|
+
import baseStyles from '../__internal/chart/chart-base.scss';
|
|
5
12
|
import styles from './chart-pie.scss';
|
|
6
13
|
|
|
7
|
-
export type
|
|
8
|
-
|
|
9
|
-
|
|
14
|
+
export type {
|
|
15
|
+
ChartSelectDetail,
|
|
16
|
+
ChartValueFormatter,
|
|
17
|
+
} from '../__internal/chart/types.js';
|
|
10
18
|
|
|
19
|
+
/** One slice. */
|
|
11
20
|
export type ChartPieItem = {
|
|
21
|
+
/** Unique key of the slice. */
|
|
12
22
|
name: string;
|
|
13
23
|
value: number;
|
|
24
|
+
/** Display name in the legend and tooltip. Defaults to `name`. */
|
|
14
25
|
label?: string;
|
|
26
|
+
/** Any CSS color. Defaults to the next `--color-chart-n` token. */
|
|
15
27
|
color?: string;
|
|
16
28
|
};
|
|
17
29
|
|
|
18
|
-
|
|
19
|
-
['purple', 'blue', 'red', 'green', 'yellow', 'orange'].forEach(colorName => {
|
|
20
|
-
chartColors.push({
|
|
21
|
-
color: `var(--color-${colorName})`,
|
|
22
|
-
});
|
|
23
|
-
});
|
|
30
|
+
type SliceArc = PieArcDatum<ChartPieItem>;
|
|
24
31
|
|
|
25
|
-
/**
|
|
26
|
-
interface
|
|
27
|
-
|
|
32
|
+
/** A slice path that remembers its last angles, so updates tween from where it was. */
|
|
33
|
+
interface SlicePath extends SVGPathElement {
|
|
34
|
+
__current?: { startAngle: number; endAngle: number };
|
|
28
35
|
}
|
|
29
36
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
const DURATION = 450;
|
|
38
|
+
/** Distance a hovered slice moves out from the centre. */
|
|
39
|
+
const PULL_OUT = 6;
|
|
40
|
+
/** Room around the pie for the hovered slice. */
|
|
41
|
+
const PADDING = PULL_OUT + 2;
|
|
42
|
+
/** Gap between slices, in pixels at the outer edge. */
|
|
43
|
+
const SLICE_GAP = 2;
|
|
44
|
+
/** Smallest slice angle that gets a percentage label. */
|
|
45
|
+
const MIN_LABEL_ANGLE = 0.35;
|
|
37
46
|
|
|
38
47
|
/**
|
|
39
|
-
* @label Chart
|
|
48
|
+
* @label Pie Chart
|
|
40
49
|
* @tag wc-chart-pie
|
|
41
50
|
* @rawTag chart-pie
|
|
42
|
-
* @summary
|
|
51
|
+
* @summary Shows how parts make up a whole. Best with a handful of slices.
|
|
43
52
|
* @tags charts
|
|
44
53
|
*
|
|
45
54
|
* @example
|
|
46
55
|
* ```html
|
|
47
|
-
* <wc-chart-pie
|
|
56
|
+
* <wc-chart-pie label="Visitors by browser"></wc-chart-pie>
|
|
48
57
|
* <script>
|
|
49
58
|
* document.querySelector('wc-chart-pie').data = [
|
|
50
|
-
* { name: '
|
|
51
|
-
* { name: '
|
|
52
|
-
* { name: '
|
|
59
|
+
* { name: 'chrome', label: 'Chrome', value: 275 },
|
|
60
|
+
* { name: 'safari', label: 'Safari', value: 200 },
|
|
61
|
+
* { name: 'firefox', label: 'Firefox', value: 187 },
|
|
53
62
|
* ];
|
|
54
63
|
* </script>
|
|
55
64
|
* ```
|
|
65
|
+
*
|
|
66
|
+
* @slot empty - Shown when there is no data. Defaults to "No data".
|
|
67
|
+
*
|
|
68
|
+
* @fires chart-select - A slice was clicked or chosen with Enter. `detail` holds `index`, `series` (the slice name) and `datum`.
|
|
69
|
+
*
|
|
70
|
+
* @csspart container - The outer wrapper.
|
|
71
|
+
* @csspart plot - The focusable plot area holding the SVG.
|
|
72
|
+
* @csspart tooltip - The tooltip.
|
|
73
|
+
* @csspart legend - The legend.
|
|
74
|
+
*
|
|
75
|
+
* @cssprop --color-chart-1 - Color of the first slice; slices cycle through `--color-chart-1` to `--color-chart-8`.
|
|
76
|
+
* @cssprop --chart-dim-opacity - Opacity of slices that are not hovered. Defaults to `0.4`.
|
|
56
77
|
*/
|
|
57
78
|
@IndividualComponent
|
|
58
|
-
export class ChartPie extends
|
|
59
|
-
|
|
79
|
+
export class ChartPie extends ChartBase {
|
|
80
|
+
// ── Static
|
|
60
81
|
|
|
61
|
-
|
|
62
|
-
private svgElement?: SVGElement;
|
|
82
|
+
static styles = [baseStyles, styles];
|
|
63
83
|
|
|
64
|
-
|
|
65
|
-
@property({ type: Number, reflect: true }) width: number = 0;
|
|
84
|
+
// ── Properties
|
|
66
85
|
|
|
67
|
-
/**
|
|
68
|
-
@property({ type:
|
|
86
|
+
/** Slices to draw. */
|
|
87
|
+
@property({ type: Array }) data: ChartPieItem[] = [];
|
|
69
88
|
|
|
70
|
-
/**
|
|
71
|
-
@property({ type:
|
|
72
|
-
|
|
89
|
+
/** Size of the hole as a fraction of the radius, from 0 (pie) to 0.9. */
|
|
90
|
+
@property({ type: Number, attribute: 'inner-radius' }) innerRadius: number =
|
|
91
|
+
0;
|
|
73
92
|
|
|
74
|
-
/**
|
|
75
|
-
@property({ type:
|
|
93
|
+
/** Prints each slice's percentage inside it when it fits. */
|
|
94
|
+
@property({ type: Boolean, attribute: 'show-labels' }) showLabels: boolean =
|
|
95
|
+
false;
|
|
76
96
|
|
|
77
|
-
|
|
97
|
+
// ── Private fields
|
|
78
98
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}, 300);
|
|
99
|
+
/** Arcs from the last draw. */
|
|
100
|
+
private arcs: SliceArc[] = [];
|
|
82
101
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
102
|
+
/** Outer radius from the last draw. */
|
|
103
|
+
protected radius: number = 0;
|
|
86
104
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
this._initialized = true;
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
const watchedProps = ['width', 'margin', 'showLabels', 'data'];
|
|
93
|
-
const hasChanged = watchedProps.some(prop => changedProperties.has(prop));
|
|
94
|
-
if (hasChanged) {
|
|
95
|
-
this._debouncedRenderChart();
|
|
96
|
-
}
|
|
97
|
-
}
|
|
105
|
+
/** Pie centre from the last draw, in plot coordinates. */
|
|
106
|
+
private center = { x: 0, y: 0 };
|
|
98
107
|
|
|
99
|
-
|
|
100
|
-
// Reserve 100px on each side for labels and padding
|
|
101
|
-
return this.width / 2 - this.margin - 100;
|
|
102
|
-
}
|
|
108
|
+
// ── Private methods
|
|
103
109
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
.sort(null)
|
|
107
|
-
.value(d => d.value);
|
|
108
|
-
return pieGenerator(this.data);
|
|
110
|
+
protected drawProperties(): string[] {
|
|
111
|
+
return [...super.drawProperties(), 'data', 'innerRadius', 'showLabels'];
|
|
109
112
|
}
|
|
110
113
|
|
|
111
|
-
|
|
112
|
-
return
|
|
113
|
-
.domain(this.data.map(d => d.name))
|
|
114
|
-
.range(chartColors);
|
|
114
|
+
protected hasData(): boolean {
|
|
115
|
+
return this.data.some(item => item.value > 0);
|
|
115
116
|
}
|
|
116
117
|
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
protected getSeries(): ChartSeries[] {
|
|
119
|
+
return this.data.map((item, i) => ({
|
|
120
|
+
key: item.name,
|
|
121
|
+
label: item.label ?? item.name,
|
|
122
|
+
color: item.color ?? chartColor(i),
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
119
125
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const colorScale = this._getColorScale();
|
|
126
|
+
protected legendVisible(): boolean {
|
|
127
|
+
return !this.hideLegend;
|
|
128
|
+
}
|
|
124
129
|
|
|
125
|
-
|
|
130
|
+
protected getItemCount(): number {
|
|
131
|
+
return this.data.length;
|
|
132
|
+
}
|
|
126
133
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
134
|
+
/** Sum of the visible slices. */
|
|
135
|
+
protected visibleTotal(): number {
|
|
136
|
+
return this.data
|
|
137
|
+
.filter(item => !this.isHidden(item.name))
|
|
138
|
+
.reduce((sum, item) => sum + Math.max(item.value, 0), 0);
|
|
139
|
+
}
|
|
130
140
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
141
|
+
protected percent(value: number): string {
|
|
142
|
+
const total = this.visibleTotal();
|
|
143
|
+
return total
|
|
144
|
+
? new Intl.NumberFormat(undefined, {
|
|
145
|
+
style: 'percent',
|
|
146
|
+
maximumFractionDigits: 1,
|
|
147
|
+
}).format(value / total)
|
|
148
|
+
: '';
|
|
149
|
+
}
|
|
134
150
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
.select('.chart-container')
|
|
139
|
-
.attr('transform', `translate(${this.width / 2},${this.width / 2})`);
|
|
151
|
+
private innerFraction(): number {
|
|
152
|
+
return Math.min(Math.max(this.innerRadius || 0, 0), 0.9);
|
|
153
|
+
}
|
|
140
154
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
155
|
+
protected draw(animate: boolean): void {
|
|
156
|
+
if (!this.svgElement) return;
|
|
157
|
+
const { width, height } = this.svgSize();
|
|
158
|
+
const radius = Math.max(Math.min(width, height) / 2 - PADDING, 0);
|
|
159
|
+
this.radius = radius;
|
|
160
|
+
this.center = { x: width / 2, y: height / 2 };
|
|
161
|
+
const inner = radius * this.innerFraction();
|
|
162
|
+
const colors = new Map(this.getSeries().map(s => [s.key, s.color]));
|
|
163
|
+
|
|
164
|
+
const arcs = pie<ChartPieItem>()
|
|
165
|
+
.sort(null)
|
|
166
|
+
.value(item => (this.isHidden(item.name) ? 0 : Math.max(item.value, 0)))
|
|
167
|
+
.padAngle(radius ? SLICE_GAP / radius : 0)(this.data);
|
|
168
|
+
this.arcs = arcs;
|
|
169
|
+
|
|
170
|
+
const sliceArc = arc<{
|
|
171
|
+
startAngle: number;
|
|
172
|
+
endAngle: number;
|
|
173
|
+
padAngle?: number;
|
|
174
|
+
}>()
|
|
175
|
+
.innerRadius(inner)
|
|
176
|
+
.outerRadius(radius)
|
|
177
|
+
.cornerRadius(Math.min(4, (radius - inner) / 4));
|
|
178
|
+
|
|
179
|
+
const root = select(this.svgElement)
|
|
180
|
+
.select('.pie')
|
|
181
|
+
.attr('transform', `translate(${this.center.x},${this.center.y})`);
|
|
182
|
+
|
|
183
|
+
const slices = root
|
|
184
|
+
.select('.slices')
|
|
185
|
+
.selectAll<SVGPathElement, SliceArc>('path.slice')
|
|
186
|
+
.data(arcs, d => d.data.name)
|
|
146
187
|
.join('path')
|
|
147
|
-
.attr('class', '
|
|
148
|
-
.
|
|
188
|
+
.attr('class', 'slice mark')
|
|
189
|
+
.attr('data-key', d => d.data.name)
|
|
190
|
+
.style('fill', d => colors.get(d.data.name) ?? '');
|
|
149
191
|
|
|
150
192
|
if (animate) {
|
|
151
|
-
|
|
193
|
+
slices
|
|
152
194
|
.transition()
|
|
153
195
|
.duration(DURATION)
|
|
154
|
-
.ease(
|
|
155
|
-
.attrTween('d', function (
|
|
156
|
-
const
|
|
157
|
-
//
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return (t:
|
|
168
|
-
pieArc({ ...d, startAngle: iStart(t), endAngle: iEnd(t) }) ?? '';
|
|
196
|
+
.ease(easeCubicOut)
|
|
197
|
+
.attrTween('d', function (d) {
|
|
198
|
+
const el = this as SlicePath;
|
|
199
|
+
// New slices sweep open from their start angle.
|
|
200
|
+
const from = el.__current ?? {
|
|
201
|
+
startAngle: d.startAngle,
|
|
202
|
+
endAngle: d.startAngle,
|
|
203
|
+
};
|
|
204
|
+
const tween = interpolate(from, {
|
|
205
|
+
startAngle: d.startAngle,
|
|
206
|
+
endAngle: d.endAngle,
|
|
207
|
+
});
|
|
208
|
+
el.__current = { startAngle: d.startAngle, endAngle: d.endAngle };
|
|
209
|
+
return t => sliceArc({ ...tween(t), padAngle: d.padAngle }) ?? '';
|
|
169
210
|
});
|
|
170
211
|
} else {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
212
|
+
slices.interrupt().each(function (d) {
|
|
213
|
+
(this as SlicePath).__current = {
|
|
214
|
+
startAngle: d.startAngle,
|
|
215
|
+
endAngle: d.endAngle,
|
|
216
|
+
};
|
|
217
|
+
});
|
|
218
|
+
slices.attr('d', d => sliceArc(d) ?? '');
|
|
177
219
|
}
|
|
178
220
|
|
|
179
|
-
//
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
.
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
.transition()
|
|
227
|
-
.duration(DURATION)
|
|
228
|
-
.ease(easeCubicInOut)
|
|
229
|
-
.attr('transform', transformFn)
|
|
230
|
-
.style('text-anchor', anchorFn);
|
|
231
|
-
} else {
|
|
232
|
-
$polylines.attr('points', pointsFn);
|
|
233
|
-
$labels.attr('transform', transformFn).style('text-anchor', anchorFn);
|
|
234
|
-
}
|
|
235
|
-
} else {
|
|
236
|
-
$chartContainer.selectAll('.item-polyline').remove();
|
|
237
|
-
$chartContainer.selectAll('.item-label').remove();
|
|
221
|
+
// Percentages inside slices big enough to hold them.
|
|
222
|
+
const labelArc = arc<SliceArc>()
|
|
223
|
+
.innerRadius(inner + (radius - inner) / 2)
|
|
224
|
+
.outerRadius(inner + (radius - inner) / 2);
|
|
225
|
+
const labelled = this.showLabels
|
|
226
|
+
? arcs.filter(d => d.endAngle - d.startAngle >= MIN_LABEL_ANGLE)
|
|
227
|
+
: [];
|
|
228
|
+
root
|
|
229
|
+
.select('.slice-labels')
|
|
230
|
+
.selectAll<SVGTextElement, SliceArc>('text')
|
|
231
|
+
.data(labelled, d => d.data.name)
|
|
232
|
+
.join('text')
|
|
233
|
+
.attr('class', 'slice-label')
|
|
234
|
+
.attr('text-anchor', 'middle')
|
|
235
|
+
.attr('dominant-baseline', 'middle')
|
|
236
|
+
.attr('transform', d => `translate(${labelArc.centroid(d)})`)
|
|
237
|
+
.text(d => this.percent(d.data.value));
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
protected drawHighlight(): void {
|
|
241
|
+
if (!this.svgElement) return;
|
|
242
|
+
const { activeIndex, activeSeries } = this;
|
|
243
|
+
const activeName =
|
|
244
|
+
activeIndex >= 0 ? this.data[activeIndex]?.name : activeSeries;
|
|
245
|
+
select(this.svgElement)
|
|
246
|
+
.select('.slices')
|
|
247
|
+
.selectAll<SVGPathElement, SliceArc>('path.slice')
|
|
248
|
+
.classed(
|
|
249
|
+
'is-dimmed',
|
|
250
|
+
d => activeName !== undefined && d.data.name !== activeName,
|
|
251
|
+
)
|
|
252
|
+
.style('transform', d => {
|
|
253
|
+
if (d.data.name !== activeName) return 'translate(0px, 0px)';
|
|
254
|
+
const mid = (d.startAngle + d.endAngle) / 2;
|
|
255
|
+
return `translate(${Math.sin(mid) * PULL_OUT}px, ${-Math.cos(mid) * PULL_OUT}px)`;
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
protected itemAtPoint(x: number, y: number): number {
|
|
260
|
+
const dx = x - this.center.x;
|
|
261
|
+
const dy = y - this.center.y;
|
|
262
|
+
const distance = Math.hypot(dx, dy);
|
|
263
|
+
if (
|
|
264
|
+
distance > this.radius + PULL_OUT ||
|
|
265
|
+
distance < this.radius * this.innerFraction()
|
|
266
|
+
) {
|
|
267
|
+
return -1;
|
|
238
268
|
}
|
|
269
|
+
let angle = Math.atan2(dx, -dy);
|
|
270
|
+
if (angle < 0) angle += Math.PI * 2;
|
|
271
|
+
const hit = this.arcs.find(
|
|
272
|
+
d =>
|
|
273
|
+
d.endAngle > d.startAngle &&
|
|
274
|
+
angle >= d.startAngle &&
|
|
275
|
+
angle < d.endAngle,
|
|
276
|
+
);
|
|
277
|
+
return hit ? hit.index : -1;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
protected getTooltip(index: number): ChartTooltipModel | undefined {
|
|
281
|
+
const item = this.data[index];
|
|
282
|
+
const slice = this.arcs.find(d => d.index === index);
|
|
283
|
+
if (!item || !slice || this.isHidden(item.name)) return undefined;
|
|
284
|
+
const mid = (slice.startAngle + slice.endAngle) / 2;
|
|
285
|
+
const reach = this.radius * 0.75;
|
|
286
|
+
return {
|
|
287
|
+
title: '',
|
|
288
|
+
rows: [
|
|
289
|
+
{
|
|
290
|
+
key: item.name,
|
|
291
|
+
label: item.label ?? item.name,
|
|
292
|
+
color: item.color ?? chartColor(index),
|
|
293
|
+
value: `${this.formatValue(item.value)} · ${this.percent(item.value)}`,
|
|
294
|
+
},
|
|
295
|
+
],
|
|
296
|
+
x: this.center.x + Math.sin(mid) * reach,
|
|
297
|
+
y: this.center.y - Math.cos(mid) * reach,
|
|
298
|
+
};
|
|
239
299
|
}
|
|
240
300
|
|
|
241
|
-
|
|
242
|
-
return
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
301
|
+
protected getSelectDetail(index: number) {
|
|
302
|
+
return { index, series: this.data[index]?.name, datum: this.data[index] };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ── Render helpers
|
|
306
|
+
|
|
307
|
+
protected renderSkeleton() {
|
|
308
|
+
return html`<div
|
|
309
|
+
class="skeleton skeleton-circle"
|
|
310
|
+
style=${styleMap({ height: `${this.height}px` })}
|
|
311
|
+
aria-hidden="true"
|
|
312
|
+
>
|
|
313
|
+
<span></span>
|
|
314
|
+
</div>`;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
protected renderSvgContent() {
|
|
318
|
+
return svg`<g class="pie"><g class="slices"></g><g class="slice-labels"></g></g>`;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
protected renderTable() {
|
|
322
|
+
return html`<thead>
|
|
323
|
+
<tr>
|
|
324
|
+
<th scope="col">Name</th>
|
|
325
|
+
<th scope="col">Value</th>
|
|
326
|
+
<th scope="col">Share</th>
|
|
327
|
+
</tr>
|
|
328
|
+
</thead>
|
|
329
|
+
<tbody>
|
|
330
|
+
${this.data.map(
|
|
331
|
+
item =>
|
|
332
|
+
html`<tr>
|
|
333
|
+
<th scope="row">${item.label ?? item.name}</th>
|
|
334
|
+
<td>${this.formatValue(item.value)}</td>
|
|
335
|
+
<td>${this.percent(item.value)}</td>
|
|
336
|
+
</tr>`,
|
|
337
|
+
)}
|
|
338
|
+
</tbody>`;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
declare global {
|
|
343
|
+
interface HTMLElementTagNameMap {
|
|
344
|
+
'wc-chart-pie': ChartPie;
|
|
251
345
|
}
|
|
252
346
|
}
|
|
@@ -1,49 +1,41 @@
|
|
|
1
1
|
<!doctype html>
|
|
2
|
-
<html lang='en-GB'
|
|
2
|
+
<html lang='en-GB'>
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset='utf-8'>
|
|
5
5
|
<meta name='viewport' content='width=device-width, initial-scale=1.0, viewport-fit=cover' />
|
|
6
6
|
<link rel='stylesheet' href='/dist/assets/styles.css' />
|
|
7
|
-
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@100..900&family=Noto+Sans:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet">
|
|
8
|
-
|
|
9
7
|
<style>
|
|
10
8
|
body {
|
|
11
9
|
background: var(--color-surface);
|
|
10
|
+
max-width: 480px;
|
|
11
|
+
margin: 2rem auto;
|
|
12
12
|
}
|
|
13
13
|
</style>
|
|
14
14
|
</head>
|
|
15
15
|
<body>
|
|
16
16
|
|
|
17
|
-
<wc-button id=
|
|
18
|
-
<br />
|
|
17
|
+
<wc-button id='randomize'>Randomize</wc-button>
|
|
19
18
|
|
|
20
|
-
<wc-chart-pie
|
|
19
|
+
<wc-chart-pie label='Visitors by browser' show-labels></wc-chart-pie>
|
|
21
20
|
|
|
22
21
|
<script>
|
|
23
22
|
const chart = document.querySelector('wc-chart-pie');
|
|
24
|
-
chart.label = "Browsers";
|
|
25
23
|
chart.data = [
|
|
26
|
-
{
|
|
27
|
-
{
|
|
28
|
-
{
|
|
29
|
-
{
|
|
30
|
-
{
|
|
24
|
+
{ name: 'chrome', label: 'Chrome', value: 275 },
|
|
25
|
+
{ name: 'safari', label: 'Safari', value: 200 },
|
|
26
|
+
{ name: 'firefox', label: 'Firefox', value: 187 },
|
|
27
|
+
{ name: 'edge', label: 'Edge', value: 173 },
|
|
28
|
+
{ name: 'other', label: 'Other', value: 90 },
|
|
31
29
|
];
|
|
32
30
|
|
|
33
|
-
function randomNumber(min, max) {
|
|
34
|
-
return parseInt(Math.random() * (max - min) + min);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let count = 1;
|
|
38
31
|
document.querySelector('#randomize').addEventListener('click', () => {
|
|
39
|
-
chart.data = chart.data.map(d => ({ ...d, value:
|
|
40
|
-
chart.width = randomNumber(400, 600);
|
|
32
|
+
chart.data = chart.data.map(d => ({ ...d, value: Math.round(Math.random() * 300) + 20 }));
|
|
41
33
|
});
|
|
42
34
|
</script>
|
|
43
35
|
|
|
44
|
-
|
|
45
36
|
<script type='module'>
|
|
46
|
-
import { Button
|
|
37
|
+
import { Button } from '/dist/button.js';
|
|
38
|
+
import { ChartPie } from '/dist/chart-pie.js';
|
|
47
39
|
window.customElements.define('wc-chart-pie', ChartPie);
|
|
48
40
|
window.customElements.define('wc-button', Button);
|
|
49
41
|
</script>
|