PieChart
Overview
The PieChart component draws parts of a whole as the slices of a pie or a donut, with each slice's share in the tooltip and the data table.
It wraps Recharts' PieChart and comes from
the @allxsmith/bestax-charts package, which the Charts guide
shows how to install. You give it data, a list of slices each with a name, a value and an
optional color, and it draws them with Bulma's colors, a legend whose entries hide and show
slices, a tooltip in a Bulma box, and a data table screen readers read.
Import
import { PieChart } from '@allxsmith/bestax-charts';
Usage
Basic Usage
Each slice of data has a name and a value, and the pie gives it its share of the whole.
label names the chart; it's required, because it's what a screen reader announces.
function example() { const visits = [ { name: 'Search', value: 4200, color: 'primary' }, { name: 'Direct', value: 2600, color: 'info' }, { name: 'Social', value: 1500, color: 'success' }, { name: 'Email', value: 900, color: 'warning' }, ]; return <PieChart label="Visits by source" data={visits} hasLegend />; }
Donut
isDonut draws the pie as a ring, thickness pixels wide, and centerLabel writes text in the
middle of it, such as a total. The center label is drawn only, so say what it says in
description too when it matters.
function example() { const visits = [ { name: 'Search', value: 4200, color: 'primary' }, { name: 'Direct', value: 2600, color: 'info' }, { name: 'Social', value: 1500, color: 'success' }, ]; return ( <Columns> <Column> <PieChart label="Visits by source" data={visits} isDonut centerLabel="8,300" description="8,300 visits in all, half of them from search." height={240} /> </Column> <Column> <PieChart label="Visits by source, thin ring" data={visits} isDonut thickness={16} paddingAngle={2} height={240} /> </Column> </Columns> ); }
Angles and Half Pies
The slices run clockwise from the top, as RingProgress draws its ring. startAngle and
endAngle set where they start and end, in degrees counter-clockwise from three o'clock, so
startAngle={180} and endAngle={0} draw a half pie. paddingAngle leaves a gap between
slices.
function example() { const storage = [ { name: 'Photos', value: 48, color: 'link' }, { name: 'Apps', value: 22, color: 'info' }, { name: 'Mail', value: 9, color: 'warning' }, ]; return ( <PieChart label="Storage used" data={storage} unit=" GB" isDonut startAngle={180} endAngle={0} paddingAngle={2} centerLabel="79 GB" hasLegend /> ); }
Labels
showLabels writes each slice's value beside it, with a line to it. labelsType="percent"
writes its share instead, and labelsPosition="inside" writes the labels on the slices. A label
on a slice takes its Bulma color's -invert color, which stays readable on it.
function example() { const visits = [ { name: 'Search', value: 4200, color: 'primary' }, { name: 'Direct', value: 2600, color: 'info' }, { name: 'Social', value: 1500, color: 'success' }, { name: 'Email', value: 900, color: 'warning' }, ]; return ( <Columns> <Column> <PieChart label="Visits by source" data={visits} showLabels height={240} /> </Column> <Column> <PieChart label="Share of visits by source" data={visits} showLabels labelsType="percent" labelsPosition="inside" height={240} /> </Column> </Columns> ); }
Legend and Hidden Slices
hasLegend draws a legend below the chart, or above it with legendPosition="top". Each entry
is a toggle button that hides or shows its slice, and the slices shown share the whole pie. A
hidden slice stays in the data table. The chart keeps that state itself, starting from
defaultHiddenSeries, or you hold it with hiddenSeries and onHiddenSeriesChange.
function example() { const visits = [ { name: 'Search', value: 4200, color: 'primary' }, { name: 'Direct', value: 2600, color: 'info' }, { name: 'Social', value: 1500, color: 'success' }, { name: 'Email', value: 900, color: 'warning' }, ]; const [hidden, setHidden] = useState(['Search']); return ( <> <Buttons mb="3"> <Button size="small" onClick={() => setHidden([])}> Show all </Button> </Buttons> <PieChart label="Visits by source" data={visits} hasLegend legendPosition="top" showLabels labelsType="percent" hiddenSeries={hidden} onHiddenSeriesChange={setHidden} /> </> ); }
With slices hidden, the labels and the tooltip give each slice's share of the slices shown, as the pie draws it. The data table always gives its share of every slice.
Formatting Values
Values are written as useNumberFormat writes a number, in the
LocaleProvider's locale, and shares as it writes a percentage. valueFormatter writes the
values your way instead, in the tooltip, on the labels and in the data table, and unit is
written after each one.
function example() { const spending = [ { name: 'Payroll', value: 48250 }, { name: 'Rent', value: 12000 }, { name: 'Software', value: 6400 }, { name: 'Travel', value: 3100 }, ]; const dollars = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0, }); return ( <PieChart label="Spending by category" data={spending} valueFormatter={value => dollars.format(value)} showLabels /> ); }
Colors
A slice's color takes a Bulma color name (primary, info, grey-dark and the rest), a shade
of one of Bulma's six palette colors (primary-30, info-dark), or any CSS color. Bulma names
draw with Bulma's own CSS variables, so they follow Theme and dark mode.
A slice with no color takes the next of Bulma's palette colors.
function example() { const visits = [ { name: 'Search', value: 4200, color: 'primary-30' }, { name: 'Direct', value: 2600, color: 'primary-45' }, { name: 'Social', value: 1500, color: 'primary-60' }, { name: 'Email', value: 900, color: 'primary-75' }, ]; return ( <PieChart label="Visits by source" data={visits} isDonut showLabels labelsPosition="inside" hasLegend /> ); }
Showing the Data Table
The data table is always there for screen readers: a row per slice, with its value and its share
of the whole. showTable puts it on screen too, below the chart, and showLabel shows the
chart's label above it as a caption. labels renames the table's columns, and the share in the
tooltip, for a page in another language.
function example() { const visits = [ { name: 'Recherche', value: 4200 }, { name: 'Direct', value: 2600 }, { name: 'Réseaux', value: 1500 }, ]; return ( <PieChart label="Visites par source" showLabel showTable data={visits} labels={{ name: 'Source', value: 'Visites', share: 'Part' }} height={220} /> ); }
Recharts Props and Children
Each slice is edged with a border in the page's background color, strokeWidth pixels wide, or
none with 0. Each Recharts part the chart draws takes its own props after the chart's:
pieProps for Recharts' Pie (here, rounded corners), chartProps for its PieChart, and
tooltipProps. children adds more Recharts elements, such as <defs> for a pattern a slice
names with url(#id).
function example() { const visits = [ { name: 'Search', value: 4200, color: 'url(#search-stripes)' }, { name: 'Direct', value: 2600, color: 'info' }, { name: 'Social', value: 1500, color: 'success' }, ]; return ( <PieChart label="Visits by source" data={visits} isDonut paddingAngle={3} strokeWidth={0} pieProps={{ cornerRadius: 6 }} hasLegend > <defs> <pattern id="search-stripes" width="8" height="8" patternUnits="userSpaceOnUse" patternTransform="rotate(45)" > <rect width="8" height="8" fill="var(--bulma-primary)" /> <line x1="0" y1="0" x2="0" y2="8" stroke="var(--bulma-scheme-main)" strokeWidth="3" /> </pattern> </defs> </PieChart> ); }
Size
The chart fills its container's width and is 300 pixels tall, and the pie fills the room that
leaves. width and height take pixels or a percentage of the container. A chart sized in
pixels draws on the server as well; one sized in percent draws once the browser has measured its
container, and its legend and data table render on the server either way.
<PieChart label="Two sources" data={[ { name: 'Search', value: 4200 }, { name: 'Direct', value: 2600 }, ]} width={240} height={180} />
Theming
The chart draws its own lines and text from CSS variables that fall back to Bulma's tokens, so
it follows Theme and dark mode with nothing set. A pie uses these, and the
BarChart Theming section lists the rest:
:root {
--bulma-chart-stroke-color: var(--bulma-scheme-main); /* slice borders */
--bulma-chart-text-color: var(--bulma-text-weak); /* labels beside slices */
--bulma-chart-label-color: var(--bulma-text-strong); /* a donut's center */
}
Accessibility
- The chart is a
<figure>named bylabel. The drawing is named by the same caption, and the data table's caption is the same name.descriptionis read with the name of both. - A data table of every slice renders beside the drawing, visually hidden with Bulma's
is-sr-only, so a screen reader reads the numbers the pie shows (WAI: complex images). Each row has the slice's name as its row header, then its value and its share of the whole. A hidden slice stays in it.showTableputs it on screen. - The drawing is one tab stop: the chart turns off the extra stop Recharts gives a
Pie. Once it has focus, the tooltip shows the first slice, the right and left arrow keys move through the slices in the order they're drawn (clockwise from the top by default), and Enter hides and shows the tooltip (Recharts' keyboard layer). - Each legend entry is a
buttonwitharia-pressed. A hidden slice's swatch is drawn hollow and its label greyed, so its state isn't told by color alone. - The swatches in the legend and tooltip are
aria-hidden; each entry's text says which slice it is. - Slices are told apart by color alone in the drawing. When that matters, write the values on
them with
showLabels, or tell slices apart with a pattern fill (see Recharts Props and Children). A label on a slice takes its Bulma color's-invertcolor; on a CSS color it takes the chart's text color, so check that it reads. centerLabelis drawn only, and a screen reader doesn't read it. Put what it says indescriptionwhen it matters.
Related Components
FunnelChart: The stages of a process, each as wide as its value.RadialBarChart: Values as bars bent into rings.BarChart: Values as bars, one group per category.RingProgress: One value, or a few, drawn as a ring.Table: The Bulma table the chart's data table is drawn with.LocaleProvider: The locale the chart writes its numbers in.- Helper Props: Bulma helper props for spacing, color, etc.
Additional Resources
Props
| Prop | Type | Default | Description |
|---|---|---|---|
hiddenSeries | string[] | — | The names of the slices that are hidden, when you hold that state. A hidden slice leaves the drawing and the tooltip, and the slices shown share the whole pie, but it stays in the data table. |
isDonut | boolean | false | Draws the pie as a ring, with a hole in the middle. |
thickness | number | 40 | The ring's width in pixels, measured in from the pie's edge. Ignored unless isDonut. pieProps radii replace the ones the chart works out from it. |
centerLabel | string | — | Text written in the middle of a donut, such as a total. Ignored unless isDonut. It's drawn only, so a screen reader doesn't read it: say it in description too when it matters. |
paddingAngle | number | 0 | The gap between slices, in degrees. |
startAngle | number | 90 | Where the first slice starts, in degrees counter-clockwise from three o'clock. The default and endAngle's run the slices clockwise from the top, as RingProgress does; startAngle={180} and endAngle={0} draw a half pie. |
endAngle | number | -270 | Where the last slice ends, in degrees counter-clockwise from three o'clock. |
showLabels | boolean | false | Writes each slice's value, or its share with labelsType="percent", on or beside it. A label on a slice takes the -invert color of a Bulma color, which stays readable on it; on a CSS color it takes the chart's text color, so check that it reads. |
labelsType | 'value' | 'percent' | 'value' | What the labels write: each slice's value, or its share of the slices shown. |
labelsPosition | 'inside' | 'outside' | 'outside' | Where the labels sit: on each slice, or beside it with a line to it. |
strokeWidth | number | 1 | The width of the border drawn around each slice in the page's background color, which keeps neighbouring slices apart. 0 draws none. |
pieProps | Partial<Omit<PieProps, 'ref'>> | — | Props passed to Recharts' Pie, after the chart's own, such as cornerRadius. |
chartProps | Partial<React.ComponentProps<typeof RechartsPieChart>> | — | Props passed to Recharts' PieChart, after the chart's own, such as margin. |
data | readonly ChartSlice[] | — | Required. The slices to draw, in order, each a name, a value and an optional color. |
hasLegend | boolean | false | Shows a legend of the slices. Each entry is a toggle button that hides or shows its slice. |
defaultHiddenSeries | string[] | — | The names of the slices hidden to begin with, when the chart holds it. |
onHiddenSeriesChange | (hiddenSeries: string[]) => void | — | Called with the names of the hidden slices when the legend changes them. |
labels | { name?: string; value?: string; share?: string } | — | The data table's column headers and the tooltip's name for a share, for a page in another language. The table lists each slice's name, its value and its share: of the whole on a PieChart, of the first stage on a FunnelChart, and of max on a RadialBarChart. |
tooltipProps | Partial<TooltipProps> | — | Props passed to Recharts' Tooltip, after the chart's own. |
className | string | — | Additional CSS classes to apply. |
label | string | — | Required. What the chart shows, such as "Monthly sales". It names the chart for assistive tech, and captions its data table. Shown above the chart with showLabel. |
description | string | — | A sentence or two on what the chart says, such as the trend it shows. A screen reader reads it with the chart's name. Not shown on screen. |
showLabel | boolean | false | Shows label above the chart as its caption. |
showTable | boolean | false | Shows the chart's data table below it. The table is always there for screen readers, which read the numbers from it; this puts it on screen as well. |
hasTooltip | boolean | true | Shows a tooltip with the values under the pointer, or under the keyboard focus as the arrow keys move through the chart. |
valueFormatter | ChartValueFormatter | — | Writes each value on the axes, in the tooltip, on the labels and in the data table. Without one, values are written as useNumberFormat writes a number, in the LocaleProvider's locale. |
unit | string | — | Written after every value, such as % or kg. |
width | ChartSize | '100%' | The chart's width, in pixels or as a percentage of its container. A percentage makes the chart follow its container's size as the browser measures it. Recharts draws in the browser either way: a server render holds the figure, its caption, legend and data table, and the drawing appears once the page hydrates. |
height | ChartSize | 300 | The chart's height, in pixels or as a percentage of its container, which then needs a height of its own. |
children | React.ReactNode | — | More Recharts elements to draw inside the chart, such as a Brush, a ReferenceArea, or <defs> for a pattern a color names with url(#id). |
legendPosition | 'top' | 'bottom' | 'bottom' | Where the legend sits. |
ref | React.Ref<HTMLElement> | — | Ref forwarded to the root <figure>. |
... | All standard HTML attributes and Bulma helper props | — | See Helper Props |
Types:
ChartColor: Bulma color |`${(typeof paletteColors)[number]}-${(typeof validColorShades)[number]}`|(string & {})— A color for a series, a slice or a mark. A Bulma color name such asprimaryorgrey-light, a shade of one of Bulma's six palette colors such asprimary-60orinfo-dark, or any CSS color, such as#3e8ed0,var(--brand)orurl(#stripes)for a pattern the chart's children define. A Bulma name draws with Bulma's own CSS variable, so it followsThemeand dark mode.ChartSize:number|`${number}%`— A chart's width or height: pixels, or a percentage of its container.ChartSlice: One slice of a chart that draws parts of a whole: a name and a value. Its fields:name:string— Required. What the legend, the tooltip and the data table call it, and the namehiddenSeriesholds. Give each slice a name no other has: the chart tells its slices apart by name, so two of one name share a legend entry and a tooltip.value:number— Required. Its value.color:ChartColor— Its color. Without one, it takes the next of Bulma's palette colors.
ChartValueFormatter:(value: number) => string— Writes a value for the axes, the tooltip, the labels and the data table.