Stacked Chart
Stacked chart for the parts of a total: stacked and 100 % columns, stacked areas, diverging Likert bars, Marimekko and gapped rows.
Playground
Installation
Stacked Chart comes with WunderUI Core and Pro, in the package from your plan. See the plans.
The charts live in their own entry point: import the chart, its card and its hooks from @wunderui/react/charts, not from @wunderui/react.
Then import what you need:
import {
StackedChart,
StackedChartProps,
StackedChartVariant,
} from "@wunderui/react/charts"Then use it:
import { StackedChart } from "@wunderui/react/charts"
const data = [
{ month: "Jan", direct: 14000, organic: 10000 },
{ month: "Feb", direct: 18000, organic: 11000 },
{ month: "Mar", direct: 16000, organic: 10000 },
{ month: "Apr", direct: 20000, organic: 14000 },
]
export function SessionsByChannel() {
return (
<StackedChart
data={data}
index="month"
series={[
{ key: "direct", label: "Direct" },
{ key: "organic", label: "Organic" },
]}
label="Sessions by channel"
/>
)
}Expected result: You see four columns, each made of a Direct and an Organic part stacked on one zero line.
When to use
Use Stacked Chart for
- The parts that make up a total, per category or period
- Shares of a whole over time (the 100 % variants)
- Survey answers on a scale (the diverging stack)
- Two dimensions at once: the width of a column says how big a group is (Marimekko)
Use something else for
- Bar Chart for comparing single values or a few series side by side
- Pie Chart for the parts of one total, at one moment
- Area Chart for overlapping series that are not parts of one total
- Treemap for parts of a hierarchy
States
Loading, empty and error keep the chart's size, so the card never jumps. Add a retry handler and the error state offers a Try again button.
Table view
Every chart carries a table of its data for screen readers. A ChartCard can show it on screen too, with a toggle in the header.
Keyboard interactions
The plot is focusable, so the chart works without a pointer.
- TaborShiftTab
- Moves focus to the plot and away from it. The plot is one tab stop; so is every legend badge.
- ArrowRightorArrowLeft
- Moves to the next or previous category. The tooltip follows, and a live region announces the one you are on. The first press starts at the first one (Left: the last one).
- HomeorEnd
- Jumps to the first or last category.
- ShiftArrowRightorShiftArrowLeft
- Extends a selected range from the point you started on. A range is only selected when is set.
- Escape
- Clears the active point and the selected range.
- EnterorSpace
- On a legend badge of a ChartCard: switches its series off or on. The last visible series stays on.
On horizontal and diverging stacks the categories run top to bottom, so Up and Down walk them instead of Left and Right.
Behavior and motion
What the chart does on its own and what the page decides.
- Stacking order
- Series stack in the order you list them, from the bottom or left. Put the part that matters most there, where the eye compares it best.
- Normalized variants
- The 100 percent bar and area variants and the Marimekko scale every stack to 100. The axis reads percent while the tooltip and table still show the raw amounts.
- Diverging stack
- The diverging bar puts 0 in the middle. You say how many series, counted from the first, go to the left (default: half of them).
- Segments
- Gap and bar width shape the segments, and the Marimekko percentages can be toggled. A series switched off in the legend keeps its place and fades, so the rest of the stack doesn't jump.
- Loading, empty and error
- The chart keeps its size while loading, empty or failed, so the card never jumps. Add a retry handler to show a Try again button on error.
- Legend
- A ChartCard shows the series as badges. With two or more series each badge switches its series off, but the last visible one stays on.
- Data table
- The chart renders its data as a table for screen readers, in the same order as the plot. A ChartCard can show that table on screen through a toggle that keeps the card's height.
- Size
- The chart fills the width of its container, and you set the plot height. Text never scales, so labels keep their size at every width.
Motion
The enter sequence and the hover follow the library's motion tokens.
- Grow
- Segments grow from the baseline (areas wipe in) when the chart first comes into view, after loading and when the data changes.
- Hover
- The bar variants light a muted band behind the category. The area variants follow it with a ringed point on top of each stack.
- Reduced motion
- With reduced motion the chart appears at once. The tooltip, the crosshair and every state still work.
Anatomy
A stacked chart splits each category into its parts, as columns, areas, 100 percent stacks, horizontal rows, a diverging Likert stack or a Marimekko whose column width is the share.
Sessions
| month | Direct | Organic | Referral |
|---|---|---|---|
| Jan | 14k | 10k | 5k |
| Feb | 18k | 11k | 7k |
| Mar | 16k | 10k | 6k |
| Apr | 20k | 14k | 8k |
| May | 19k | 13k | 7k |
| Jun | 23k | 15k | 9k |
| Jul | 21k | 14k | 8k |
| Aug | 25k | 16k | 10k |
| Sep | 22k | 15k | 9k |
| Oct | 20k | 13k | 8k |
| Nov | 24k | 16k | 10k |
| Dec | 27k | 17k | 11k |
Takes the data, an index field and the series in stacking order, from the bottom or left. Options split a diverging stack, shape the segments and toggle the Marimekko percentages.
- data-slot="chart-plot"
The focusable region. Arrow keys walk the categories (Up and Down for horizontal stacks), and Shift with an arrow or a drag selects a range.
- data-slot="chart-grid"
A value axis from 0, or 0 to 100 percent on the normalized variants and the Marimekko. Diverging stacks put 0 in the middle and the negative side on the left.
- data-slot="chart-marks"
One segment per series and category, stacked in order. A series switched off in the legend keeps its place and fades, so the rest of the stack doesn't jump.
- data-slot="chart-active"
On the two area variants, a ringed point on top of each stack follows the hovered category. The bar variants use the hover band instead.
- data-slot="chart-tooltip"
The category and one row per visible part with its raw value, plus a change chip for the first series. Normalized variants still show amounts here while the axis reads percent.
- data-slot="chart-message"
A bar-shaped skeleton while loading. The empty and error messages replace the stack.
A hidden table with the raw values, not percentages, so screen readers hear the same numbers the tooltip shows.
Best practices
- Do
Order the series by importance, with the part people compare most at the baseline. Only the bottom (or left) segment shares a common start, so only it can be compared exactly.
- Do
Switch to a 100 percent variant when the shares matter and the totals do not. All bars then reach 100 and segment sizes are comparable between categories.
- Do
Use the diverging variant for survey scales such as strongly disagree to strongly agree. With an odd number of series the middle one is neutral and straddles zero.
- Don’t
Don't stack more than five series. Thin segments lose their labels and hover target, and the hues get hard to match to the legend.
- Don’t
Don't stack values that aren't additive, such as averages or rates. A stack implies the parts sum to the total.
API reference
One table for StackedChart, listing every prop. The ChartCard around it has its own props, and the chart also works without one.
Takes the data, an index field and the series in stacking order, from the bottom or left. Options split a diverging stack, shape the segments and toggle the Marimekko percentages.
Notes for AI
Give your coding assistant the Markdown reference instead of screenshots.
get_component("stacked-chart")The full library index for assistants is at /llms.txt.