# PieChart

Donut or pie chart drawn with chart.js. Pass `data` as `{ labels, datasets: [{ data, color? }] }`: one label and one value per slice. By default it is a donut with the total in the middle (`centerLabel` adds a line below it, `centerSnippet` replaces it); `donut={false}` draws a full pie. Slice colors come from the theme (`--global-color-data-1…6`, in order) and follow light and dark mode and theme changes by themselves; `color` takes an array of CSS colors, also `var(--…)`. The legend sits on the right (below when the chart is narrow) with the share of every slice, and hides a slice on click; the total and the shares count only the visible slices. `format` writes values with `Intl.NumberFormat`. `onitemclick` receives the clicked slice. `options` takes any other chart.js option and wins over the defaults. Screen readers get `label` and a hidden table with values and shares. The chart fills the height of `--pie-chart-height` (14rem).

```ts
import { PieChart } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/charts/PieChart.svelte`.

## Examples

### Donut

The default: a donut with the total in the middle and `centerLabel` below it, the legend on the right with the share of every slice. A click on a legend entry hides the slice; total and shares count only the visible ones.

```svelte
<script lang="ts">
	import { PieChart } from '@likable-hair/svelte';

	const data = {
		labels: ['Color', 'Cut', 'Treatments', 'Retail'],
		datasets: [{ label: 'Services', data: [539, 334, 231, 180] }]
	};
</script>

<PieChart {data} label="Services by type" centerLabel="services" />
```

### Pie

`donut={false}` draws a full pie, without the total in the middle. `legend="bottom"` moves the legend below the chart.

```svelte
<script lang="ts">
	import { PieChart } from '@likable-hair/svelte';

	const data = {
		labels: ['Paid', 'Pending', 'Overdue'],
		datasets: [{ label: 'Invoices', data: [182, 41, 12], color: ['var(--global-color-success)', 'var(--global-color-warning)', 'var(--global-color-error)'] }]
	};
</script>

<PieChart {data} donut={false} legend="bottom" label="Invoices by status" />
```

### Format and center

`format` writes the values as euros in the tooltip and in the total. `centerSnippet` replaces the middle of the donut and receives the `total` of the visible slices, as a number and already `formatted`.

```svelte
<script lang="ts">
	import { PieChart } from '@likable-hair/svelte';

	const data = {
		labels: ['Rent', 'Salaries', 'Products', 'Marketing', 'Utilities', 'Other'],
		datasets: [{ label: 'Costs', data: [2400, 9800, 3100, 1200, 640, 420] }]
	};
</script>

<PieChart {data} label="Costs of the month" format={{ style: 'currency', currency: 'EUR', maximumFractionDigits: 0 }}>
	{#snippet centerSnippet({ formatted })}
		<div class="center">
			<small>This month</small>
			<strong>{formatted}</strong>
		</div>
	{/snippet}
</PieChart>

<style>
	.center {
		display: grid;
		gap: 4px;
	}

	small {
		color: var(--global-color-text-3);
	}

	strong {
		font-size: 20px;
	}
</style>
```

### Click on a slice

`onitemclick` receives the clicked slice, for example to open the list of customers in that status.

```svelte
<script lang="ts">
	import { PieChart } from '@likable-hair/svelte';

	let selected = $state('Click a slice');
	const data = {
		labels: ['New', 'Active', 'Dormant', 'Lost'],
		datasets: [{ label: 'Customers', data: [64, 412, 133, 58] }]
	};
</script>

<div class="stack">
	<PieChart {data} label="Customers by status" centerLabel="customers" onitemclick={({ label, value }) => (selected = `${label}: ${value}`)} />
	<p>{selected}</p>
</div>

<style>
	.stack {
		display: grid;
		inline-size: 100%;
		gap: 8px;
	}

	p {
		margin: 0;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `data` (required) | `ChartData` |  | The slices: one label in `labels` and one value in `datasets[0].data` each. More datasets draw more rings. Svelte state can be passed as it is. |
| `label` | `string` |  | Name of the chart for screen readers, also the caption of the hidden table of values. |
| `donut` | `boolean` | `true` | Draws a donut with the total in the middle; `false` draws a full pie. The hole is `--pie-chart-cutout` (72%). |
| `showTotal` | `boolean` | `true` | Shows the total of the visible slices in the middle of the donut. |
| `centerLabel` | `string` |  | Small text below the total, for example what is counted. |
| `format` | `ChartFormat` |  | How values are written: options of `Intl.NumberFormat` (for example `{ style: 'currency', currency: 'EUR' }`) or a function. |
| `legend` | `'auto' \| false \| 'top' \| 'bottom' \| 'right'` | `'auto'` | Where the legend goes. `auto` shows it on the right (below when the chart is narrow) when there are two or more slices. |
| `options` | `ChartOptions<'doughnut'>` |  | Any other chart.js option of a doughnut chart, merged over the defaults of the library. |
| `locale` | `string` |  | BCP 47 locale of the numbers (`it` and `en` are built in, other languages fall back to English). Defaults to the one set with `setDefaultLocale`, else `en`. |
| `chart` (bindable) | `Chart` |  | The chart.js instance, to call its methods (`toBase64Image()`). |
| `pieChartElement` (bindable) | `HTMLDivElement` |  | The root `<div>` element. |
| `class` | `string` | `''` | Extra classes on the root `<div>` element. |

It also accepts the native attributes and events of `<div>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `tooltipSnippet` | `ChartTooltip` | Replaces the content of the tooltip. |
| `legendSnippet` | `{ items: ChartLegendItem[]; toggle: (key: string) => void }` | Replaces the legend; `toggle(key)` hides or shows a slice. |
| `centerSnippet` | `{ total: number; formatted: string }` | Replaces the content in the middle of the donut; `total` counts the visible slices, `formatted` is written with `format`. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onitemclick` | `(item: ChartItemClick) => void` | Called with the slice clicked; the pointer becomes a hand over the slices. |

## Types

Exported by the package: `import type { ChartData, ChartFormat, ChartTooltip, ChartLegendItem, ChartItemClick, ChartSeries, ChartTooltipItem } from '@likable-hair/svelte';`

### ChartData

The data of a chart: the labels of the categories and the series. Same shape as the `data` of chart.js.

`interface ChartData`

| Name | Type | Description |
| --- | --- | --- |
| `labels` (required) | `string[]` | One label per category: months, products, countries. |
| `datasets` (required) | `ChartSeries[]` | The series, drawn in order. |

### ChartFormat

How values are written: options of `Intl.NumberFormat` (in the chart's `locale`), or a function. Axes write numbers in short form (`€1.2M`), tooltips in full.

`type ChartFormat = Intl.NumberFormatOptions \| ((value: number) => string)`

### ChartTooltip

What the tooltip shows.

`interface ChartTooltip`

| Name | Type | Description |
| --- | --- | --- |
| `title` (required) | `string` | The label of the category. |
| `items` (required) | `ChartTooltipItem[]` | One row per series (one in a PieChart). |

### ChartLegendItem

One entry of the legend: a series, or a slice in a PieChart.

`interface ChartLegendItem`

| Name | Type | Description |
| --- | --- | --- |
| `key` (required) | `string` | Stable key of the entry. |
| `label` (required) | `string` | Name shown. |
| `color` (required) | `string` | CSS color of the swatch (can be a gradient). |
| `hidden` (required) | `boolean` | The series or slice is hidden. |
| `detail` | `string` | Extra text after the name, for example the share of a slice. |

### ChartItemClick

The value clicked in a chart.

`interface ChartItemClick`

| Name | Type | Description |
| --- | --- | --- |
| `index` (required) | `number` | Position of the value in `data`, the same as its label in `labels`. |
| `datasetIndex` (required) | `number` | Position of the series in `datasets`. |
| `label` (required) | `string` | The label of the category. |
| `value` (required) | `number \| null` | The value as in the data. |
| `event` (required) | `MouseEvent` | The native click event. |

### ChartSeries

One series of a chart: the values, one per label, and how they are drawn. Any other key of a chart.js dataset passes through unchanged (for example `borderWidth`, `pointStyle`, `order`).

`interface ChartSeries`

| Name | Type | Description |
| --- | --- | --- |
| `label` | `string` | Name of the series, in the legend, the tooltip and the table read by screen readers. |
| `data` (required) | `(number \| null)[]` | One value per label; `null` leaves a gap. |
| `color` | `string \| string[]` | Any CSS color, also `var(--…)`, `light-dark()` and `color-mix()`; an array gives one color per bar or slice. Defaults to the data palette of the theme (`--global-color-data-1…6`). |
| `type` | `'bar' \| 'line'` | In a BarChart, `line` draws this series as a line over the bars. |
| `axis` | `'left' \| 'right'` | `right` puts the series on a second value axis on the right (at the top in a horizontal chart), with its own scale and format. |
| `format` | `ChartFormat` | Format of this series, instead of the chart's `format`. |
| `dashed` | `boolean` | Draws the line dashed, for example a comparison with the previous year. |
| `area` | `boolean` | Fills the area under the line with a fading gradient. |

### ChartTooltipItem

One row of the tooltip.

`interface ChartTooltipItem`

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | Name of the series (empty in a PieChart). |
| `value` (required) | `string` | The value, already formatted. |
| `raw` (required) | `number \| null` | The value as in the data. |
| `color` (required) | `string` | CSS color of the series or slice. |
| `datasetIndex` (required) | `number` | Position of the series in `datasets`. |
| `index` (required) | `number` | Position of the value in `data`, the same as its label in `labels`. |

## CSS variables

For a single instance, pass them as style props: `<PieChart --pie-chart-slice-border-color="tomato">`. The `--pie-chart-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--pie-chart-width` | `--pie-chart-default-width`: `100%` |
| `--pie-chart-min-width` | `--pie-chart-default-min-width`: `12rem` |
| `--pie-chart-height` | `--pie-chart-default-height`: `14rem` |
| `--pie-chart-gap` | `--pie-chart-default-gap`: `24px` |
| `--pie-chart-cutout` | `--pie-chart-default-cutout`: `72%` |
| `--pie-chart-slice-border-color` | `--pie-chart-default-slice-border-color`: `var(--global-color-surface-solid)` |
| `--pie-chart-slice-border-width` | `--pie-chart-default-slice-border-width`: `2px` |
| `--pie-chart-center-color` | `--pie-chart-default-center-color`: `var(--global-color-text)` |
| `--pie-chart-center-font-family` | `--pie-chart-default-center-font-family`: `var(--global-font-family-display)` |
| `--pie-chart-center-font-size` | `--pie-chart-default-center-font-size`: `28px` |
| `--pie-chart-center-label-color` | `--pie-chart-default-center-label-color`: `var(--global-color-text-3)` |
| `--pie-chart-center-label-font-size` | `--pie-chart-default-center-label-font-size`: `11px` |
| `--pie-chart-legend-gap` | `--pie-chart-default-legend-gap`: `16px` |
| `--pie-chart-legend-color` | `--pie-chart-default-legend-color`: `var(--global-color-text-2)` |
| `--pie-chart-legend-hidden-opacity` | `--pie-chart-default-legend-hidden-opacity`: `0.45` |
| `--pie-chart-legend-detail-color` | `--pie-chart-default-legend-detail-color`: `var(--global-color-text)` |
| `--pie-chart-legend-detail-font-family` | `--pie-chart-default-legend-detail-font-family`: `var(--global-font-family-mono)` |
| `--pie-chart-legend-min-width` | `--pie-chart-default-legend-min-width`: `180px` |
| `--pie-chart-legend-font-size` | `--pie-chart-default-legend-font-size`: `13px` |
| `--pie-chart-legend-swatch-size` | `--pie-chart-default-legend-swatch-size`: `10px` |
| `--pie-chart-legend-swatch-radius` | `--pie-chart-default-legend-swatch-radius`: `3px` |
| `--pie-chart-tooltip-background` | `--pie-chart-default-tooltip-background`: `var(--global-color-surface-pop)` |
| `--pie-chart-tooltip-border-color` | `--pie-chart-default-tooltip-border-color`: `var(--global-color-border-strong)` |
| `--pie-chart-tooltip-border-width` | `--pie-chart-default-tooltip-border-width`: `var(--global-border-width)` |
| `--pie-chart-tooltip-radius` | `--pie-chart-default-tooltip-radius`: `var(--global-radius-lg)` |
| `--pie-chart-tooltip-shadow` | `--pie-chart-default-tooltip-shadow`: `var(--global-shadow-md)` |
| `--pie-chart-tooltip-padding` | `--pie-chart-default-tooltip-padding`: `8px 12px` |
| `--pie-chart-tooltip-min-width` | `--pie-chart-default-tooltip-min-width`: `auto` |
| `--pie-chart-tooltip-color` | `--pie-chart-default-tooltip-color`: `var(--global-color-text)` |
| `--pie-chart-tooltip-title-color` | `--pie-chart-default-tooltip-title-color`: `var(--global-color-text-3)` |
| `--pie-chart-tooltip-title-font-family` | `--pie-chart-default-tooltip-title-font-family`: `var(--global-font-family-mono)` |
| `--pie-chart-tooltip-title-font-size` | `--pie-chart-default-tooltip-title-font-size`: `10.5px` |
| `--pie-chart-tooltip-label-color` | `--pie-chart-default-tooltip-label-color`: `var(--global-color-text-2)` |
| `--pie-chart-tooltip-label-font-size` | `--pie-chart-default-tooltip-label-font-size`: `12px` |
| `--pie-chart-tooltip-value-font-family` | `--pie-chart-default-tooltip-value-font-family`: `var(--global-font-family-display)` |
| `--pie-chart-tooltip-value-font-size` | `--pie-chart-default-tooltip-value-font-size`: `13px` |
