Data Display & Media
Chart
The frame around a chart: series colours from the theme, a legend, and the axes and tooltip in the Nana look. Bring the chart library you prefer.
Nana does not draw charts. It gives a chart its colours, its legend and its look, and leaves the drawing to a chart library. The examples use LayerChart, which reads the colours as CSS variables; canvas and SVG libraries such as ApexCharts get the same colours as concrete values. The chart is available for Svelte.
Preview
Section titled “Preview”- Premiums
- Claims paid
<script lang="ts">
import { BarChart } from 'layerchart';
import { NanaChartContainer, NanaChartLegend, type ChartConfig } from '$lib';
const config: ChartConfig = {
premiums: { label: 'Premiums' },
claims: { label: 'Claims paid' },
};
const data = [
{ month: 'Jan', premiums: 186, claims: 80 },
{ month: 'Feb', premiums: 305, claims: 200 },
{ month: 'Mar', premiums: 237, claims: 120 },
{ month: 'Apr', premiums: 173, claims: 190 },
{ month: 'May', premiums: 209, claims: 130 },
{ month: 'Jun', premiums: 214, claims: 140 },
];
</script>
<div class="w-full max-w-xl">
<NanaChartContainer {config} aria-label="Premiums and claims paid by month, in thousands">
{#snippet children({ series })}
<BarChart {data} x="month" {series} seriesLayout="group" />
{/snippet}
</NanaChartContainer>
<NanaChartLegend {config} />
</div> Installation
Section titled “Installation”npx @nana-tec/ui add chart --framework svelteLands in src/lib/components/ui/chart/ as NanaChartContainer.svelte and
NanaChartLegend.svelte. It installs no chart library, so add the one you use:
npm install layerchartThe config
Section titled “The config”A chart starts from a config: one entry per series, keyed by the field that holds its values, with the label people read.
import type { ChartConfig } from '$lib/components/ui/chart';
const config: ChartConfig = { premiums: { label: 'Premiums' }, claims: { label: 'Claims paid' },};NanaChartContainer turns each entry into a CSS variable named after its key,
--chart-premiums and --chart-claims, set on the container. Anything inside
can use them: a chart library, an SVG you draw yourself, or a Tailwind class such
as fill-(--chart-premiums).
Colours
Section titled “Colours”Series take the theme’s chart colours in order, --nana-chart-1 to
--nana-chart-5. Each set is checked for colour-blind separation on its own
surface, in light and in dark. A sixth series gets a quiet grey: more than five
series rarely read well, so fold the smallest into “Other” or split the chart.
Give a series its own colour in the config. Prefer a token, so the colour follows the theme:
const config: ChartConfig = { premiums: { label: 'Premiums', color: 'var(--nana-chart-3)' }, target: { label: 'Target', color: 'var(--nana-surface-text-subtle)' },};To change the palette everywhere, override the tokens in your theme:
:root { --nana-chart-1: #2563eb;}With LayerChart
Section titled “With LayerChart”The container’s snippet receives series: each key with its label and its
colour variable, in the shape LayerChart’s charts take.
<script lang="ts"> import { BarChart } from 'layerchart'; import { NanaChartContainer, NanaChartLegend } from '$lib/components/ui/chart';</script>
<NanaChartContainer {config} aria-label="Premiums and claims paid by month"> {#snippet children({ series })} <BarChart {data} x="month" {series} seriesLayout="group" /> {/snippet}</NanaChartContainer><NanaChartLegend {config} />The container styles LayerChart’s axes, grid, highlight and tooltip with the
Nana tokens, and keeps the tooltip inside the chart so it shares the series
colours. AreaChart, LineChart and PieChart work the same way.
With ApexCharts
Section titled “With ApexCharts”Libraries that paint to a canvas or set SVG attributes cannot read var(). The
snippet also receives colors: each series key mapped to the colour it resolves
to right now. The container resolves them again when the theme changes, so a
Svelte action that updates the chart keeps it in step with dark mode.
<script lang="ts"> import ApexCharts from 'apexcharts'; import { NanaChartContainer, type ChartConfig } from '$lib/components/ui/chart';
const config: ChartConfig = { premiums: { label: 'Premiums' }, claims: { label: 'Claims paid' }, };
function apex(node: HTMLElement, colors: Record<string, string>) { const chart = new ApexCharts(node, { chart: { type: 'bar', height: 280, toolbar: { show: false }, foreColor: 'currentColor' }, series: [ { name: 'Premiums', data: [186, 305, 237, 173] }, { name: 'Claims paid', data: [80, 200, 120, 190] }, ], xaxis: { categories: ['Jan', 'Feb', 'Mar', 'Apr'] }, colors: [colors.premiums, colors.claims], }); chart.render(); return { update: (next: Record<string, string>) => chart.updateOptions({ colors: [next.premiums, next.claims] }), destroy: () => chart.destroy(), }; }</script>
<NanaChartContainer {config} class="aspect-auto"> {#snippet children({ colors })} <div class="w-full" use:apex={colors}></div> {/snippet}</NanaChartContainer>The same values are available outside Svelte from @nana-tec/ui-dom/chart:
resolveChartColors(element, config) reads them, and watchTheme(callback)
reports a theme change and returns a function that stops watching.
Examples
Section titled “Examples”Ready-made layouts to copy. Each takes its colours from the config, so it follows the theme and dark mode without changes.
Area chart in a card
Section titled “Area chart in a card”Policies sold
January to June 2026
<script lang="ts">
import { AreaChart } from 'layerchart';
import {
NanaCard,
NanaCardContent,
NanaCardDescription,
NanaCardFooter,
NanaCardHeader,
NanaCardTitle,
NanaChartContainer,
type ChartConfig,
} from '$lib';
const config: ChartConfig = {
policies: { label: 'Policies sold' },
};
const data = [
{ month: new Date(2026, 0, 1), policies: 186 },
{ month: new Date(2026, 1, 1), policies: 305 },
{ month: new Date(2026, 2, 1), policies: 237 },
{ month: new Date(2026, 3, 1), policies: 273 },
{ month: new Date(2026, 4, 1), policies: 209 },
{ month: new Date(2026, 5, 1), policies: 314 },
];
</script>
<NanaCard class="w-full max-w-md">
<NanaCardHeader>
<NanaCardTitle>Policies sold</NanaCardTitle>
<NanaCardDescription>January to June 2026</NanaCardDescription>
</NanaCardHeader>
<NanaCardContent>
<NanaChartContainer {config} class="aspect-[2/1]" aria-label="Policies sold each month">
{#snippet children({ series })}
<AreaChart
{data}
x="month"
{series}
props={{
area: { fillOpacity: 0.25 },
xAxis: { format: (d: Date) => d.toLocaleString('en', { month: 'short' }) },
}}
/>
{/snippet}
</NanaChartContainer>
</NanaCardContent>
<NanaCardFooter class="text-sm text-foreground-muted">
Up 12% on the previous six months
</NanaCardFooter>
</NanaCard> Stacked bars
Section titled “Stacked bars”Stack parts of a whole with seriesLayout="stack". Keep the stack to three or
four series, so each segment stays readable.
- Motor
- Health
- Property
<script lang="ts">
import { BarChart } from 'layerchart';
import { NanaChartContainer, NanaChartLegend, type ChartConfig } from '$lib';
const config: ChartConfig = {
motor: { label: 'Motor' },
health: { label: 'Health' },
property: { label: 'Property' },
};
const data = [
{ quarter: 'Q1', motor: 120, health: 80, property: 45 },
{ quarter: 'Q2', motor: 140, health: 95, property: 52 },
{ quarter: 'Q3', motor: 132, health: 110, property: 61 },
{ quarter: 'Q4', motor: 158, health: 118, property: 70 },
];
</script>
<div class="w-full max-w-xl">
<NanaChartContainer {config} aria-label="Claims by line of business each quarter">
{#snippet children({ series })}
<BarChart {data} x="quarter" {series} seriesLayout="stack" />
{/snippet}
</NanaChartContainer>
<NanaChartLegend {config} />
</div> Line chart
Section titled “Line chart”- Renewals
- Lapses
<script lang="ts">
import { LineChart } from 'layerchart';
import { NanaChartContainer, NanaChartLegend, type ChartConfig } from '$lib';
const config: ChartConfig = {
renewals: { label: 'Renewals' },
lapses: { label: 'Lapses' },
};
const data = [
{ week: 1, renewals: 42, lapses: 8 },
{ week: 2, renewals: 51, lapses: 11 },
{ week: 3, renewals: 47, lapses: 6 },
{ week: 4, renewals: 63, lapses: 9 },
{ week: 5, renewals: 58, lapses: 14 },
{ week: 6, renewals: 70, lapses: 10 },
];
</script>
<div class="w-full max-w-xl">
<NanaChartContainer {config} aria-label="Renewals and lapses by week">
{#snippet children({ series })}
<LineChart {data} x="week" {series} points />
{/snippet}
</NanaChartContainer>
<NanaChartLegend {config} />
</div> A donut colours each slice by row, so map the config onto LayerChart’s colour
scale with cDomain and cRange.
- M-Pesa
- Card
- Bank transfer
- Cash
<script lang="ts">
import { PieChart } from 'layerchart';
import { NanaChartContainer, NanaChartLegend, type ChartConfig } from '$lib';
const config: ChartConfig = {
mpesa: { label: 'M-Pesa' },
card: { label: 'Card' },
bank: { label: 'Bank transfer' },
cash: { label: 'Cash' },
};
const data = [
{ method: 'mpesa', share: 58 },
{ method: 'card', share: 21 },
{ method: 'bank', share: 15 },
{ method: 'cash', share: 6 },
];
</script>
<div class="w-full max-w-xs">
<NanaChartContainer {config} class="aspect-square" aria-label="Premiums by payment method">
{#snippet children({ series })}
<PieChart
{data}
key="method"
value="share"
label={(row) => config[row.method]?.label ?? row.method}
cDomain={series.map((item) => item.key)}
cRange={series.map((item) => item.color)}
innerRadius={-24}
cornerRadius={4}
padAngle={0.02}
/>
{/snippet}
</NanaChartContainer>
<NanaChartLegend {config} />
</div> Legend
Section titled “Legend”NanaChartLegend lists the series with a swatch beside each name. Inside a
container it reads the container’s config; outside one, pass config. To list
only some series, pass their keys.
<NanaChartLegend {config} keys={['premiums']} />Use the legend whenever a chart has two or more series, so a series is never told apart by colour alone.
Sizing
Section titled “Sizing”The container fills its width at a 16:9 ratio. Set a height or another ratio with a class:
<NanaChartContainer {config} class="aspect-auto h-72">Accessibility
Section titled “Accessibility”- Name the chart with
aria-labelon the container, saying what it shows. - A chart is hard to read with a screen reader. Put the same numbers in a table nearby, or a sentence with the main finding.
- The legend is a list, and its swatches are hidden from assistive technology.
NanaChartContainer
| Prop | Type |
|---|---|
configrequired | ChartConfig |
id | string |
children | Snippet<[{ colors: Record<string, string>; series: ChartSeries[] }]> |
Also accepts Omit<HTMLAttributes<HTMLDivElement>, 'class' | 'children'>.
NanaChartLegend
| Prop | Type |
|---|---|
config | ChartConfig |
keys | string[] |
Also accepts Omit<HTMLAttributes<HTMLUListElement>, 'class'>.
config is required on NanaChartContainer. id names the chart in
data-chart; one is generated when you leave it out. The children snippet
receives series and colors. On NanaChartLegend, config is needed only
outside a container, and keys limits the series it lists.