@nanahoshi/mona-ui 0.26.0 → 0.26.1
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/chart/README.md +2 -2
- package/docs/chart-bar.md +714 -0
- package/docs/chart-financial.md +567 -0
- package/docs/chart-funnel.md +546 -0
- package/docs/chart-heatmap.md +535 -0
- package/docs/chart-line-area.md +462 -0
- package/docs/chart-pie.md +645 -0
- package/docs/chart-radar.md +518 -0
- package/docs/chart-radial.md +658 -0
- package/docs/chart-scatter.md +479 -0
- package/docs/chart-treemap.md +651 -0
- package/docs/chart.md +263 -1856
- package/fesm2022/nanahoshi-mona-ui-chart.mjs +126 -66
- package/llms.txt +11 -1
- package/package.json +1 -1
- package/types/nanahoshi-mona-ui-chart.d.ts +50 -20
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
# Pie & Donut
|
|
2
|
+
|
|
3
|
+
> Pie and donut series for the parts of a whole, with slice labels and a donut center.
|
|
4
|
+
|
|
5
|
+
Entry point: `@nanahoshi/mona-ui/chart`
|
|
6
|
+
|
|
7
|
+
**Selectors:** `mona-pie-series`, `mona-donut-series`
|
|
8
|
+
|
|
9
|
+
Pie and donut series show how a whole splits into parts. Each row of data becomes a slice whose angle is its share of the total, and a donut leaves a hole in the middle for a total or a short summary. They go inside a `mona-chart` without axes, take one slice label per slice inside or outside the circle, and let the legend hide slices one by one.
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
Pick the series by what else the chart has to say.
|
|
14
|
+
|
|
15
|
+
| Series | Use it for | Reads |
|
|
16
|
+
| :--- | :--- | :--- |
|
|
17
|
+
| `mona-pie-series` | A few parts of one whole, where the reader should see at a glance which part is largest. | `field`, `categoryField` |
|
|
18
|
+
| `mona-donut-series` | The same, with the total or a summary in the hole, through `ng-template[monaChartCenterTemplate]`. | `field`, `categoryField` |
|
|
19
|
+
|
|
20
|
+
**Use something else when:**
|
|
21
|
+
|
|
22
|
+
- There are more than five or six parts, or they're close in size. Angles are hard to compare, and bars show small differences better, see [Bar & Waterfall](chart-bar.md).
|
|
23
|
+
- You need to compare the parts of several wholes, such as each year's split. A chart takes one pie or donut, so use stacked bars with `stackMode="percent"`, see [Bar & Waterfall](chart-bar.md).
|
|
24
|
+
- The parts form a hierarchy. Use a [Treemap](chart-treemap.md).
|
|
25
|
+
- The values are stages that narrow, such as a sign-up flow. Use a [Funnel](chart-funnel.md).
|
|
26
|
+
- You want to show one value against a target. Use a gauge, see [Radial](chart-radial.md).
|
|
27
|
+
|
|
28
|
+
The chart, legend, tooltip and templates these series sit in are described on [Chart Overview](chart.md).
|
|
29
|
+
|
|
30
|
+
## Import & Quick Start
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { ChartComponent, ChartLegendComponent, ChartTooltipComponent, PieSeriesComponent } from "@nanahoshi/mona-ui/chart";
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```html
|
|
37
|
+
<mona-chart aria-label="Visits by traffic source" class="h-72 w-full" [data]="sources">
|
|
38
|
+
<mona-pie-series categoryField="source" field="visits" name="Visits" />
|
|
39
|
+
<mona-chart-legend position="end" />
|
|
40
|
+
<mona-chart-tooltip />
|
|
41
|
+
</mona-chart>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Give the chart a height. It fills its host, and a host without a height has none. Pie and donut charts have no axes, so leave out `mona-chart-x-axis` and `mona-chart-y-axis`, and the chart's `xField`.
|
|
45
|
+
|
|
46
|
+
## Anatomy
|
|
47
|
+
|
|
48
|
+
Each row of data is one slice. The series reads its rows from the chart's `data`, unless it has its own `data`, and takes the value from `field` and the slice's name from `categoryField`. A field is a property name or an accessor, `(row, index) => value`. The defaults are `"value"` and `"category"`, so rows shaped `{ category, value }` need neither.
|
|
49
|
+
|
|
50
|
+
| Input | Pie | Donut | What it reads or sets |
|
|
51
|
+
| :--- | :---: | :---: | :--- |
|
|
52
|
+
| `field`, `categoryField` | ✓ | ✓ | The value and the name of each slice. |
|
|
53
|
+
| `colorField`, `colors` | ✓ | ✓ | The color of each slice. |
|
|
54
|
+
| `keyField` | ✓ | ✓ | A stable identity for each row, so slices animate as the same slice when the data changes. |
|
|
55
|
+
| `valueFormatter`, `categoryFormatter` | ✓ | ✓ | How values and names read in the tooltip, labels, legend and announcements. |
|
|
56
|
+
| `startAngle`, `endAngle`, `padAngle`, `cornerRadius`, `outerRadiusRatio` | ✓ | ✓ | The shape of the circle and its slices. |
|
|
57
|
+
| `innerRadiusRatio` | | ✓ | The size of the hole. |
|
|
58
|
+
| `showLabels`, `labelPosition`, `labelContent`, `minLabelAngle` | ✓ | ✓ | The slice labels. |
|
|
59
|
+
| `fillMode`, `fillOpacity`, `strokeColor`, `strokeWidth` | ✓ | ✓ | The fill and the lines between slices. |
|
|
60
|
+
| `[(hiddenIndices)]`, `[(visible)]` | ✓ | ✓ | Which slices are hidden, and whether the series is shown. |
|
|
61
|
+
|
|
62
|
+
A chart takes one pie or donut series. With a second one, the chart draws neither and logs a development warning. They also can't share a chart with Cartesian series, such as lines or bars.
|
|
63
|
+
|
|
64
|
+
### Templates
|
|
65
|
+
|
|
66
|
+
- `ng-template[monaChartSliceLabelTemplate]`, inside either series, replaces the text of each slice label. The context is a `ChartSliceLabelTemplateContext`: the slice is `$implicit` and `slice`, and its fields, such as `formattedCategory`, `formattedValue`, `formattedPercentage`, `percentage`, `color`, `dataIndex` and `datum`, are also on the context itself. Turn `showLabels` on as well.
|
|
67
|
+
- `ng-template[monaChartCenterTemplate]`, inside a donut, fills the hole. The context is a `ChartCenterTemplateContext`, with `total` (also `$implicit`), `formattedTotal`, `visibleSliceCount`, `seriesId` and `seriesName`.
|
|
68
|
+
- The chart, legend and tooltip templates are described on [Chart Overview](chart.md).
|
|
69
|
+
|
|
70
|
+
## Examples
|
|
71
|
+
|
|
72
|
+
### Donut
|
|
73
|
+
|
|
74
|
+
A donut with the total in its hole. The rows are shaped `{ category, value }`, so the series needs no fields, and `padAngle` leaves a small gap between slices.
|
|
75
|
+
|
|
76
|
+
`donut-example.component.ts`
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { Component } from "@angular/core";
|
|
80
|
+
import {
|
|
81
|
+
ChartCenterTemplateDirective,
|
|
82
|
+
ChartComponent,
|
|
83
|
+
ChartLegendComponent,
|
|
84
|
+
DonutSeriesComponent
|
|
85
|
+
} from "@nanahoshi/mona-ui/chart";
|
|
86
|
+
|
|
87
|
+
@Component({
|
|
88
|
+
selector: "app-chart-pie-donut-example",
|
|
89
|
+
imports: [ChartCenterTemplateDirective, ChartComponent, ChartLegendComponent, DonutSeriesComponent],
|
|
90
|
+
templateUrl: "./donut-example.component.html"
|
|
91
|
+
})
|
|
92
|
+
export class ChartDonutExampleComponent {
|
|
93
|
+
protected readonly sources = [
|
|
94
|
+
{ category: "Search", value: 4200 },
|
|
95
|
+
{ category: "Direct", value: 2600 },
|
|
96
|
+
{ category: "Social", value: 1500 },
|
|
97
|
+
{ category: "Email", value: 900 }
|
|
98
|
+
];
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`donut-example.component.html`
|
|
103
|
+
|
|
104
|
+
```html
|
|
105
|
+
<mona-chart aria-label="Visits by traffic source" class="h-72 w-full" [data]="sources">
|
|
106
|
+
<mona-donut-series name="Visits" [padAngle]="1">
|
|
107
|
+
<ng-template monaChartCenterTemplate let-formattedTotal="formattedTotal">
|
|
108
|
+
<div class="flex flex-col items-center">
|
|
109
|
+
<span class="text-xs">Total visits</span>
|
|
110
|
+
<span class="text-lg font-semibold">{{ formattedTotal }}</span>
|
|
111
|
+
</div>
|
|
112
|
+
</ng-template>
|
|
113
|
+
</mona-donut-series>
|
|
114
|
+
<mona-chart-legend position="end" />
|
|
115
|
+
</mona-chart>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Outside labels
|
|
119
|
+
|
|
120
|
+
`showLabels` writes a label beside each slice, joined to it by a leader line. `labelContent="category-percentage"` names the slice and gives its share, so this pie needs no legend. `strokeColor` set to the surface color draws a gap between the slices, and `valueFormatter` adds the unit to the tooltip.
|
|
121
|
+
|
|
122
|
+
`outside-labels-example.component.ts`
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import { Component } from "@angular/core";
|
|
126
|
+
import { ChartComponent, ChartTooltipComponent, PieSeriesComponent } from "@nanahoshi/mona-ui/chart";
|
|
127
|
+
import type { ChartValueFormatter } from "@nanahoshi/mona-ui/chart";
|
|
128
|
+
|
|
129
|
+
@Component({
|
|
130
|
+
selector: "app-chart-pie-outside-labels-example",
|
|
131
|
+
imports: [ChartComponent, ChartTooltipComponent, PieSeriesComponent],
|
|
132
|
+
templateUrl: "./outside-labels-example.component.html"
|
|
133
|
+
})
|
|
134
|
+
export class ChartPieOutsideLabelsExampleComponent {
|
|
135
|
+
protected readonly browsers = [
|
|
136
|
+
{ browser: "Chrome", views: 61200 },
|
|
137
|
+
{ browser: "Safari", views: 18400 },
|
|
138
|
+
{ browser: "Edge", views: 9800 },
|
|
139
|
+
{ browser: "Firefox", views: 6100 },
|
|
140
|
+
{ browser: "Other", views: 4500 }
|
|
141
|
+
];
|
|
142
|
+
protected readonly views: ChartValueFormatter = value => `${Number(value).toLocaleString("en")} views`;
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
`outside-labels-example.component.html`
|
|
147
|
+
|
|
148
|
+
```html
|
|
149
|
+
<mona-chart aria-label="Page views by browser" class="h-80 w-full" [data]="browsers">
|
|
150
|
+
<mona-pie-series
|
|
151
|
+
categoryField="browser"
|
|
152
|
+
field="views"
|
|
153
|
+
labelContent="category-percentage"
|
|
154
|
+
name="Page views"
|
|
155
|
+
strokeColor="var(--color-surface)"
|
|
156
|
+
[showLabels]="true"
|
|
157
|
+
[strokeWidth]="2"
|
|
158
|
+
[valueFormatter]="views" />
|
|
159
|
+
<mona-chart-tooltip />
|
|
160
|
+
</mona-chart>
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Hiding slices
|
|
164
|
+
|
|
165
|
+
`[(hiddenIndices)]` holds the indices of the hidden rows. The legend and the buttons above the chart change the same list, and the center shows how many regions are left and their total.
|
|
166
|
+
|
|
167
|
+
`hidden-slices-example.component.ts`
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { Component, signal } from "@angular/core";
|
|
171
|
+
import { ButtonDirective } from "@nanahoshi/mona-ui/button";
|
|
172
|
+
import {
|
|
173
|
+
ChartCenterTemplateDirective,
|
|
174
|
+
ChartComponent,
|
|
175
|
+
ChartLegendComponent,
|
|
176
|
+
ChartTooltipComponent,
|
|
177
|
+
DonutSeriesComponent
|
|
178
|
+
} from "@nanahoshi/mona-ui/chart";
|
|
179
|
+
import type { ChartValueFormatter } from "@nanahoshi/mona-ui/chart";
|
|
180
|
+
|
|
181
|
+
const euros = new Intl.NumberFormat("en", { currency: "EUR", maximumFractionDigits: 0, style: "currency" });
|
|
182
|
+
|
|
183
|
+
@Component({
|
|
184
|
+
selector: "app-chart-pie-hidden-slices-example",
|
|
185
|
+
imports: [
|
|
186
|
+
ButtonDirective,
|
|
187
|
+
ChartCenterTemplateDirective,
|
|
188
|
+
ChartComponent,
|
|
189
|
+
ChartLegendComponent,
|
|
190
|
+
ChartTooltipComponent,
|
|
191
|
+
DonutSeriesComponent
|
|
192
|
+
],
|
|
193
|
+
templateUrl: "./hidden-slices-example.component.html"
|
|
194
|
+
})
|
|
195
|
+
export class ChartPieHiddenSlicesExampleComponent {
|
|
196
|
+
protected readonly hidden = signal<readonly number[]>([3]);
|
|
197
|
+
protected readonly regions = [
|
|
198
|
+
{ region: "Europe", revenue: 412000 },
|
|
199
|
+
{ region: "North America", revenue: 356000 },
|
|
200
|
+
{ region: "Asia Pacific", revenue: 228000 },
|
|
201
|
+
{ region: "Latin America", revenue: 94000 },
|
|
202
|
+
{ region: "Middle East & Africa", revenue: 61000 }
|
|
203
|
+
];
|
|
204
|
+
protected readonly currency: ChartValueFormatter = value => euros.format(Number(value));
|
|
205
|
+
|
|
206
|
+
protected isShown(index: number): boolean {
|
|
207
|
+
return !this.hidden().includes(index);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
protected setShown(index: number, shown: boolean): void {
|
|
211
|
+
this.hidden.update(hidden => (shown ? hidden.filter(other => other !== index) : [...hidden, index]));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
`hidden-slices-example.component.html`
|
|
217
|
+
|
|
218
|
+
```html
|
|
219
|
+
<div class="flex flex-col gap-4">
|
|
220
|
+
<div aria-label="Regions in the chart" class="flex flex-wrap gap-2" role="group">
|
|
221
|
+
@for (row of regions; track row.region; let index = $index) {
|
|
222
|
+
<button
|
|
223
|
+
monaButton
|
|
224
|
+
size="small"
|
|
225
|
+
[selected]="isShown(index)"
|
|
226
|
+
[toggleable]="true"
|
|
227
|
+
(selectedChange)="setShown(index, $event)">
|
|
228
|
+
{{ row.region }}
|
|
229
|
+
</button>
|
|
230
|
+
}
|
|
231
|
+
</div>
|
|
232
|
+
<mona-chart aria-label="Revenue by region" class="h-72 w-full" [data]="regions">
|
|
233
|
+
<mona-donut-series
|
|
234
|
+
categoryField="region"
|
|
235
|
+
field="revenue"
|
|
236
|
+
name="Revenue"
|
|
237
|
+
[valueFormatter]="currency"
|
|
238
|
+
[(hiddenIndices)]="hidden">
|
|
239
|
+
<ng-template monaChartCenterTemplate let-count="visibleSliceCount" let-formattedTotal="formattedTotal">
|
|
240
|
+
<div class="flex flex-col items-center">
|
|
241
|
+
<span class="text-xs">{{ count }} of {{ regions.length }} regions</span>
|
|
242
|
+
<span class="text-lg font-semibold">{{ formattedTotal }}</span>
|
|
243
|
+
</div>
|
|
244
|
+
</ng-template>
|
|
245
|
+
</mona-donut-series>
|
|
246
|
+
<mona-chart-legend position="end" />
|
|
247
|
+
<mona-chart-tooltip />
|
|
248
|
+
</mona-chart>
|
|
249
|
+
</div>
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### Slice label template
|
|
253
|
+
|
|
254
|
+
With `labelPosition="inside"`, a template puts the name and the share on each slice. `colorField` takes each slice's color from its row. A slice narrower than `minLabelAngle`, here Training, gets no label, and the legend and tooltip still name it.
|
|
255
|
+
|
|
256
|
+
`slice-template-example.component.ts`
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
import { Component } from "@angular/core";
|
|
260
|
+
import {
|
|
261
|
+
ChartComponent,
|
|
262
|
+
ChartLegendComponent,
|
|
263
|
+
ChartSliceLabelTemplateDirective,
|
|
264
|
+
ChartTooltipComponent,
|
|
265
|
+
PieSeriesComponent
|
|
266
|
+
} from "@nanahoshi/mona-ui/chart";
|
|
267
|
+
|
|
268
|
+
@Component({
|
|
269
|
+
selector: "app-chart-pie-slice-template-example",
|
|
270
|
+
imports: [
|
|
271
|
+
ChartComponent,
|
|
272
|
+
ChartLegendComponent,
|
|
273
|
+
ChartSliceLabelTemplateDirective,
|
|
274
|
+
ChartTooltipComponent,
|
|
275
|
+
PieSeriesComponent
|
|
276
|
+
],
|
|
277
|
+
templateUrl: "./slice-template-example.component.html"
|
|
278
|
+
})
|
|
279
|
+
export class ChartPieSliceTemplateExampleComponent {
|
|
280
|
+
protected readonly budget = [
|
|
281
|
+
{ amount: 540, color: "#1d4ed8", item: "Salaries" },
|
|
282
|
+
{ amount: 210, color: "#047857", item: "Infrastructure" },
|
|
283
|
+
{ amount: 150, color: "#b45309", item: "Marketing" },
|
|
284
|
+
{ amount: 60, color: "#be185d", item: "Travel" },
|
|
285
|
+
{ amount: 40, color: "#6d28d9", item: "Training" }
|
|
286
|
+
];
|
|
287
|
+
}
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
`slice-template-example.component.html`
|
|
291
|
+
|
|
292
|
+
```html
|
|
293
|
+
<mona-chart aria-label="Budget by item, in thousands of euros" class="h-80 w-full" [data]="budget">
|
|
294
|
+
<mona-pie-series
|
|
295
|
+
categoryField="item"
|
|
296
|
+
colorField="color"
|
|
297
|
+
field="amount"
|
|
298
|
+
labelPosition="inside"
|
|
299
|
+
name="Budget"
|
|
300
|
+
[cornerRadius]="4"
|
|
301
|
+
[minLabelAngle]="20"
|
|
302
|
+
[padAngle]="1.5"
|
|
303
|
+
[showLabels]="true">
|
|
304
|
+
<ng-template monaChartSliceLabelTemplate let-slice>
|
|
305
|
+
<span class="flex flex-col items-center leading-tight">
|
|
306
|
+
<span>{{ slice.formattedCategory }}</span>
|
|
307
|
+
<span class="text-sm">{{ slice.formattedPercentage }}</span>
|
|
308
|
+
</span>
|
|
309
|
+
</ng-template>
|
|
310
|
+
</mona-pie-series>
|
|
311
|
+
<mona-chart-legend position="end" />
|
|
312
|
+
<mona-chart-tooltip />
|
|
313
|
+
</mona-chart>
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Appearance & Styling
|
|
317
|
+
|
|
318
|
+
### Shape
|
|
319
|
+
|
|
320
|
+
| Input | Values | Effect |
|
|
321
|
+
| :--- | :--- | :--- |
|
|
322
|
+
| `startAngle`, `endAngle` | Degrees, default `0` and `360` | Where the slices begin and end, clockwise from 12 o'clock. `-90` to `90` draws a half circle across the top. When `endAngle` isn't after `startAngle`, the span wraps around, so `270` to `90` is also a half circle. A span is never more than a full circle. |
|
|
323
|
+
| `padAngle` | Degrees, default `0` | The gap between neighboring slices. All the gaps together never take more than 35% of the span, and a single visible slice has none. |
|
|
324
|
+
| `cornerRadius` | Pixels | Rounds the corners of each slice. It's capped at half the ring's thickness, or half the radius for a pie. |
|
|
325
|
+
| `outerRadiusRatio` | `0.1` to `1`, default `0.9` | The radius, as a share of the room the chart has for it. |
|
|
326
|
+
| `innerRadiusRatio` | `0` to `0.95`, default `0.6` | Donut only. The radius of the hole, as a share of the outer radius. `0` draws a pie. |
|
|
327
|
+
|
|
328
|
+
Values outside a range are clamped to it.
|
|
329
|
+
|
|
330
|
+
### Color and fill
|
|
331
|
+
|
|
332
|
+
| Input | Values | Effect |
|
|
333
|
+
| :--- | :--- | :--- |
|
|
334
|
+
| `colorField` | A field returning a CSS color | The color of each slice, read from its row. A row without one falls back to `colors`. |
|
|
335
|
+
| `colors` | CSS colors | The slice colors in order, repeated when there are more slices than colors. |
|
|
336
|
+
| `fillMode` | `"solid"`, `"gradient"` | A gradient is a translucent radial wash that grows stronger toward the outer edge. |
|
|
337
|
+
| `fillOpacity` | `0` to `1` | The opacity of the fill. A gradient is translucent already, and gets lighter still. |
|
|
338
|
+
| `strokeColor` | A CSS color | The color of the line around each slice. Set it to the background, such as `var(--color-surface)`, to separate the slices. |
|
|
339
|
+
| `strokeWidth` | Pixels, default `1` | The width of that line. `0` removes it. |
|
|
340
|
+
|
|
341
|
+
Without `colorField` or `colors`, slices take `--color-chart-1` to `--color-chart-5` in order, and the sixth slice starts over. Colors can be CSS colors or theme variables, written `var(--name)` or `--name`. Without `strokeColor`, each slice's outline is its own color, so the slices meet without a visible line.
|
|
342
|
+
|
|
343
|
+
### Slice labels
|
|
344
|
+
|
|
345
|
+
Slice labels are off by default. `showLabels` turns them on, in either position.
|
|
346
|
+
|
|
347
|
+
| Input | Values | Effect |
|
|
348
|
+
| :--- | :--- | :--- |
|
|
349
|
+
| `labelPosition` | `"outside"`, `"inside"` | Outside labels sit beside the circle, with a leader line to their slice. Inside labels sit on the slice. |
|
|
350
|
+
| `labelContent` | `"percentage"`, `"value"`, `"category"`, `"category-percentage"` | What a label says when there's no template. `"category-percentage"` reads as `Search: 42%`. |
|
|
351
|
+
| `minLabelAngle` | Degrees, default `12` | Inside labels only. A slice narrower than this gets no label. |
|
|
352
|
+
|
|
353
|
+
- **Outside labels take room.** The chart keeps space for them on the left and the right, so the circle gets smaller. Labels on each side are spread out so they don't overlap. When a side has more labels than height, the labels of the smallest slices are dropped first, and a label that would run past the edge of the plot is dropped too.
|
|
354
|
+
- **Inside labels pick their own text color**, white or dark, whichever contrasts more with the slice. They're centered a little past the middle of the ring.
|
|
355
|
+
- **Percentages are shares of what's visible.** When a slice is hidden, the others' labels and tooltips are worked out again from the slices that are left. They're formatted for the chart locale, with at most one decimal.
|
|
356
|
+
|
|
357
|
+
## Theming
|
|
358
|
+
|
|
359
|
+
These variables set the slice style without an input. They're read from the series element, so set them on the chart or on the series with a class. An input wins over its variable.
|
|
360
|
+
|
|
361
|
+
| Variable | Default | Sets |
|
|
362
|
+
| :--- | :--- | :--- |
|
|
363
|
+
| `--mona-chart-slice-stroke-color` | Each slice's own color | The line around each slice, like `strokeColor`. |
|
|
364
|
+
| `--mona-chart-slice-stroke-width` | `1` | Its width in pixels, like `strokeWidth`. |
|
|
365
|
+
| `--mona-chart-slice-fill-opacity` | `1` | The fill opacity, like `fillOpacity`. |
|
|
366
|
+
| `--mona-chart-slice-hover-overlay` | A translucent white | The color laid over the slice under the pointer. |
|
|
367
|
+
| `--mona-chart-label-line-color` | `--color-muted-foreground` | The leader lines of outside labels. |
|
|
368
|
+
|
|
369
|
+
The slice palette is `--color-chart-1` to `--color-chart-5`, shared with every chart, and the keyboard focus ring uses `--color-focus-indicator`.
|
|
370
|
+
|
|
371
|
+
## Behavior
|
|
372
|
+
|
|
373
|
+
### Values that draw no slice
|
|
374
|
+
|
|
375
|
+
A row whose value is `null`, `undefined`, zero, negative or not a finite number draws no slice and gets no legend entry. A numeric string, such as `"42"`, counts as its number. Negative and non-finite values log a development warning, and so does a series with no value above zero. A row without a name is called "Item" and its position, such as `Item 3`. The data can also be plain numbers, `[42, 18, 7]`, with those names.
|
|
376
|
+
|
|
377
|
+
### Hiding slices
|
|
378
|
+
|
|
379
|
+
- The legend lists each slice, and toggling an entry hides or shows that slice. It doesn't hide the series, so the chart's `seriesVisibilityChange` doesn't fire. The series emits `sliceVisibilityChange` instead, with the slice's `dataIndex`, `category`, `datum` and whether it's now `visible`.
|
|
380
|
+
- `[(hiddenIndices)]` holds the hidden slices as indices into the data, counting every row, including rows that draw no slice. The legend writes to it, and the series drops indices past the end of the data when the data gets shorter. Because they're positions, a hidden index stays with the position, not the item, when rows are reordered.
|
|
381
|
+
- `toggleSlice(dataIndex)`, a method on the series, shows or hides a slice from code, and returns whether it's now visible.
|
|
382
|
+
- A hidden slice leaves its room to the others, and the percentages, the donut's total and `visibleSliceCount` follow. In a legend item template, `percentage` is each slice's share of the whole total, hidden slices included, so it doesn't change as slices come and go.
|
|
383
|
+
- When every slice is hidden, the chart shows its no data state, and the legend stays so the user can bring them back.
|
|
384
|
+
|
|
385
|
+
### The donut center
|
|
386
|
+
|
|
387
|
+
The center template is drawn in the middle of the hole and limited to a box a little smaller than it. It appears only while the donut has a visible slice. `total` is the sum of the visible slices, and `formattedTotal` is that sum formatted by `valueFormatter`, called with the index `-1`, or by the chart locale without one.
|
|
388
|
+
|
|
389
|
+
### Value formatting
|
|
390
|
+
|
|
391
|
+
`valueFormatter` formats the values in the tooltip, value labels, screen reader announcements and the donut's `formattedTotal`. `categoryFormatter` formats the slice names in the legend, labels, tooltip and announcements. Both receive the value and the row's index.
|
|
392
|
+
|
|
393
|
+
### Two-way binding
|
|
394
|
+
|
|
395
|
+
`[(visible)]` holds whether the whole series is shown. The legend toggles slices, not the series, so it doesn't change `visible`. `[(hiddenIndices)]` is described under [Hiding slices](#hiding-slices).
|
|
396
|
+
|
|
397
|
+
## Accessibility
|
|
398
|
+
|
|
399
|
+
The chart is the focusable element, and these series add no roles of their own. When a slice gets focus, the chart announces the series name, the slice's name and formatted value, its percentage, and its position, such as 2 of 5. Give the series a `name`, and use `valueFormatter` to add a unit.
|
|
400
|
+
|
|
401
|
+
- **Name the chart.** Give it an `aria-label` or a `title`, as for any chart, see [Chart Overview](chart.md).
|
|
402
|
+
- **Don't rely on color alone.** Keep the legend, or turn on labels that name the slices with `labelContent="category"` or `"category-percentage"`. Slice colors next to each other should differ in lightness, and a `strokeColor` that matches the background helps tell them apart.
|
|
403
|
+
- **Labels are hidden from screen readers**, since the announcements already carry the same values. The content of the donut center template isn't hidden, so screen readers can reach it. Keep it short, and make its text meet contrast against the chart background.
|
|
404
|
+
- **The tooltip shows one slice at a time.** The tooltip's `shared` input has no effect on these series.
|
|
405
|
+
|
|
406
|
+
### Keyboard Navigation
|
|
407
|
+
|
|
408
|
+
| Key | Action |
|
|
409
|
+
| :--- | :--- |
|
|
410
|
+
| `ArrowRight` / `ArrowDown` | Move to the next slice, clockwise. After the last slice, go back to the first. |
|
|
411
|
+
| `ArrowLeft` / `ArrowUp` | Move to the previous slice. Before the first slice, go to the last. |
|
|
412
|
+
| `Home` / `End` | Move to the first or last visible slice. |
|
|
413
|
+
| `Enter` / `Space` | Activate the focused slice, which emits the chart's `pointClick`. |
|
|
414
|
+
|
|
415
|
+
The focused slice gets a ring in `--color-focus-indicator`. The legend's entries are buttons, toggled with `Enter` or `Space`. The rest of the chart's keys are listed on [Chart Overview](chart.md).
|
|
416
|
+
|
|
417
|
+
## API
|
|
418
|
+
|
|
419
|
+
Everything below is imported from `@nanahoshi/mona-ui/chart`.
|
|
420
|
+
|
|
421
|
+
### `DonutSeriesComponent`
|
|
422
|
+
|
|
423
|
+
Renders a full or partial donut. Use `ng-template[monaChartCenterTemplate]` for the center.
|
|
424
|
+
|
|
425
|
+
**Selector:** `mona-donut-series`
|
|
426
|
+
|
|
427
|
+
#### Inputs
|
|
428
|
+
|
|
429
|
+
| Name | Type | Default | Description |
|
|
430
|
+
| --- | --- | --- | --- |
|
|
431
|
+
| `categoryField` | `ChartField` | `"category"` | Property key or accessor extracting the category label for each slice. |
|
|
432
|
+
| `categoryFormatter` | `ChartValueFormatter \| undefined` | `undefined` | Formatter callback for slice category labels. |
|
|
433
|
+
| `class` | `string` | `""` | Additional CSS classes applied to the series. |
|
|
434
|
+
| `colorField` | `ChartField \| undefined` | `undefined` | Accessor function or property key extracting an explicit color for each slice. |
|
|
435
|
+
| `colors` | `readonly string[] \| undefined` | `undefined` | Explicit array of colors used to fill successive slices. |
|
|
436
|
+
| `cornerRadius` | `number \| undefined` | `undefined` | Corner radius in pixels applied to slice arc boundaries. |
|
|
437
|
+
| `data` | `readonly unknown[] \| undefined` | `undefined` | Series-specific dataset overriding root chart data. |
|
|
438
|
+
| `endAngle` | `number` | `360` | Ending angle in degrees (clockwise from 12 o'clock). |
|
|
439
|
+
| `field` | `ChartField` | `"value"` | Property key or accessor extracting the numeric slice value. |
|
|
440
|
+
| `fillMode` | `ChartPolarFillMode` | `"solid"` | Fill style applied to slices ("solid" or radial "gradient" from center to arc). |
|
|
441
|
+
| `fillOpacity` | `number \| undefined` | `undefined` | Opacity of slice fills (between 0 and 1). |
|
|
442
|
+
| `hiddenIndices` (two-way) | `readonly number[]` | `[]` | The hidden data indices. Bind `[(hiddenIndices)]` for two-way binding; legend toggles write to it. |
|
|
443
|
+
| `innerRadiusRatio` | `number` | `0.6` | Ratio of the inner hole radius relative to the outer radius (0 to 0.95). |
|
|
444
|
+
| `keyField` | `ChartField \| undefined` | `undefined` | Property key or accessor extracting a stable datum identity across updates. |
|
|
445
|
+
| `labelContent` | `ChartPolarLabelContent` | `"percentage"` | Content to display in default slice data labels. |
|
|
446
|
+
| `labelPosition` | `ChartPolarLabelPosition` | `"outside"` | Placement position for slice data labels ("outside" with leader lines or "inside" slice bounds). |
|
|
447
|
+
| `minLabelAngle` | `number` | `12` | Minimum slice arc angle in degrees required to render an inside data label. |
|
|
448
|
+
| `name` | `string` | `"Donut"` | Series name used in tooltips and accessibility announcements. |
|
|
449
|
+
| `outerRadiusRatio` | `number` | `0.9` | Outer radius ratio relative to available plot bounds (0.1 to 1). |
|
|
450
|
+
| `padAngle` | `number` | `0` | Angular padding in degrees between adjacent slices. |
|
|
451
|
+
| `showLabels` | `boolean` | `false` | Whether to display slice labels, inside or outside as `labelPosition` sets. |
|
|
452
|
+
| `startAngle` | `number` | `0` | Starting angle in degrees (0 is 12 o'clock, clockwise). |
|
|
453
|
+
| `strokeColor` | `string` | `""` | Color of slice separator strokes. |
|
|
454
|
+
| `strokeWidth` | `number \| undefined` | `undefined` | Stroke width in pixels for slice separators. |
|
|
455
|
+
| `valueFormatter` | `ChartValueFormatter \| undefined` | `undefined` | Formatter callback for slice numeric values. |
|
|
456
|
+
| `visible` (two-way) | `boolean` | `true` | Two-way bindable visibility of the series. |
|
|
457
|
+
|
|
458
|
+
#### Outputs
|
|
459
|
+
|
|
460
|
+
| Name | Type | Description |
|
|
461
|
+
| --- | --- | --- |
|
|
462
|
+
| `hiddenIndicesChange` | `readonly number[]` | Emitted when `hiddenIndices` changes. |
|
|
463
|
+
| `sliceVisibilityChange` | `ChartSliceVisibilityEvent` | Emits when an individual slice's visibility is toggled via the legend. |
|
|
464
|
+
| `visibleChange` | `boolean` | Emitted when `visible` changes. |
|
|
465
|
+
|
|
466
|
+
#### Methods
|
|
467
|
+
|
|
468
|
+
| Signature | Description |
|
|
469
|
+
| --- | --- |
|
|
470
|
+
| `toggleSlice(dataIndex: number): boolean` | Shows or hides the slice at `dataIndex`, and returns whether it's now visible. |
|
|
471
|
+
|
|
472
|
+
### `PieSeriesComponent`
|
|
473
|
+
|
|
474
|
+
Renders a full or partial pie.
|
|
475
|
+
|
|
476
|
+
**Selector:** `mona-pie-series`
|
|
477
|
+
|
|
478
|
+
#### Inputs
|
|
479
|
+
|
|
480
|
+
| Name | Type | Default | Description |
|
|
481
|
+
| --- | --- | --- | --- |
|
|
482
|
+
| `categoryField` | `ChartField` | `"category"` | Property key or accessor extracting the category label for each slice. |
|
|
483
|
+
| `categoryFormatter` | `ChartValueFormatter \| undefined` | `undefined` | Formatter callback for slice category labels. |
|
|
484
|
+
| `class` | `string` | `""` | Additional CSS classes applied to the series. |
|
|
485
|
+
| `colorField` | `ChartField \| undefined` | `undefined` | Accessor function or property key extracting an explicit color for each slice. |
|
|
486
|
+
| `colors` | `readonly string[] \| undefined` | `undefined` | Explicit array of colors used to fill successive slices. |
|
|
487
|
+
| `cornerRadius` | `number \| undefined` | `undefined` | Corner radius in pixels applied to slice arc boundaries. |
|
|
488
|
+
| `data` | `readonly unknown[] \| undefined` | `undefined` | Series-specific dataset overriding root chart data. |
|
|
489
|
+
| `endAngle` | `number` | `360` | Ending angle in degrees (clockwise from 12 o'clock). |
|
|
490
|
+
| `field` | `ChartField` | `"value"` | Property key or accessor extracting the numeric slice value. |
|
|
491
|
+
| `fillMode` | `ChartPolarFillMode` | `"solid"` | Fill style applied to slices ("solid" or radial "gradient" from center to arc). |
|
|
492
|
+
| `fillOpacity` | `number \| undefined` | `undefined` | Opacity of slice fills (between 0 and 1). |
|
|
493
|
+
| `hiddenIndices` (two-way) | `readonly number[]` | `[]` | The hidden data indices. Bind `[(hiddenIndices)]` for two-way binding; legend toggles write to it. |
|
|
494
|
+
| `keyField` | `ChartField \| undefined` | `undefined` | Property key or accessor extracting a stable datum identity across updates. |
|
|
495
|
+
| `labelContent` | `ChartPolarLabelContent` | `"percentage"` | Content to display in default slice data labels. |
|
|
496
|
+
| `labelPosition` | `ChartPolarLabelPosition` | `"outside"` | Placement position for slice data labels ("outside" with leader lines or "inside" slice bounds). |
|
|
497
|
+
| `minLabelAngle` | `number` | `12` | Minimum slice arc angle in degrees required to render an inside data label. |
|
|
498
|
+
| `name` | `string` | `"Pie"` | Series name used in tooltips and accessibility announcements. |
|
|
499
|
+
| `outerRadiusRatio` | `number` | `0.9` | Outer radius ratio relative to available plot bounds (0.1 to 1). |
|
|
500
|
+
| `padAngle` | `number` | `0` | Angular padding in degrees between adjacent slices. |
|
|
501
|
+
| `showLabels` | `boolean` | `false` | Whether to display slice labels, inside or outside as `labelPosition` sets. |
|
|
502
|
+
| `startAngle` | `number` | `0` | Starting angle in degrees (0 is 12 o'clock, clockwise). |
|
|
503
|
+
| `strokeColor` | `string` | `""` | Color of slice separator strokes. |
|
|
504
|
+
| `strokeWidth` | `number \| undefined` | `undefined` | Stroke width in pixels for slice separators. |
|
|
505
|
+
| `valueFormatter` | `ChartValueFormatter \| undefined` | `undefined` | Formatter callback for slice numeric values. |
|
|
506
|
+
| `visible` (two-way) | `boolean` | `true` | Two-way bindable visibility of the series. |
|
|
507
|
+
|
|
508
|
+
#### Outputs
|
|
509
|
+
|
|
510
|
+
| Name | Type | Description |
|
|
511
|
+
| --- | --- | --- |
|
|
512
|
+
| `hiddenIndicesChange` | `readonly number[]` | Emitted when `hiddenIndices` changes. |
|
|
513
|
+
| `sliceVisibilityChange` | `ChartSliceVisibilityEvent` | Emits when an individual slice's visibility is toggled via the legend. |
|
|
514
|
+
| `visibleChange` | `boolean` | Emitted when `visible` changes. |
|
|
515
|
+
|
|
516
|
+
#### Methods
|
|
517
|
+
|
|
518
|
+
| Signature | Description |
|
|
519
|
+
| --- | --- |
|
|
520
|
+
| `toggleSlice(dataIndex: number): boolean` | Shows or hides the slice at `dataIndex`, and returns whether it's now visible. |
|
|
521
|
+
|
|
522
|
+
### `ChartCenterTemplateDirective`
|
|
523
|
+
|
|
524
|
+
**Selector:** `ng-template[monaChartCenterTemplate]`
|
|
525
|
+
|
|
526
|
+
#### Template context
|
|
527
|
+
|
|
528
|
+
| Name | Type |
|
|
529
|
+
| --- | --- |
|
|
530
|
+
| `$implicit` | `number` |
|
|
531
|
+
| `formattedTotal` | `string` |
|
|
532
|
+
| `seriesId` | `string` |
|
|
533
|
+
| `seriesName` | `string` |
|
|
534
|
+
| `total` | `number` |
|
|
535
|
+
| `visibleSliceCount` | `number` |
|
|
536
|
+
|
|
537
|
+
### `ChartSliceLabelTemplateDirective`
|
|
538
|
+
|
|
539
|
+
**Selector:** `ng-template[monaChartSliceLabelTemplate]`
|
|
540
|
+
|
|
541
|
+
#### Template context
|
|
542
|
+
|
|
543
|
+
| Name | Type |
|
|
544
|
+
| --- | --- |
|
|
545
|
+
| `$implicit` | `ChartSliceContext` |
|
|
546
|
+
| `category` | `unknown` |
|
|
547
|
+
| `color` | `string` |
|
|
548
|
+
| `dataIndex` | `number` |
|
|
549
|
+
| `datum` | `unknown` |
|
|
550
|
+
| `formattedCategory` | `string` |
|
|
551
|
+
| `formattedPercentage` | `string` |
|
|
552
|
+
| `formattedValue` | `string` |
|
|
553
|
+
| `percentage` | `number` |
|
|
554
|
+
| `seriesId` | `string` |
|
|
555
|
+
| `seriesName` | `string` |
|
|
556
|
+
| `seriesType` | `"donut" \| "pie"` |
|
|
557
|
+
| `slice` | `ChartSliceContext` |
|
|
558
|
+
| `value` | `number` |
|
|
559
|
+
|
|
560
|
+
### Types
|
|
561
|
+
|
|
562
|
+
#### `ChartCenterTemplateContext`
|
|
563
|
+
|
|
564
|
+
| Name | Type |
|
|
565
|
+
| --- | --- |
|
|
566
|
+
| `$implicit` | `number` |
|
|
567
|
+
| `formattedTotal` | `string` |
|
|
568
|
+
| `seriesId` | `string` |
|
|
569
|
+
| `seriesName` | `string` |
|
|
570
|
+
| `total` | `number` |
|
|
571
|
+
| `visibleSliceCount` | `number` |
|
|
572
|
+
|
|
573
|
+
#### `ChartSliceContext`
|
|
574
|
+
|
|
575
|
+
| Name | Type |
|
|
576
|
+
| --- | --- |
|
|
577
|
+
| `category` | `unknown` |
|
|
578
|
+
| `color` | `string` |
|
|
579
|
+
| `dataIndex` | `number` |
|
|
580
|
+
| `datum` | `unknown` |
|
|
581
|
+
| `formattedCategory` | `string` |
|
|
582
|
+
| `formattedPercentage` | `string` |
|
|
583
|
+
| `formattedValue` | `string` |
|
|
584
|
+
| `percentage` | `number` |
|
|
585
|
+
| `seriesId` | `string` |
|
|
586
|
+
| `seriesName` | `string` |
|
|
587
|
+
| `seriesType` | `"donut" \| "pie"` |
|
|
588
|
+
| `value` | `number` |
|
|
589
|
+
|
|
590
|
+
#### `ChartSliceLabelTemplateContext`
|
|
591
|
+
|
|
592
|
+
| Name | Type |
|
|
593
|
+
| --- | --- |
|
|
594
|
+
| `$implicit` | `ChartSliceContext` |
|
|
595
|
+
| `category` | `unknown` |
|
|
596
|
+
| `color` | `string` |
|
|
597
|
+
| `dataIndex` | `number` |
|
|
598
|
+
| `datum` | `unknown` |
|
|
599
|
+
| `formattedCategory` | `string` |
|
|
600
|
+
| `formattedPercentage` | `string` |
|
|
601
|
+
| `formattedValue` | `string` |
|
|
602
|
+
| `percentage` | `number` |
|
|
603
|
+
| `seriesId` | `string` |
|
|
604
|
+
| `seriesName` | `string` |
|
|
605
|
+
| `seriesType` | `"donut" \| "pie"` |
|
|
606
|
+
| `slice` | `ChartSliceContext` |
|
|
607
|
+
| `value` | `number` |
|
|
608
|
+
|
|
609
|
+
#### `ChartSliceVisibilityEvent`
|
|
610
|
+
|
|
611
|
+
| Name | Type |
|
|
612
|
+
| --- | --- |
|
|
613
|
+
| `category` | `unknown` |
|
|
614
|
+
| `dataIndex` | `number` |
|
|
615
|
+
| `datum` | `unknown` |
|
|
616
|
+
| `seriesId` | `string` |
|
|
617
|
+
| `seriesName` | `string` |
|
|
618
|
+
| `seriesType` | `"donut" \| "pie"` |
|
|
619
|
+
| `visible` | `boolean` |
|
|
620
|
+
|
|
621
|
+
#### `ChartPolarFillMode`
|
|
622
|
+
|
|
623
|
+
```typescript
|
|
624
|
+
type ChartPolarFillMode = "gradient" | "solid";
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
#### `ChartPolarLabelContent`
|
|
628
|
+
|
|
629
|
+
```typescript
|
|
630
|
+
type ChartPolarLabelContent = "category" | "category-percentage" | "percentage" | "value";
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
#### `ChartPolarLabelPosition`
|
|
634
|
+
|
|
635
|
+
```typescript
|
|
636
|
+
type ChartPolarLabelPosition = "inside" | "outside";
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
#### `ChartPolarLabelSide`
|
|
640
|
+
|
|
641
|
+
```typescript
|
|
642
|
+
type ChartPolarLabelSide = "left" | "right";
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
**Documented elsewhere:** [`ChartField`](chart.md), [`ChartValueFormatter`](chart.md)
|