@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
@@ -1,252 +1,346 @@
1
- import { html, LitElement, PropertyValues } from 'lit';
2
- import { property, query } from 'lit/decorators.js';
3
- import { select, arc, pie, scaleOrdinal, PieArcDatum, easeCubicInOut, interpolateNumber } from 'd3';
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 ChartPieColor = {
8
- color: string;
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
- const chartColors: ChartPieColor[] = [];
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
- /** SVGPathElement augmented with the last rendered arc datum for smooth tween interpolation. */
26
- interface ArcPathElement extends SVGPathElement {
27
- _prevDatum?: PieArcDatum<ChartPieItem>;
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
- function debounce<T extends (...args: any[]) => void>(fn: T, wait: number): T {
31
- let timer: ReturnType<typeof setTimeout>;
32
- return ((...args: any[]) => {
33
- clearTimeout(timer);
34
- timer = setTimeout(() => fn(...args), wait);
35
- }) as T;
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 Pie
48
+ * @label Pie Chart
40
49
  * @tag wc-chart-pie
41
50
  * @rawTag chart-pie
42
- * @summary A pie chart is a circular statistical graphic that visually represents numerical proportions.
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 width="400"></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: 'A', value: 30, label: 'Category A' },
51
- * { name: 'B', value: 50, label: 'Category B' },
52
- * { name: 'C', value: 20, label: 'Category C' },
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 LitElement {
59
- static styles = [styles];
79
+ export class ChartPie extends ChartBase {
80
+ // ── Static
60
81
 
61
- @query('svg')
62
- private svgElement?: SVGElement;
82
+ static styles = [baseStyles, styles];
63
83
 
64
- /** Width (and height) of the chart in pixels. */
65
- @property({ type: Number, reflect: true }) width: number = 0;
84
+ // ── Properties
66
85
 
67
- /** Margin around the chart. */
68
- @property({ type: Number, reflect: true }) margin: number = 10;
86
+ /** Slices to draw. */
87
+ @property({ type: Array }) data: ChartPieItem[] = [];
69
88
 
70
- /** Whether to show labels outside the chart. */
71
- @property({ type: Boolean, reflect: true, attribute: 'show-labels' })
72
- showLabels: boolean = true;
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
- /** Chart data array. Each item should have name, value, and optional label and color. */
75
- @property({ type: Array }) data: ChartPieItem[] = [];
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
- private _initialized = false;
97
+ // ── Private fields
78
98
 
79
- private _debouncedRenderChart = debounce(() => {
80
- this._renderChart(true);
81
- }, 300);
99
+ /** Arcs from the last draw. */
100
+ private arcs: SliceArc[] = [];
82
101
 
83
- firstUpdated() {
84
- this._renderChart(false);
85
- }
102
+ /** Outer radius from the last draw. */
103
+ protected radius: number = 0;
86
104
 
87
- updated(changedProperties: PropertyValues) {
88
- if (!this._initialized) {
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
- private _getRadius(): number {
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
- private _getPieData() {
105
- const pieGenerator = pie<ChartPieItem>()
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
- private _getColorScale() {
112
- return scaleOrdinal<string, ChartPieColor>()
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
- private _renderChart(animate: boolean) {
118
- if (!this.svgElement) return;
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
- const DURATION = 500;
121
- const radius = this._getRadius();
122
- const pieData = this._getPieData();
123
- const colorScale = this._getColorScale();
126
+ protected legendVisible(): boolean {
127
+ return !this.hideLegend;
128
+ }
124
129
 
125
- const svg = select(this.svgElement);
130
+ protected getItemCount(): number {
131
+ return this.data.length;
132
+ }
126
133
 
127
- const pieArc = arc<PieArcDatum<ChartPieItem>>()
128
- .innerRadius(0)
129
- .outerRadius(radius);
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
- const labelsArc = arc<PieArcDatum<ChartPieItem>>()
132
- .innerRadius(radius + 10)
133
- .outerRadius(radius + 10);
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
- // Update SVG dimensions and center transform
136
- svg.attr('width', this.width).attr('height', this.width);
137
- svg
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
- // Arc paths — keyed by name so D3 matches elements across updates
142
- const $paths = svg
143
- .select('.arc-container')
144
- .selectAll<SVGPathElement, PieArcDatum<ChartPieItem>>('.arc')
145
- .data(pieData, d => d.data.name)
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', 'arc')
148
- .style('fill', d => d.data.color || colorScale(d.data.name).color);
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
- $paths
193
+ slices
152
194
  .transition()
153
195
  .duration(DURATION)
154
- .ease(easeCubicInOut)
155
- .attrTween('d', function (this: SVGPathElement, d) {
156
- const self = this as ArcPathElement;
157
- // Interpolate from the last rendered angles to the new ones.
158
- // New (entering) arcs start collapsed at their startAngle.
159
- const prev: { startAngle: number; endAngle: number } =
160
- self._prevDatum ?? {
161
- startAngle: d.startAngle,
162
- endAngle: d.startAngle,
163
- };
164
- self._prevDatum = d;
165
- const iStart = interpolateNumber(prev.startAngle, d.startAngle);
166
- const iEnd = interpolateNumber(prev.endAngle, d.endAngle);
167
- return (t: number) =>
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
- // Initial render: draw immediately and seed previous-datum for later tweens
172
- $paths
173
- .each(function (this: SVGPathElement, d) {
174
- (this as ArcPathElement)._prevDatum = d;
175
- })
176
- .attr('d', d => pieArc(d) ?? '');
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
- // Label polylines and text
180
- const $chartContainer = svg.select('.chart-container');
181
-
182
- if (this.showLabels) {
183
- const pointsFn = (d: PieArcDatum<ChartPieItem>) => {
184
- const posA = pieArc.centroid(d);
185
- const posB = labelsArc.centroid(d);
186
- const posC = posB.slice() as [number, number];
187
- const midAngle = d.startAngle + (d.endAngle - d.startAngle) / 2;
188
- posC[0] = radius * (midAngle < Math.PI ? 1 : -1);
189
- return [posA, posB, posC].map(p => p.join(',')).join(' ');
190
- };
191
-
192
- const transformFn = (d: PieArcDatum<ChartPieItem>) => {
193
- const pos = labelsArc.centroid(d);
194
- const midAngle = d.startAngle + (d.endAngle - d.startAngle) / 2;
195
- pos[0] = radius * (midAngle < Math.PI ? 1 : -1);
196
- return `translate(${pos})`;
197
- };
198
-
199
- const anchorFn = (d: PieArcDatum<ChartPieItem>) => {
200
- const midAngle = d.startAngle + (d.endAngle - d.startAngle) / 2;
201
- return midAngle < Math.PI ? 'start' : 'end';
202
- };
203
-
204
- const $polylines = $chartContainer
205
- .selectAll<SVGPolylineElement, PieArcDatum<ChartPieItem>>(
206
- '.item-polyline',
207
- )
208
- .data(pieData, d => d.data.name)
209
- .join('polyline')
210
- .attr('class', 'item-polyline');
211
-
212
- const $labels = $chartContainer
213
- .selectAll<SVGTextElement, PieArcDatum<ChartPieItem>>('.item-label')
214
- .data(pieData, d => d.data.name)
215
- .join('text')
216
- .attr('class', 'item-label')
217
- .text(d => d.data.label ?? '');
218
-
219
- if (animate) {
220
- $polylines
221
- .transition()
222
- .duration(DURATION)
223
- .ease(easeCubicInOut)
224
- .attr('points', pointsFn);
225
- $labels
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
- render() {
242
- return html`
243
- <div class="chart">
244
- <svg>
245
- <g class="chart-container">
246
- <g class="arc-container"></g>
247
- </g>
248
- </svg>
249
- </div>
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' data-theme=dark>
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="randomize">Randomize</wc-button>
18
- <br />
17
+ <wc-button id='randomize'>Randomize</wc-button>
19
18
 
20
- <wc-chart-pie width="400" margin="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
- { label: 'Firefox', name: 'firefox', value: 10},
27
- { label: 'Chrome', name: 'chrome', value: 20},
28
- { label: 'Microsoft Edge', name: 'microsoft-edge', value: 30 },
29
- { label: 'Internet Explorer', name: 'internet-explorer', value: 40 },
30
- { label: 'MC Browser', name: 'mc-browser', value: 50 },
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: parseInt(Math.random() * 100) }));
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, ChartPie } from '/dist/index.js';
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>