Stacked Bar Chart

CwStackedBarChart compares how several categories add up over time, such as traffic counts per hour. Colours come from the shared --cw-chart-cat-* palette and follow the document theme, the legend toggles series, and bars, ticks and tooltips format values with your formatValue function.

Basic usage

05010015020006:0006:00: 5207:0007:00: 6508:0008:00: 5309:0009:00: 11710:0010:00: 7511:0011:00: 9712:0012:00: 8613:0013:00: 4114:0014:00: 5415:0015:00: 108

Click a legend item to hide a series. The last visible series can't be hidden. Switch the theme in the header to see the palette follow it.

CwStackedBarChart example Copy code
<script lang="ts">
	import { CwStackedBarChart } from '@cropwatchdevelopment/cwui';
	import type { CwStackedBarBucket, CwStackedBarCategory } from '@cropwatchdevelopment/cwui';

	const categories: CwStackedBarCategory[] = [
		{ id: 'car', label: 'Cars' },
		{ id: 'truck', label: 'Trucks' }
	];

	const buckets: CwStackedBarBucket[] = [
		{ key: '2026-09-23T06:00', label: '06:00', values: { car: 42, truck: 9 } },
		{ key: '2026-09-23T07:00', label: '07:00', values: { car: 58, truck: 12 } }
	];
</script>

<CwStackedBarChart {categories} {buckets} height={280} />

Hidden series and value formatting

initialHidden starts with Bicycles and Pedestrians switched off, and formatValue adds a unit to ticks and tooltips.

0 counted25 counted50 counted75 counted100 counted06:0006:00: 42 counted07:0007:00: 57 counted08:0008:00: 35 counted09:0009:00: 86 counted10:0010:00: 40 counted11:0011:00: 66 counted12:0012:00: 59 counted13:0013:00: 28 counted14:0014:00: 27 counted15:0015:00: 76 counted

Loading and no data

Loading
No buckets
00.250.50.751

No data available

Custom message
00.250.50.751

No traffic recorded today

Loading and empty states Copy code
<!-- Skeleton bars while data loads -->
<CwStackedBarChart {categories} {buckets} loading={isLoading} />

<!-- Empty buckets show the shared no-data overlay automatically -->
<CwStackedBarChart {categories} buckets={[]} />

<!-- Custom message -->
<CwStackedBarChart {categories} buckets={[]} noData="No traffic recorded today" />
API

Usage and API reference

CwStackedBarChart shows how several categories add up per bucket, such as counts per hour. Colours come from the `--cw-chart-cat-*` tokens and follow the document theme; the legend toggles series and never hides the last one.

How to think about it

  1. Define categories in stack order `categories` is bottom-of-stack first. Give each a stable `id` and a translated `label`; `color` is optional.
  2. One bucket per bar `buckets` holds a `key`, a pre-formatted `label`, and a `values` map from category id to number.
  3. Format values with your locale `formatValue` is used for ticks, tooltips and accessible bar labels.
  4. Let the states take care of themselves `loading` shows skeleton bars. With no buckets, or when everything is zero, the no-data overlay appears automatically.

Props and callbacks

APITypeDetails
categories
CwStackedBarCategory[]Series in stack order: `{ id, label, color? }`.
buckets
CwStackedBarBucket[]Bars in x-axis order: `{ key, label, values }`.
initialHidden
string[]Category ids hidden on first render.
height
numberPlot height in pixels.
showLegend

Default: true

booleanShows the interactive legend.
formatValue
(value: number) => stringFormats ticks, tooltips and accessible labels.
loading

Default: false

booleanShows skeleton bars.
noData
string | booleanForces the no-data overlay, optionally with custom text. Automatic when omitted.
theme
'light' | 'dark'Forces a fixed palette. Omit it to follow the document theme.
labels
CwStackedBarChartLabels`ariaLabel`, `total`, `barLabel(label, total)`, `loading`.

Copy-paste examples

Each snippet is a complete, working example you can paste into a Svelte 5 component.

Counts per hour
Counts per hour Copy code
<script lang="ts">
	import { CwStackedBarChart } from '@cropwatchdevelopment/cwui';

	const categories = [
		{ id: 'car', label: 'Cars' },
		{ id: 'truck', label: 'Trucks' }
	];
	const buckets = [
		{ key: '06:00', label: '06:00', values: { car: 42, truck: 9 } },
		{ key: '07:00', label: '07:00', values: { car: 58, truck: 12 } }
	];
</script>

<CwStackedBarChart {categories} {buckets} height={280} />