# Donut chart

A sweeping donut that shows the hovered slice in the middle, paired with a legend.

Section: Charts / Radial
Page: https://ui.heyramzi.com/components/donut-chart
Import path: `@heyramzi/ui/charts`

## Import

```ts
import { DonutChart, Legend } from "@heyramzi/ui/charts";
```

## Usage

```svelte
<DonutChart
  data={[{ source: "Direct", visits: 4200, color: "var(--chart-1)" }]}
  valueKey="visits"
  labelKey="source"
  colorKey="color"
  centerLabel="Visits"
/>
<Legend items={[{ label: "Direct", value: 4200, color: "var(--chart-1)" }]} showValue />
```

## Demo

```svelte
<script lang="ts">
	import { DonutChart, Legend } from "@heyramzi/ui/charts";

	const data = [
		{ source: "Direct", visits: 4200, color: "var(--chart-1)" },
		{ source: "Search", visits: 3100, color: "var(--chart-2)" },
		{ source: "Social", visits: 1800, color: "var(--chart-3)" },
		{ source: "Email", visits: 900, color: "var(--chart-4)" },
	];
</script>

<div class="flex flex-wrap items-center justify-center gap-8">
	<DonutChart
		{data}
		valueKey="visits"
		labelKey="source"
		colorKey="color"
		centerLabel="Visits"
		height={220}
	/>
	<Legend
		items={data.map((d) => ({ label: d.source, value: d.visits, color: d.color }))}
		showValue
	/>
</div>
```

## API

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `data` | `TData[]` |  | required |
| `valueKey` | `keyof TData & string` |  | required |
| `labelKey` | `keyof TData & string` |  | required |
| `colorKey` | `keyof TData & string` |  |  |
| `height` | `number` | `240` |  |
| `innerRadius` | `number` | `60` |  |
| `outerRadius` | `number` | `100` |  |
| `centerLabel` | `string` |  | Center label shown when nothing is hovered |
| `formatValue` | `(v: number) => string` | `(v) => v.toLocaleString()` | Format the center value |
| `class` | `string` |  |  |
