PeakUIPro

PieChart

A whole shared out as sectors, as a donut, or as nested rings

Usage

PieChart is imported from its own subpath and needs the optional recharts peer.

npm install recharts
import {PieChart} from "@peakui-pro/react/pie-chart";

Anatomy

import {PieChart} from "@peakui-pro/react/pie-chart";

// Only the root is this package's own; the parts are the charting library's components under
// PeakUI names. The root is not exported from `@peakui-pro/react`, so an app that renders no
// chart never needs the peer installed, not even to render on a server.
export default () => (
  <PieChart aria-label="Browser usage">
    <PieChart.Pie data={[]} dataKey="value" nameKey="name">
      <PieChart.Cell />
      <PieChart.Label />
    </PieChart.Pie>
    <PieChart.Tooltip content={<PieChart.TooltipContent />} />
  </PieChart>
);

Data belongs to the Pie

PieChart takes no data. Each Pie has its own, which is what lets one chart hold two rings (see Nested Donut). Name the field that holds the value with dataKey and the one that holds the slice's name with nameKey; the tooltip prints both.

Colouring sectors

Give each sector its colour with a Cell, or put a fill on every row of the data and map it to a Cell. Use the series ramp var(--chart-2) … var(--chart-5).

--chart-1 is the darkest step and nearly vanishes on a dark surface. A sector is a large shape the reader has to find, so the examples on this page start at --chart-5 and work down.

Separating sectors

Sectors are drawn with no outline. The gap between two is paddingAngle, in degrees, which stays the colour of whatever sits behind the chart. cornerRadius rounds each sector's ends.

A hole

Set innerRadius on the Pie to turn the pie into a donut. Whatever you want in the hole goes in one of two places: a PieChart.Label inside the Pie, drawn in the plot's own coordinates, or any React node laid over the chart (see Donut With Content).

Zero and empty data

A Pie whose values sum to zero, or whose data is empty, draws no sector and does not throw. Show your own empty state beside it.

Accessibility

The plot takes focus and answers the arrow keys, so aria-label names it, not the wrapper. Sectors are told apart by colour; add a second cue the reader can use without it: separate them with paddingAngle, and print each value and name beside the ring in a legend or a breakdown (see With Breakdown).

// Named by a string, or by the element that already carries the chart's title.
<PieChart aria-label="Browser usage">{/* … */}</PieChart>
<PieChart aria-labelledby="usage-title">{/* … */}</PieChart>

// A chart that only repeats a figure printed beside it leaves the keyboard layer out.
<PieChart accessibilityLayer={false}>{/* … */}</PieChart>

Examples

Donut

An innerRadius makes the ring and paddingAngle separates its sectors.

Donut With Label

A Label inside the Pie draws in the hole. Its content receives the ring's viewBox, whose cx and cy are the centre.

Donut With Content

Any React node can fill the hole: lay it over the chart in a relative wrapper, centre it, and set pointer-events-none so the pointer still reaches the sectors beneath.

With Breakdown

The ring beside the figures it draws.

Custom Tooltip

TooltipContent takes a valueFormatter; hideHeader leaves out the row above the slice, which a pie has no use for.

Nested Donut

Two Pies in one chart, each with its own radii.

Customization

Tailwind CSS

<PieChart className="mx-auto max-w-xs" height={220} />

Global CSS

@layer components {
  .chart-polar {
    --chart-axis-size: 11px;
    --chart-grid-opacity: 0.4;
  }
}

Styling Reference

PeakUI follows the BEM methodology, so variants and states are plain class names you can target.

CSS Classes

The PieChart component uses these CSS classes (view source styles):

Base Classes

  • .chart-polar - The block every polar chart carries; grid, tick text, the hidden tooltip cursor and the raised tooltip
  • .pie-chart - This chart's own block; gives the root its width, removes sector outlines and mutes label text and lines

Slots

  • [data-slot="pie-chart"] - The root element

CSS Variables

VariablePurpose
--chart-axis-sizeType size of tick text; set on .chart-polar
--chart-grid-opacityHow far a polar grid recedes behind the data; set on .chart-polar
--chart-1 … --chart-5The series ramp, derived from the theme's accent

API Reference

PieChart

PropTypeDefaultDescription
heightnumber | `${number}%` 300Height of the plot
widthnumber | `${number}%` '100%'Width of the plot
accessibilityLayerbooleantrueWhether the plot is focusable and answers the arrow keys
childrenReactNode-The chart's parts
aria-labelstring-Names the plot, which is what takes focus — not the wrapping <div>
aria-labelledbystring-Names the plot by another element's id
classNamestring-Additional classes
...restComponentProps<'div'>-Native <div> attributes, on the wrapper

There is no data and no margin: a pie's data belongs to its Pie, and a polar chart sizes itself from its container.

PieChart.Pie

The charting library's Pie, re-exported. See the Recharts Pie API.

PieChart.Cell

The charting library's Cell, re-exported. See the Recharts Cell API.

PieChart.Label

The charting library's Label, re-exported. See the Recharts Label API.

PieChart.Tooltip

The charting library's Tooltip, re-exported. See the Recharts Tooltip API.

PieChart.TooltipContent

ChartTooltip.Content, for the content prop of PieChart.Tooltip. Its props are on the ChartTooltip page.

On this page