Playground

Pie chart

Parts of a whole as a pie, or a donut with innerRadius.

  • 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

Pie

Donut

innerRadius is a fraction of the radius.

Empty

Props

PieChartProps

PropTypeDefaultDescription
title requiredstring–Accessible name and table caption. Required.
data requiredChartDatum[]–Slices: `[{label: 'Search', value: 40}]`. Negative values count as 0.
innerRadiusnumber0Hole size as a fraction of the radius, from 0 (pie) to just under 1. 0.6 makes a donut. Default 0.
heightnumber240Chart 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.

AttributeStylesheet
[data-active]chart.css
[data-align="end"]chart.css
[data-align="start"]chart.css

CSS classes

ClassStylesheet
.chartchart.css
.chart-areachart.css
.chart-emptychart.css
.chart-hitchart.css
.chart-hitschart.css
.chart-legendchart.css
.chart-legend-detailchart.css
.chart-linechart.css
.chart-markschart.css
.chart-outlinechart.css
.chart-patchart.css
.chart-pat-dotchart.css
.chart-plotchart.css
.chart-rulechart.css
.chart-s0chart.css
.chart-s1chart.css
.chart-s2chart.css
.chart-s3chart.css
.chart-s4chart.css
.chart-svgchart.css
.chart-swatchchart.css
.chart-tickchart.css
.chart-tipchart.css
.chart-tip-rowchart.css
.chart-tip-titlechart.css
.chart-tip-valuechart.css
.chart-valuechart.css
.chart-zerochart.css
.chart-slicepie-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

KeysAction
Arrow keysMove between them and Escape hides the tooltip