# Pie Chart

Show parts of a whole as slices or a donut.

Page: https://sui.draco.dev/docs/charts/pie-chart

### Example: pie-chart-demo

```tsx
"use client";
import { chartLabelsZhCN, PieChart } from "@workspace/ui/charts/pie-chart";
import type { ExampleProps } from "../types";

const browsers = [
  { browser: "chrome", visitors: 275 },
  { browser: "safari", visitors: 200 },
  { browser: "firefox", visitors: 187 },
  { browser: "edge", visitors: 173 },
  { browser: "other", visitors: 90 },
];

function browserConfig(chinese: boolean) {
  return {
    chrome: { label: "Chrome" },
    safari: { label: "Safari" },
    firefox: { label: "Firefox" },
    edge: { label: "Edge" },
    other: { label: chinese ? "其他" : "Other" },
  };
}

export default function Example({ locale }: ExampleProps) {
  const chinese = locale === "zh-CN";
  return (
    <PieChart
      data={browsers}
      config={browserConfig(chinese)}
      nameKey="browser"
      valueKey="visitors"
      showLegend
      labels={chinese ? chartLabelsZhCN : undefined}
    />
  );
}
```

## Installation

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

With pnpm, Yarn or Bun, use `pnpm dlx`, `yarn dlx` or `bunx --bun` in place of `npx`, and `pnpm add`, `yarn add` or `bun add` in place of `npm install`.

Install with the shadcn CLI or use the shared `@workspace/ui` package. Follow the [installation guide](/docs/installation) to configure the registry, load styles, and choose import aliases.

## Usage

```tsx
import { PieChart } from "@workspace/ui/charts/pie-chart";

<PieChart data={data} config={config} nameKey="browser" valueKey="visitors" donut />;
```

## Donut with total

With `donut`, `label` shows the total in the center, with the label text under it.

### Example: pie-chart-donut

```tsx
"use client";
import { chartLabelsZhCN, PieChart } from "@workspace/ui/charts/pie-chart";
import type { ExampleProps } from "../types";

const browsers = [
  { browser: "chrome", visitors: 275 },
  { browser: "safari", visitors: 200 },
  { browser: "firefox", visitors: 187 },
  { browser: "edge", visitors: 173 },
  { browser: "other", visitors: 90 },
];

function browserConfig(chinese: boolean) {
  return {
    chrome: { label: "Chrome" },
    safari: { label: "Safari" },
    firefox: { label: "Firefox" },
    edge: { label: "Edge" },
    other: { label: chinese ? "其他" : "Other" },
  };
}

export default function Example({ locale }: ExampleProps) {
  const chinese = locale === "zh-CN";
  return (
    <PieChart
      data={browsers}
      config={browserConfig(chinese)}
      nameKey="browser"
      valueKey="visitors"
      donut
      label={chinese ? "访客" : "Visitors"}
      labels={chinese ? chartLabelsZhCN : undefined}
    />
  );
}
```

## Customize

The wrapper covers common cases. For more control, pass Recharts elements as `children`, or compose your own chart with the [Chart](/docs/charts/chart) primitives.

## API reference

| Prop | Type | Default / behavior |
| --- | --- | --- |
| `nameKey` | `string` | Required. Names each slice; colors come from `config[name]`. |
| `valueKey` | `string` | Required. |
| `donut` | `boolean` | `false`. |
| `label` | `string \| boolean` | `false`. Donut only. |
| `data` | `readonly Record<string, unknown>[]` | Required rows. |
| `config` | `ChartConfig` | Required. Labels, colors and icons by key; series without a color use `--chart-1` to `--chart-5`. |
| `valueFormatter` | `(value: number) => string` | `toLocaleString`. Formats axes, tooltip and the screen-reader table; build one with `numberFormatter`. |
| `showLegend` | `boolean` | `false`. Lists each slice or ring. |
| `showTooltip` | `boolean` | `true`. |
| `loading` | `boolean` | `false`. Shows a skeleton. |
| `labels` | `Partial<ChartLabels>` | English labels; pass `chartLabelsZhCN` for Chinese. |
| `aria-label` | `string` | Names the figure and captions its data table. |
| `className` | `string` | Sizes the chart; defaults to a 16:9 box. |
| `children` | `ReactNode` | Extra Recharts elements, such as `ReferenceLine`. |

The module also exports `PieChartProps`, `numberFormatter`, and the label presets `chartLabels` and `chartLabelsZhCN`. Screen readers get a hidden data table while the drawing itself is hidden from assistive technology, and animation is off when reduced motion is requested.
