Pie Chart¶
A pie or donut chart drawn as inline SVG on the shared chart core, the same core as the Bar Chart. Each slice is its share of the total, and a legend lists every slice with its value and percent, so the colors always have a key.
Decision¶
| When | Use |
|---|---|
| How a whole splits into a few parts, such as storage by library or streams by client | Pie Chart |
| The same split with the total in the middle | Pie Chart with donut: true and centerText |
| Comparing amounts that are not parts of one whole | Bar Chart |
| Used out of a limit, such as disk space against its capacity | Usage Meter |
| One share, such as a task's progress | Progress Ring |
Usage¶
import { createPieChart } from './configurationpage?name=myplugin_stolon.js';
var chart = createPieChart({
label: 'Storage by library',
unit: 'GB',
donut: true,
centerText: '2.2 TB',
centerLabel: 'Total',
slices: [
{ label: 'Movies', value: 820 },
{ label: 'Shows', value: 1240 },
{ label: 'Music', value: 96, color: 'teal' }
],
onSelect: function (slice, index) { showLibrary(slice ? slice.label : null); }
});
view.querySelector('#chartHost').appendChild(chart.element);
chart.setSlices([{ label: 'Movies', value: 900 }, { label: 'Shows', value: 1300 }]);
Classes: stolon-chart and stolon-pie-chart, with the parts stolon-chart-plot, stolon-chart-mark, stolon-pie-chart-legend, stolon-pie-chart-key, stolon-pie-chart-center, stolon-chart-empty, and stolon-chart-tip. The handle is { element, getValue, setValue, setSlices, getSelected, select, redraw, destroy }. setValue and setSlices both take the slices and draw again.
States and Modifiers¶
- A slice's
coloris a status color name or a hex code. A slice without one takes blue, green, orange, purple, red, teal, pink, and yellow in turn, by its place in the list. - Slices start at twelve o'clock and run clockwise in the order given. One slice with a value draws a full circle.
- A zero, negative, or missing value draws no slice and is left out of the total. Its legend row stays, with its value and 0%, and is disabled, since there is nothing to select.
- Every slice keeps its own slice and legend row, however small. Nothing merges into an "Other" slice, so sort or group the data first when there are many slivers.
- A percent is a whole number from 10%, has one decimal under it, and reads "<0.1%" for a sliver, so the percents may not add to exactly 100%.
sizeissmall,medium(the default), orlarge, and sets the pie's diameter to five times that size's control height. It never grows past the chart's width.donut: trueaddsdonutand draws a ring.centerTextsits in its center in the large font, overcenterLabelin the small font. Each ends in an ellipsis when it is wider than the hole, with the full text as its tooltip.- The legend sits beside the pie, and moves under it with
compactwhen the chart is narrower than 480 pixels. Each row is a button with the slice's color dot, label, value, and percent, the values and percents each in one column.showLegend: falsehides it, which leaves the tooltips as the only key to the colors. - Every slice is a button named with its label, value, and percent, such as "Movies, 820 GB, 38%". Hovering it shows the same on the shared tooltip surface. The plot takes focus, the arrow keys move the tooltip from slice to slice, and Escape hides it.
- A click on a slice or its legend row selects it, as do Enter and Space on the plot for the slice the arrow keys reached. A second one clears it. The selected row takes the accent wash and bar, the other slices dim, and
onSelect(slice, index)reports it, with(null, -1)when it clears. unitfollows each value, such as "GB", andformat(value)replaces the text of every value.- While no slice has a positive value, the chart shows
emptyText(default "Nothing to show.") in the List's empty look, in place of the pie and the legend. - A visually hidden table holds every value and percent, captioned by
label, under the column headingvalueName(default "Value").
Gotchas¶
- Call
destroy()when the page hides. It stops the resize watches. - The total is the sum of the positive values. A part of something larger, such as used space out of a disk, belongs in a Usage Meter.
Reference¶
Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.
createPieChart¶
function createPieChart(options?: PieChartOptions): PieChartHandle
A pie or donut chart on the shared chart core, drawn as inline SVG at a size from the size scale. Each slice is its share of the total, in its color, and is a button whose name and tooltip give its label, value, and percent. A legend beside the pie, or under it when the chart is narrow, lists every slice with its value and percent. A click, Enter, or Space on a slice or a legend row selects it, and again clears it. The core keeps a visually hidden table of every number.
| Option | Type | What it does |
|---|---|---|
slices |
PieSlice[] |
[{ label, value, color }] where color is a status color name or a hex code (default the series colors in turn) [optional] |
label |
string |
the chart's accessible name and the hidden table's caption [optional] |
donut |
boolean |
true draws a ring with room in the center (default false) |
centerText |
string |
the large text in a donut's center, such as the total [optional] |
centerLabel |
string |
the small text under centerText, such as 'Total' [optional] |
unit |
string |
text after each value, such as 'GB' [optional] |
showLegend |
boolean |
false hides the legend (default true) |
emptyText |
string |
the sentence shown while no slice has a positive value (default 'Nothing to show.') |
valueName |
string |
the heading of the value column in the hidden table (default 'Value') |
size |
'small' | 'medium' | 'large' |
'small', 'medium', or 'large', the pie's diameter on the size scale (default 'medium') |
format |
(value: number): string |
function(value) returning the text of a value, which wins over unit [optional] |
onSelect |
(slice: PieSlice | null, index: number): void |
function(slice, index) called when a slice is selected, and with (null, -1) when it is cleared [optional] |
| Handle member | Type |
|---|---|
getValue |
(): PieSlice[] |
setValue |
(slices: PieSlice[] | null): void |
setSlices |
(slices: PieSlice[] | null): void |
getSelected |
(): number |
select |
(index: number | null): void |
redraw |
(): void |
destroy |
(): void |
| Type | What it is |
|---|---|
PieSlice |
One slice of a pie: its name, its amount, and an optional status color name or hex code. |
PieAngle |
The start and end angle of one slice, in radians clockwise from twelve o'clock, and its share of the whole from 0 to 1. |
| Class | What it looks like |
|---|---|
stolon-pie-chart |
A pie chart on the shared chart core: the pie, a diameter on the size scale, beside its legend |
stolon-pie-chart.stolon-compact |
A narrow pie chart, which stacks the legend under the pie |
stolon-pie-chart-slice |
One slice in its color, parted from the next by a line of the surface, dimmed while another is under the pointer or selected |
stolon-pie-chart-center |
The value and its label in a donut's center |
stolon-pie-chart-legend |
The legend of a pie: a row per slice with its color dot, label, value, and percent, the values and percents each in one column |
Example¶
Example Code¶
<!-- title: Pie Chart -->
<div id="mount" style="display: grid; gap: 24px;"></div>
<script type="module">
const base = await window.__baseReady;
const mount = document.getElementById('mount');
mount.appendChild(base.createPieChart({
label: 'Items by library',
slices: [
{ label: 'Movies', value: 1240 },
{ label: 'Shows', value: 860 },
{ label: 'Music', value: 410 },
{ label: 'Photos', value: 95 },
{ label: 'Books', value: 0 }
]
}).element);
const storage = [
{ label: 'Movies', value: 820 },
{ label: 'Shows', value: 1240 },
{ label: 'Music', value: 96 },
{ label: 'Photos', value: 44 }
];
const total = storage.reduce((sum, s) => sum + s.value, 0);
mount.appendChild(base.createPieChart({
label: 'Storage by library',
donut: true,
unit: 'GB',
centerText: (total / 1000).toFixed(1) + ' TB',
centerLabel: 'Total',
slices: storage
}).element);
mount.appendChild(base.createPieChart({
label: 'Streams by client',
donut: true,
size: 'small',
format: (v) => v + (v === 1 ? ' stream' : ' streams'),
slices: [
{ label: 'Android TV', value: 12, color: '#3ddc84' },
{ label: 'Web', value: 7, color: '#00a4dc' },
{ label: 'Roku', value: 4, color: '#6c3c97' },
{ label: 'Swiftfin', value: 2, color: '#f2994a' }
]
}).element);
</script>