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

BarChart

Bar chart drawn with chart.js. Pass data as { labels, datasets: [{ label, data, color? }] }: one label per category, one value per label in every series. Bars are vertical, or horizontal with horizontal (then the chart grows with the number of rows, --bar-chart-row-height each); several series stand side by side, or on top of each other with stacked. A series with type: 'line' is drawn as a line over the bars, and axis: 'right' gives it a second value axis with its own format. Colors come from the theme (--global-color-data-1…6, a gradient when one series has no color) and follow light and dark mode and theme changes by themselves; color takes any CSS color, also var(--…), or an array for one color per bar. format writes values with Intl.NumberFormat (short form on the axis, full in the tooltip). The legend appears with two or more series and hides a series on click. Drag on the chart to zoom (Ctrl + drag to pan); a "Reset zoom" icon appears in the top-right corner of the plot while zoomed, and a double click also resets the zoom. onitemclick receives the clicked bar. options takes any other chart.js option and wins over the defaults. Screen readers get label and a hidden table with the values. The chart fills the height of --bar-chart-height (16rem; 100% fills a sized container).

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

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

Basic

One series: the bars take the brand gradient of the theme. format writes the values as euros, in short form on the axis and in full in the tooltip; label names the chart for screen readers.

Revenue per month
Revenue
Jan€18,200
Feb€21,400
Mar€19,800
Apr€24,600
May€28,900
Jun€31,200
Jul€26,400
Aug€17,800
Sep€29,600
Oct€33,100
Nov€35,800
Dec€41,200
Code
<script lang="ts">
	import { BarChart } from '@likable-hair/svelte';

	const data = {
		labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
		datasets: [
			{ label: 'Revenue', data: [18200, 21400, 19800, 24600, 28900, 31200, 26400, 17800, 29600, 33100, 35800, 41200] }
		]
	};
</script>

<BarChart {data} label="Revenue per month" format={{ style: 'currency', currency: 'EUR', maximumFractionDigits: 0 }} />

Several series

Series stand side by side and take the colors of the data palette in order. With two or more series the legend appears below the chart: a click on an entry hides or shows that series.

Appointments per quarter
202420252026
Q1412455498
Q2468502561
Q3390448512
Q4521590640
Code
<script lang="ts">
	import { BarChart } from '@likable-hair/svelte';

	const data = {
		labels: ['Q1', 'Q2', 'Q3', 'Q4'],
		datasets: [
			{ label: '2024', data: [412, 468, 390, 521] },
			{ label: '2025', data: [455, 502, 448, 590] },
			{ label: '2026', data: [498, 561, 512, 640] }
		]
	};
</script>

<BarChart {data} label="Appointments per quarter" />

Stacked

stacked puts the series on top of each other; the tooltip lists every part of the bar. legend="top" moves the legend above the chart.

Services per day
CutColorTreatments
Mon1284
Tue1593
Wed1175
Thu18126
Fri22169
Sat302112
Code
<script lang="ts">
	import { BarChart } from '@likable-hair/svelte';

	const data = {
		labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
		datasets: [
			{ label: 'Cut', data: [12, 15, 11, 18, 22, 30] },
			{ label: 'Color', data: [8, 9, 7, 12, 16, 21] },
			{ label: 'Treatments', data: [4, 3, 5, 6, 9, 12] }
		]
	};
</script>

<BarChart {data} stacked legend="top" label="Services per day" />

Horizontal

horizontal puts the categories on the vertical axis, one row of --bar-chart-row-height each: the chart grows with the rows. With a fixed --bar-chart-height, as here, the rows scroll inside the chart.

Imports by country
Imports
Germany€9,800,000
France€7,400,000
Spain€5,200,000
Netherlands€4,900,000
Belgium€3,100,000
Austria€2,700,000
Poland€2,200,000
Sweden€1,900,000
Portugal€1,400,000
Greece€1,100,000
Ireland€800,000
Denmark€600,000
Code
<script lang="ts">
	import { BarChart } from '@likable-hair/svelte';

	const countries = ['Germany', 'France', 'Spain', 'Netherlands', 'Belgium', 'Austria', 'Poland', 'Sweden', 'Portugal', 'Greece', 'Ireland', 'Denmark'];
	const values = [9.8, 7.4, 5.2, 4.9, 3.1, 2.7, 2.2, 1.9, 1.4, 1.1, 0.8, 0.6];
	const data = { labels: countries, datasets: [{ label: 'Imports', data: values.map((v) => v * 1_000_000) }] };
</script>

<BarChart
	{data}
	horizontal
	label="Imports by country"
	format={{ style: 'currency', currency: 'EUR', maximumFractionDigits: 0 }}
	--bar-chart-height="18rem"
/>

Bars and a line

A series with type: 'line' is drawn as a line over the bars; axis: 'right' gives it a second value axis, written with its own format.

Goods value by quarter
Total invoicedGoods valueGoods share
Q1 2026€182,000€41,00022.5%
Q2 2026€214,000€52,00024.3%
Q3 2026€198,000€61,00030.8%
Q4 2026€246,000€70,00028.5%
Code
<script lang="ts">
	import { BarChart } from '@likable-hair/svelte';

	const totals = [182000, 214000, 198000, 246000];
	const goods = [41000, 52000, 61000, 70000];
	const data = {
		labels: ['Q1 2026', 'Q2 2026', 'Q3 2026', 'Q4 2026'],
		datasets: [
			{ label: 'Total invoiced', data: totals, color: 'var(--global-color-text-3)' },
			{ label: 'Goods value', data: goods },
			{
				label: 'Goods share',
				data: goods.map((g, i) => (g / totals[i]) * 100),
				type: 'line' as const,
				axis: 'right' as const,
				format: (v: number) => `${v.toFixed(1)}%`
			}
		]
	};
</script>

<BarChart {data} label="Goods value by quarter" format={{ style: 'currency', currency: 'EUR', maximumFractionDigits: 0 }} />

Colors

color takes any CSS color, also the tokens of the theme with var(--…): they follow light and dark mode by themselves. An array gives one color per bar, here to mark the days under target.

Appointments per day
Appointments
Mon28
Tue34
Wed26
Thu41
Fri52
Sat48
Sun19
Code
<script lang="ts">
	import { BarChart } from '@likable-hair/svelte';

	const values = [28, 34, 26, 41, 52, 48, 19];
	const data = {
		labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
		datasets: [
			{
				label: 'Appointments',
				data: values,
				color: values.map((v) => (v < 30 ? 'var(--global-color-warning)' : 'var(--global-color-success)'))
			}
		]
	};
</script>

<BarChart {data} label="Appointments per day" />

Click on a bar

onitemclick receives the clicked bar: its label, value and position. Use it to open the details of a category. The pointer becomes a hand over the bars.

Contracts by status
Contracts
Active128
Suspended14
Expired37
Draft9

Click a bar

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

	let selected = $state('Click a bar');
	const data = {
		labels: ['Active', 'Suspended', 'Expired', 'Draft'],
		datasets: [{ label: 'Contracts', data: [128, 14, 37, 9] }]
	};
</script>

<div class="stack">
	<BarChart {data} label="Contracts by status" 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 categories (labels) and the series (datasets), with one value per label. 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.
horizontal booleanfalseHorizontal bars: categories on the vertical axis, one row each (--bar-chart-row-height); the chart grows with the rows and scrolls when --bar-chart-height is smaller.
stacked booleanfalsePuts the series on top of each other instead of side by side.
format ChartFormatHow values are written: options of Intl.NumberFormat (for example { style: 'currency', currency: 'EUR' }) or a function. Axes use the short form (€1.2M). A series can have its own format.
legend 'auto' | false | 'top' | 'bottom' | 'right''auto'Where the legend goes. auto shows it below the chart when there are two or more series.
zoom booleantrueDrag on the chart to zoom on the categories, Ctrl + drag to pan; a "Reset zoom" icon appears in the top-right corner of the plot while zoomed, and a double click also resets the zoom.
options ChartOptions<'bar'>Any other chart.js option, merged over the defaults of the library (for example scales.y.max, scales.y.type: 'logarithmic').
locale stringBCP 47 locale of the texts and of the numbers (it and en are built in, other languages fall back to English). Defaults to the one set with setDefaultLocale, else en.
resetZoomLabel stringText of the button that resets the zoom. Defaults to "Reset zoom", in the language of locale.
chart bindable ChartThe chart.js instance, to call its methods (resetZoom(), toBase64Image()).
barChartElement 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 series.

Events

NameTypeDescription
onitemclick(item: ChartItemClick) => voidCalled with the bar (or point) clicked; the pointer becomes a hand over the bars.

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: <BarChart --bar-chart-grid-color="tomato">. The --bar-chart-default-* defaults apply to the whole app: override them in :root or in a theme.

Per-instance variableDefault
--bar-chart-width
--bar-chart-default-width 100%
--bar-chart-min-width
--bar-chart-default-min-width 12rem
--bar-chart-height
--bar-chart-default-height 16rem
--bar-chart-row-height
--bar-chart-default-row-height 2.5rem
--bar-chart-axis-size
--bar-chart-default-axis-size 2rem
--bar-chart-gap
--bar-chart-default-gap 12px
--bar-chart-grid-color
--bar-chart-default-grid-color var(--global-color-border)
--bar-chart-tick-color
--bar-chart-default-tick-color var(--global-color-text-3)
--bar-chart-tick-font-family
--bar-chart-default-tick-font-family var(--global-font-family-mono)
--bar-chart-tick-font-size
--bar-chart-default-tick-font-size 11px
--bar-chart-bar-radius
--bar-chart-default-bar-radius var(--global-radius-sm)
--bar-chart-gradient-from
--bar-chart-default-gradient-from var(--global-color-data-1)
--bar-chart-gradient-to
--bar-chart-default-gradient-to var(--global-color-data-2)
--bar-chart-line-width
--bar-chart-default-line-width 2px
--bar-chart-point-background
--bar-chart-default-point-background var(--global-color-surface-pop)
--bar-chart-reset-background
--bar-chart-default-reset-background var(--global-color-surface-pop)
--bar-chart-zoom-color
--bar-chart-default-zoom-color var(--global-color-primary)
--bar-chart-legend-gap
--bar-chart-default-legend-gap 16px
--bar-chart-legend-font-size
--bar-chart-default-legend-font-size 12.5px
--bar-chart-legend-color
--bar-chart-default-legend-color var(--global-color-text-2)
--bar-chart-legend-swatch-size
--bar-chart-default-legend-swatch-size 8px
--bar-chart-legend-swatch-radius
--bar-chart-default-legend-swatch-radius 2px
--bar-chart-legend-hidden-opacity
--bar-chart-default-legend-hidden-opacity 0.45
--bar-chart-legend-detail-color
--bar-chart-default-legend-detail-color var(--global-color-text)
--bar-chart-legend-detail-font-family
--bar-chart-default-legend-detail-font-family var(--global-font-family-mono)
--bar-chart-legend-min-width
--bar-chart-default-legend-min-width 180px
--bar-chart-tooltip-background
--bar-chart-default-tooltip-background var(--global-color-surface-pop)
--bar-chart-tooltip-border-color
--bar-chart-default-tooltip-border-color var(--global-color-border-strong)
--bar-chart-tooltip-border-width
--bar-chart-default-tooltip-border-width var(--global-border-width)
--bar-chart-tooltip-radius
--bar-chart-default-tooltip-radius var(--global-radius-lg)
--bar-chart-tooltip-shadow
--bar-chart-default-tooltip-shadow var(--global-shadow-md)
--bar-chart-tooltip-padding
--bar-chart-default-tooltip-padding 8px 12px
--bar-chart-tooltip-min-width
--bar-chart-default-tooltip-min-width auto
--bar-chart-tooltip-color
--bar-chart-default-tooltip-color var(--global-color-text)
--bar-chart-tooltip-title-color
--bar-chart-default-tooltip-title-color var(--global-color-text-3)
--bar-chart-tooltip-title-font-family
--bar-chart-default-tooltip-title-font-family var(--global-font-family-mono)
--bar-chart-tooltip-title-font-size
--bar-chart-default-tooltip-title-font-size 10.5px
--bar-chart-tooltip-label-color
--bar-chart-default-tooltip-label-color var(--global-color-text-2)
--bar-chart-tooltip-label-font-size
--bar-chart-default-tooltip-label-font-size 12px
--bar-chart-tooltip-value-font-family
--bar-chart-default-tooltip-value-font-family var(--global-font-family-display)
--bar-chart-tooltip-value-font-size
--bar-chart-default-tooltip-value-font-size 13px