Pie chart
Parts of a whole as a pie, or a donut with innerRadius.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- Built on native elements
- The chart is role=img with the required title as its name.
- A visually hidden table lists every slice with its value and share.
- One focusable stop per slice with a spoken label; Arrow keys move between them and Escape hides the tooltip.
- Styles in one file:
chart.css
Import
import '@monoline/css';
import {PieChart} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add pie-chart
Usage
import {PieChart} from '@monoline/react';
const sources = [{label: 'Search', value: 46}, {label: 'Direct', value: 28}, {label: 'Email', value: 14}, {label: 'Referral', value: 8}, {label: 'Social', value: 4}];
<PieChart title="Traffic by source" data={sources} />
Examples
Empty
Props
PieChartProps
| Prop | Type | Default | Description |
|---|---|---|---|
title required | string | – | Accessible name and table caption. Required. |
data required | ChartDatum[] | – | Slices: `[{label: 'Search', value: 40}]`. Negative values count as 0. |
innerRadius | number | 0 | Hole size as a fraction of the radius, from 0 (pie) to just under 1. 0.6 makes a donut. Default 0. |
height | number | 240 | Chart height in px. Default 240. |
valueFormat | ((value: number) => string) | – | Format a value for the legend, tooltip and table. |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
| Attribute | Stylesheet |
|---|---|
[data-active] | chart.css |
[data-align="end"] | chart.css |
[data-align="start"] | chart.css |
CSS classes
| Class | Stylesheet |
|---|---|
.chart | chart.css |
.chart-area | chart.css |
.chart-empty | chart.css |
.chart-hit | chart.css |
.chart-hits | chart.css |
.chart-legend | chart.css |
.chart-legend-detail | chart.css |
.chart-line | chart.css |
.chart-marks | chart.css |
.chart-outline | chart.css |
.chart-pat | chart.css |
.chart-pat-dot | chart.css |
.chart-plot | chart.css |
.chart-rule | chart.css |
.chart-s0 | chart.css |
.chart-s1 | chart.css |
.chart-s2 | chart.css |
.chart-s3 | chart.css |
.chart-s4 | chart.css |
.chart-svg | chart.css |
.chart-swatch | chart.css |
.chart-tick | chart.css |
.chart-tip | chart.css |
.chart-tip-row | chart.css |
.chart-tip-title | chart.css |
.chart-tip-value | chart.css |
.chart-value | chart.css |
.chart-zero | chart.css |
.chart-slice | pie-chart.css |
Customization
Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.
app.css
.chart {
border-color: var(--accent);
}
.chart[data-active] {
background: var(--wash);
}
Accessibility
- The chart is role=img with the required title as its name.
- A visually hidden table lists every slice with its value and share.
- One focusable stop per slice with a spoken label; Arrow keys move between them and Escape hides the tooltip.
- The value tooltip opens on hover and on keyboard focus.
- Slices differ by fill pattern as well as colour, and the legend lists each with its value and share.
- No entrance animation under prefers-reduced-motion.
Keyboard
| Keys | Action |
|---|---|
| Arrow keys | Move between them and Escape hides the tooltip |