# Pie Chart (/ui/components/react/pie-chart)

A pie or donut chart shows how 1 whole splits into 2 to 5 parts. Like every chart here it is a recipe: [Recharts](https://recharts.github.io) draws it and the [chart layer](/ui/components/react/charts) themes it. Each example below is a complete file to paste and adapt.

## Installation [#installation]

Recharts is the only extra dependency, with `react-is` at the same version as `react`:

```bash
pnpm add recharts react-is   # or: npm install recharts react-is / yarn add recharts react-is / bun add recharts react-is
```

The examples import the chart layer from `@/components/chart`. If your project doesn't have it yet, copy it from the [Charts](/ui/components/react/charts#the-chart-layer) page first.

## Examples [#examples]

### Donut [#donut]

A donut with a 60% inner radius and a 2 degree gap between slices. Each row of the data carries its own `fill`, which is how a slice gets its color. The total sits in the hole as HTML: give `Chart` the class `relative` and center an element over it.

```tsx
'use client'

import { Pie, PieChart, Tooltip } from 'recharts'
import { Chart, ChartTooltip, chartColors, useChartMotion } from '@/components/chart'

const data = [
  { channel: 'Online store', revenue: 23400, fill: chartColors[0] },
  { channel: 'Retail', revenue: 10800, fill: chartColors[1] },
  { channel: 'Wholesale', revenue: 8700, fill: chartColors[2] },
  { channel: 'Marketplace', revenue: 5200, fill: chartColors[3] },
]

const total = data.reduce((sum, row) => sum + row.revenue, 0)

const compact = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact' })
const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })

export default function PieChartDonut() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart className="relative">
        <PieChart responsive aria-label="Revenue by channel in December">
          <Tooltip content={<ChartTooltip formatValue={(value) => money.format(value)} />} />
          <Pie
            data={data}
            dataKey="revenue"
            nameKey="channel"
            innerRadius="60%"
            outerRadius="100%"
            paddingAngle={2}
            stroke="none"
            {...motion.series}
          />
        </PieChart>
        <p className="pointer-events-none absolute inset-0 flex flex-col items-center justify-center">
          <span className="text-foreground-intense text-2xl font-semibold tabular-nums">{compact.format(total)}</span>
          <span className="text-foreground-muted text-xs">Revenue</span>
        </p>
      </Chart>
    </div>
  )
}
```

### Half donut [#half-donut]

`startAngle={180}` and `endAngle={0}` keep the upper half, and `cy="100%"` moves the center to the bottom edge. A frame with `aspect-2/1` then fits the half circle at any width. Here the data is a status, so meaning picks the colors: delivered takes the success family and returned the error family.

```tsx
'use client'

import { Pie, PieChart, Tooltip } from 'recharts'
import { Chart, ChartTooltip, chartColors, useChartMotion } from '@/components/chart'

const data = [
  { status: 'Delivered', orders: 1840, fill: chartColors[1] },
  { status: 'In transit', orders: 620, fill: chartColors[2] },
  { status: 'Returned', orders: 140, fill: chartColors[4] },
]

const total = data.reduce((sum, row) => sum + row.orders, 0)

const count = new Intl.NumberFormat('en-US')

export default function PieChartHalfDonut() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart className="relative mx-auto aspect-2/1 h-auto max-w-sm">
        <PieChart responsive aria-label="Orders by delivery status this month">
          <Tooltip content={<ChartTooltip formatValue={(value) => count.format(value)} />} />
          <Pie
            data={data}
            dataKey="orders"
            nameKey="status"
            startAngle={180}
            endAngle={0}
            cy="100%"
            innerRadius="120%"
            outerRadius="200%"
            paddingAngle={2}
            stroke="none"
            {...motion.series}
          />
        </PieChart>
        <p className="pointer-events-none absolute inset-x-0 bottom-0 flex flex-col items-center">
          <span className="text-foreground-intense text-2xl font-semibold tabular-nums">{count.format(total)}</span>
          <span className="text-foreground-muted text-xs">Orders</span>
        </p>
      </Chart>
    </div>
  )
}
```

### Legend [#legend]

A pie has no axis to read the names from, so it carries a `Legend`. The stroke in the surface color separates the slices of a full pie.

```tsx
'use client'

import { Legend, Pie, PieChart, Tooltip } from 'recharts'
import { Chart, ChartLegend, ChartTooltip, chartColors, useChartMotion } from '@/components/chart'

const data = [
  { device: 'Mobile', sessions: 36200, fill: chartColors[0] },
  { device: 'Desktop', sessions: 28300, fill: chartColors[1] },
  { device: 'Tablet', sessions: 7000, fill: chartColors[2] },
]

const count = new Intl.NumberFormat('en-US')

export default function PieChartLegend() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart className="h-72">
        <PieChart responsive aria-label="Sessions by device in December">
          <Tooltip content={<ChartTooltip formatValue={(value) => count.format(value)} />} />
          <Legend itemSorter={null} content={<ChartLegend />} />
          <Pie
            data={data}
            dataKey="sessions"
            nameKey="device"
            outerRadius="100%"
            stroke="var(--background)"
            {...motion.series}
          />
        </PieChart>
      </Chart>
    </div>
  )
}
```

## Accessibility [#accessibility]

- Pass `aria-label` to the chart with what it shows (`"Revenue by channel in December"`). It becomes the accessible name of the SVG.
- The chart takes focus, and the arrow keys move the tooltip from slice to slice.
- Keep to 5 slices at most, and name them in a `Legend` or in the tooltip, so color is never the only cue. With more parts, a [horizontal bar chart](/ui/components/react/bar-chart#horizontal) reads better.
- A total in the hole is real text, not part of the SVG, so it is read as text.
- Nothing animates under reduced motion.
