# Pie Chart

- **Section:** Components
- **URL:** https://refast.fastapicloud.dev/docs/components/pie-chart
- **Markdown:** https://refast.fastapicloud.dev/docs/components/pie-chart.md
- **Source:** https://github.com/idling-mind/refast-docs/blob/main/docs/pages/components/charts/pie_chart.py
- **Live examples:** the widgets on this page need a browser; open the URL above to try them.
- **Applies to:** refast 0.12.2

Pie charts show part-to-whole relationships. Setting `inner_radius > 0` creates a
**donut chart**, which is often easier to read when comparing slices.

```python
from refast.components.shadcn.charts import (
    ChartContainer, PieChart, Pie,
    ChartTooltip, ChartTooltipContent,
    ChartLegend, ChartLegendContent,
)

data = [
    {"name": "Chrome",  "visitors": 275},
    {"name": "Firefox", "visitors": 200},
    {"name": "Safari",  "visitors": 187},
]

# Slice colors are auto-assigned by index (hsl(var(--chart-1)), --chart-2, ...)
ChartContainer(class_name="h-72", children=[
    PieChart(children=[
        Pie(
            data=data,
            data_key="visitors",
            name_key="name",
            inner_radius=60,   # 0 = pie, >0 = donut
            outer_radius="80%",
            padding_angle=2,
        ),
        ChartTooltip(content=ChartTooltipContent()),
        ChartLegend(content=ChartLegendContent()),
    ]),
])
```

> **Tip:** For custom per-slice colors, add a `"fill"` key to each data item
> (e.g. `{"name": "Chrome", "visitors": 275, "fill": "hsl(var(--chart-1))"}`).
> You can also use `Cell` children for fine-grained control.

## PieChart Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `children` | `list` | `[]` | `Pie` components plus optional tooltip and legend |
| `margin` | `dict` | `{top:0, right:0, left:0, bottom:0}` | Chart margins |
| `on_click` | `Callback \| None` | `None` | Click on the chart area |

## Pie Props

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `data` | `list[dict]` | *(required)* | Array of data objects |
| `data_key` | `str` | *(required)* | Key holding the numeric value for each slice |
| `name_key` | `str` | *(required)* | Key holding the label for each slice |
| `inner_radius` | `int \| str` | `0` | Inner radius; set > 0 for a donut chart |
| `outer_radius` | `int \| str` | `"80%"` | Outer radius |
| `cx` | `str \| int` | `"50%"` | X position of the centre |
| `cy` | `str \| int` | `"50%"` | Y position of the centre |
| `padding_angle` | `int` | `0` | Gap between slices (degrees) |
| `corner_radius` | `int \| str \| None` | `None` | Rounded slice corners |
| `start_angle` | `int` | `0` | Start angle (degrees) |
| `end_angle` | `int` | `360` | End angle (degrees) |
| `label` | `bool \| None` | `None` | Show percentage/value labels on slices |
| `label_line` | `bool \| dict \| None` | `None` | Leader lines from slices to labels |
| `min_angle` | `int` | `0` | Minimum sector angle so tiny slices are still visible |
| `children` | `list` | `[]` | `Cell` components for per-slice styling |

## Cell Props

Use `Cell` inside a `Pie` to set individual slice colours:

```python
Cell(extra_props={"fill": "hsl(var(--chart-1))"})
```

| Prop | Type | Description |
|------|------|-------------|
| `extra_props` | `dict` | Pass `fill` (and optionally `stroke`) for per-slice styling |

---

Part of the Refast documentation: [all pages](https://refast.fastapicloud.dev/llms.txt), [everything on one page](https://refast.fastapicloud.dev/llms-full.txt), [search](https://refast.fastapicloud.dev/api/search?q=save_prop).
