# Pie Chart

> React SVG pie chart with outside labels on leader lines, a legend and slices that pull out on hover or focus to show value and share, plus a data table.

- Kind: Component (Charts)
- Docs: https://ui.minidev.pro/docs/pie-chart
- Source (registry JSON with the full file): https://ui.minidev.pro/r/pie-chart.json
- License: MIT. One file, yours to edit after install.

## When to use

Hover or focus a slice and it pulls out on a spring while the readout shows its value and share. Slices are one tab stop; arrow keys, Home and End move between them, and a visually hidden table carries the data for screen readers. Labels hide below 320px wide. Use it for 2 to 6 parts of a whole; for more categories or comparisons over time use BarChart, for a ring with a centre total use DonutChart.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/pie-chart
```

Writes `components/ui/pie-chart.tsx` and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/pie-chart.json`.

### npm package

```bash
npm i minidev-ui-kit motion
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { PieChart } from "@/components/ui/pie-chart"
```

From the npm package:

```tsx
import { PieChart } from "minidev-ui-kit/ui/pie-chart"
```

## Usage

```tsx
import { PieChart } from "@/components/ui/pie-chart"

export function Example() {
  return (
    <PieChart />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `data` | `PieDatum[]` | `SAMPLE` | no | Slices in clockwise order, starting at the top. |
| `title` | `string` | `"Revenue by plan"` | no | Heading shown above the chart and used as the accessible name. |
| `caption` | `string` | `"Monthly recurring revenue"` | no | Caption under the total when no slice is active. |
| `format` | `(value: number) => string` | `defaultFormat` | no | Formats values in the readout, legend and table. |
| `showLabels` | `boolean` | `true` | no | Labels outside the slices with leader lines. Hidden automatically when the chart is narrow. |
| `showLegend` | `boolean` | `true` | no | Legend with values under the chart. |
| `pullOut` | `number` | `6` | no | Distance a hovered or focused slice pulls out, in chart units. |
| `className` | `string` | - | no |  |

## Examples

### Custom data and format

Pass label and value pairs and a format function; the readout, legend and labels all use it.

```tsx
<PieChart
  title="Customers by region"
  caption="Active accounts"
  format={(n) => n.toLocaleString("en-US")}
  data={[
    { label: "North America", value: 1240 },
    { label: "Europe", value: 860 },
    { label: "Asia Pacific", value: 540 },
    { label: "Latin America", value: 210 },
  ]}
/>
```

### Compact without labels

Hide the leader line labels and the legend for a small tile; hover still shows each slice.

```tsx
<PieChart
  title="Payment methods"
  caption="This month"
  showLabels={false}
  showLegend={false}
  className="max-w-xs"
  data={[
    { label: "Card", value: 61200 },
    { label: "Bank transfer", value: 28400 },
    { label: "Wallet", value: 9100 },
  ]}
/>
```

### Semantic colours

Each datum can carry its own colour; use the status tokens when the slices mean something.

```tsx
<PieChart
  title="Invoice status"
  caption="This quarter"
  format={(n) => `${n} invoices`}
  pullOut={10}
  data={[
    { label: "Paid", value: 296, color: "var(--success)" },
    { label: "Pending", value: 84, color: "var(--warning)" },
    { label: "Overdue", value: 32, color: "var(--danger)" },
  ]}
/>
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `Tab` | Moves focus into the pie. All slices share one Tab stop. |
| `ArrowRight` or `ArrowDown` | Moves focus to the next slice, pulls it out and shows its value. |
| `ArrowLeft` or `ArrowUp` | Moves focus to the previous slice. |
| `Home` or `End` | Moves focus to the first or last slice. |

## Accessibility

- The chart is a `<figure>` labelled by its title. The SVG is `role="group"`, and each slice is a focusable `role="img"` named "Label: value, share%".
- Slices with a value of 0 draw nothing and are skipped by the arrow keys.
- A visually hidden `<table>` lists every segment's value and share with a total row; the readout, outside labels and legend are `aria-hidden`.
- Under reduced motion the focused slice moves out instantly instead of on a spring.

## FAQ

### How do I format PieChart values as currency or plain numbers?

Pass `format`. It is used in the readout, the legend, each slice's accessible name and the hidden table. The default is compact US dollars, such as "$48.2K", with a fixed `en-US` locale.

### Why did the outside labels disappear?

Leader-line labels hide when the chart's box is under 320px wide, measured with a `ResizeObserver`; the pie then grows to fill the space and the legend drops to one column. Set `showLabels={false}` to hide them at every width.

### What happens with more than five PieChart slices?

Colours come from `--chart-1` to `--chart-5` and repeat after five, so a sixth slice reuses the first colour unless you pass `color`. For more categories use a [`HorizontalBarChart`](https://ui.minidev.pro/docs/horizontal-bar-chart).

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `motion`

## Related

- [Bar Chart](https://ui.minidev.pro/docs/bar-chart/md): Lightweight React bar chart with rounded bars, dashed gridlines and labels. Hovering a bar dims the rest and shows its value; the peak is highlighted first.
- [Donut Chart](https://ui.minidev.pro/docs/donut-chart/md): Single value donut chart in SVG that fills an accent ring to a percentage of max and prints the percent in the middle. Labelled as an image for screen readers.
- [Waffle Chart](https://ui.minidev.pro/docs/waffle-chart/md): Waffle chart that fills a 10 column grid of squares to show a value out of a total, with a label and count above. Clear for adoption rates and percentages.
- [Area Chart](https://ui.minidev.pro/docs/area-chart/md): Dependency free SVG area chart for React with a smoothed line, dashed grid, gradient fill and an optional highlighted point. Strokes stay crisp at any size.
- [Chart Card](https://ui.minidev.pro/docs/chart-card/md): Card wrapper for charts with a title, optional description and a minimum height body. Pairs with the area, bar and donut charts in a React dashboard.
- [Chart Legend](https://ui.minidev.pro/docs/chart-legend/md): Wrapping chart legend with small color swatches and labels. Pass colors per series or fall back to accent, success, warning and danger theme tokens.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
