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

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

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

## Examples

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

```svelte
<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.

```svelte
<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.

```svelte
<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.

```svelte
<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`.

```svelte
<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.

```svelte
<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.

```svelte
<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

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `data` (required) | `ChartData` |  | The categories (`labels`) and the series (`datasets`), with one value per label. 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. |
| `horizontal` | `boolean` | `false` | Horizontal 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` | `boolean` | `false` | Puts the series on top of each other instead of side by side. |
| `format` | `ChartFormat` |  | How 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` | `boolean` | `true` | Drag 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` | `string` |  | BCP 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` | `string` |  | Text of the button that resets the zoom. Defaults to "Reset zoom", in the language of `locale`. |
| `chart` (bindable) | `Chart` |  | The chart.js instance, to call its methods (`resetZoom()`, `toBase64Image()`). |
| `barChartElement` (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 series. |

## Events

| Name | Type | Description |
| --- | --- | --- |
| `onitemclick` | `(item: ChartItemClick) => void` | Called 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`

| 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: `<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 variable | Default |
| --- | --- |
| `--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` |
