Skip to content
Likable Aurora @likable-hair/svelte 5

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).

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

src/lib/components/simple/charts/PieChart.svelte · Markdown for LLMs

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.

1,284 services
Services by type
Services%
Color53942%
Cut33426%
Treatments23118%
Retail18014%
Code
<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.

Invoices by status
Invoices%
Paid18277.4%
Pending4117.4%
Overdue125.1%
Code
<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.

This month €17,560
Costs of the month
Costs%
Rent€2,40013.7%
Salaries€9,80055.8%
Products€3,10017.7%
Marketing€1,2006.8%
Utilities€6403.6%
Other€4202.4%
Code
<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.

667 customers
Customers by status
Customers%
New649.6%
Active41261.8%
Dormant13319.9%
Lost588.7%

Click a slice

Code
<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

NameTypeDefaultDescription
data requiredChartDataThe 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 stringName of the chart for screen readers, also the caption of the hidden table of values.
donut booleantrueDraws a donut with the total in the middle; false draws a full pie. The hole is --pie-chart-cutout (72%).
showTotal booleantrueShows the total of the visible slices in the middle of the donut.
centerLabel stringSmall text below the total, for example what is counted.
format ChartFormatHow 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 stringBCP 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 ChartThe chart.js instance, to call its methods (toBase64Image()).
pieChartElement bindable HTMLDivElementThe 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

NameParametersDescription
tooltipSnippetChartTooltipReplaces 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

NameTypeDescription
onitemclick(item: ChartItemClick) => voidCalled 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

NameTypeDescription
labels requiredstring[]One label per category: months, products, countries.
datasets requiredChartSeries[]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

NameTypeDescription
title requiredstringThe label of the category.
items requiredChartTooltipItem[]One row per series (one in a PieChart).

ChartLegendItem

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

interface ChartLegendItem

NameTypeDescription
key requiredstringStable key of the entry.
label requiredstringName shown.
color requiredstringCSS color of the swatch (can be a gradient).
hidden requiredbooleanThe series or slice is hidden.
detail stringExtra text after the name, for example the share of a slice.

ChartItemClick

The value clicked in a chart.

interface ChartItemClick

NameTypeDescription
index requirednumberPosition of the value in data, the same as its label in labels.
datasetIndex requirednumberPosition of the series in datasets.
label requiredstringThe label of the category.
value requirednumber | nullThe value as in the data.
event requiredMouseEventThe 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

NameTypeDescription
label stringName 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 ChartFormatFormat of this series, instead of the chart's format.
dashed booleanDraws the line dashed, for example a comparison with the previous year.
area booleanFills the area under the line with a fading gradient.

ChartTooltipItem

One row of the tooltip.

interface ChartTooltipItem

NameTypeDescription
label requiredstringName of the series (empty in a PieChart).
value requiredstringThe value, already formatted.
raw requirednumber | nullThe value as in the data.
color requiredstringCSS color of the series or slice.
datasetIndex requirednumberPosition of the series in datasets.
index requirednumberPosition 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 variableDefault
--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