Stacked bar chart
A total per period, split into its parts: revenue by product, requests by status. The height of a bar is the total, and only the bottom part shares a baseline, so use it when the total matters most. To compare the trends of the parts, use the line chart.
Install
npx shadcn@latest add @thrum-pro/stacked-bar-chartThis is a Pro component. The command works once your project has a licence key: set up Pro or see pricing.
Data shape
labels: readonly string[]; // one per bar
type StackedBarChartSeries = {
key: string; // stable id
name: string; // legend and readout
values: (number | null)[]; // one per label
color?: string;
};[
{ key: "plans", name: "Plans", values: [620, 655, null, 702] },
{ key: "add-ons", name: "Add-ons", values: [510, 498, 530, 541] },
]Props
| Prop | Type | Default | Description |
|---|---|---|---|
labels | readonly string[] | Required | Names of the bars, in order. |
series | readonly StackedBarChartSeries[] | Required | Up to five series. Each bar stacks them in this order, starting at zero. Negative values stack downwards from zero. A null adds nothing to the bar. A sixth series is drawn in a neutral colour. |
label | string | Required | What the chart measures. Read by screen readers and used as the table caption. |
format | Intl.NumberFormatOptions | Default compact | How values are written on the axis, in the readout and in the data table. |
locale | string | Default "en-US" | Locale for number formatting. Fixed by default so the server and the browser print the same text. |
yAxis | boolean | Default true | Show value lines and their labels. The axis always includes zero. |
xAxis | boolean | Default true | Show bar labels along the bottom. As many as fit are shown, always including the first and last. |
legend | boolean | Default true | Show a swatch and a name for each series. It appears when there are two or more series. |
onActiveChange | (index: number | null) => void | – | Called with the index of the bar under the cursor, or null when the cursor leaves. Use it to sync another chart or a table row. |
sound | boolean | Default true | Play a tick as the cursor moves between bars, if the visitor has sound on. |
emptyLabel | string | Default "No data" | Text shown when no series has a value. |
className | string | – | Sets the size. The default is full width and 256 pixels tall, legend included. |
Motion and sound
Motion
- First draw: each bar grows from the zero line in 250 ms. Bars start 40 ms apart, and the whole group starts within 300 ms however many bars there are.
- New data with the same number of bars and series: every part moves to its new size in 400 ms with no overshoot. A different number switches at once.
- The band behind the active bar follows the pointer on a stiff spring.
- Reduced motion: the chart appears complete, and data changes and the cursor move instantly.
Sound
- A soft tick each time the cursor lands on a different bar, at most one every 40 ms.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- The chart is one tab stop. It is exposed as a slider whose value text is the whole bar, for example “Tue: Plans $620, Add-ons $510, total $1,130”.
- A visually hidden table carries every value, one column per series and one for the total.
- A missing value reads “No data” in the readout and in the table, so it is not mistaken for zero.
- The legend is a plain list. In the readout a colour swatch sits beside each name, and names and values stay in the text colour.
- The whole plot is the pointer target, so a short bar is as easy to read as a tall one.
| Keys | Action |
|---|---|
| Tab | Focus the chart. The cursor goes to the last bar. |
| ← → | Move the cursor one bar. |
| Home / End | Jump to the first or last bar. |
| Esc | Hide the cursor. |
Source
The source comes with Pro
TypeScript source · Copy-paste ready · MIT-compatible for client projects · Includes all dependencies
Files and dependencies
- Adds
components/ui/stacked-bar-chart.tsx- Packages
motion- Brings along
- Chart foundationsMotion tokensSound engine
Changelog
- 1.0.1 · 2026-12-03Corners follow the project's --radius, down to square.
- 1.0.0 · 2026-12-02First release.