# LineChart

Line chart drawn with chart.js. Pass `data` as `{ labels, datasets: [{ label, data, color? }] }`: one label per point on the horizontal axis, one value per label in every series (`null` leaves a gap). Lines are smooth; a series with `area` fills the space below it with a fading gradient, `dashed` draws it dashed (for example the previous year), `axis: 'right'` gives it a second value axis with its own `format`, and `stacked` puts the series on top of each other. Colors come from the theme (`--global-color-data-1…6`; when only one series has no `color` it gets the brand gradient with a soft glow) and follow light and dark mode and theme changes by themselves; `color` takes any CSS color, also `var(--…)`. While hovering, a vertical line marks the point and the tooltip lists every series. `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 point. `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 `--line-chart-height` (16rem; `100%` fills a sized container).

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

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

## Examples

### Basic

One series takes the brand gradient of the theme with a soft glow; `area` fills the space below the line. While hovering, a vertical line marks the month and the tooltip shows the value written with `format`.

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

	const data = {
		labels: ['Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'],
		datasets: [
			{
				label: 'Revenue',
				data: [3120, 3480, 3390, 4210, 3650, 3820, 3540, 4380, 4870, 5020, 4760, 5240],
				area: true
			}
		]
	};
</script>

<LineChart {data} label="Revenue, last 12 months" format={{ style: 'currency', currency: 'EUR', maximumFractionDigits: 0 }} />
```

### Comparison with last year

The current year with `area`, the previous one `dashed` in a quiet color from the theme: only the series without `color` takes the gradient. The legend appears because there are two series.

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

	const data = {
		labels: ['Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'],
		datasets: [
			{ label: '2026', data: [3120, 3480, 3390, 4210, 3650, 3820, 3540, 4380, 4870, 5020, 4760, 5240], area: true },
			{
				label: '2025',
				data: [2810, 2950, 3100, 3600, 3240, 3300, 3180, 3560, 3820, 3990, 3870, 4100],
				dashed: true,
				color: 'var(--global-color-text-3)'
			}
		]
	};
</script>

<LineChart {data} label="Revenue, this year and last year" format={{ style: 'currency', currency: 'EUR', maximumFractionDigits: 0 }} />
```

### Several series

Every series takes the next color of the data palette. A click on a legend entry hides or shows the series; the value axis adapts to the visible ones.

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

	const data = {
		labels: ['W1', 'W2', 'W3', 'W4', 'W5', 'W6', 'W7', 'W8'],
		datasets: [
			{ label: 'Milan', data: [42, 48, 45, 52, 58, 61, 57, 66] },
			{ label: 'Rome', data: [35, 33, 39, 41, 40, 46, 50, 49] },
			{ label: 'Turin', data: [18, 22, 21, 25, 24, 28, 31, 33] }
		]
	};
</script>

<LineChart {data} label="New customers per week by store" />
```

### Many points and zoom

A day measured every minute: 1,440 points. Drag across the chart to zoom on a stretch of time, hold Ctrl and drag to move; the "Reset zoom" button brings back the whole day. `zoom={false}` turns zooming off.

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

	const labels = Array.from({ length: 1440 }, (_, i) => `${String(Math.floor(i / 60)).padStart(2, '0')}:${String(i % 60).padStart(2, '0')}`);
	const values = labels.map((_, i) => {
		const hour = i / 60;
		const day = Math.max(0, Math.sin(((hour - 6) / 16) * Math.PI));
		return Math.round(30 + day * 170 + Math.sin(i / 40) * 10 + Math.cos(i / 97) * 6);
	});
	const data = { labels, datasets: [{ label: 'Requests', data: values }] };
</script>

<LineChart {data} label="Requests per minute" />
```

### Custom tooltip

`tooltipSnippet` replaces the content of the tooltip; it receives the `title` and one item per series with the formatted `value`, the `raw` number and the `color`. The card around it stays the one of the library.

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

	const target = 50;
	const data = {
		labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
		datasets: [{ label: 'Bookings', data: [38, 44, 41, 56, 63, 71, 29] }]
	};
</script>

<LineChart {data} label="Bookings per day">
	{#snippet tooltipSnippet({ title, items })}
		<strong>{title}</strong>
		{#each items as item (item.datasetIndex)}
			<span>{item.value} bookings · {(item.raw ?? 0) >= target ? 'above' : 'below'} target</span>
		{/each}
	{/snippet}
</LineChart>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `data` (required) | `ChartData` |  | The labels of the horizontal axis (`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. |
| `stacked` | `boolean` | `false` | Puts the series on top of each other. |
| `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 horizontal axis, 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<'line'>` |  | Any other chart.js option, merged over the defaults of the library (for example `scales.y.min`, `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()`). |
| `lineChartElement` (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 point clicked; the pointer becomes a hand over the points. |

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

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