Treemap
Treemap for the parts of a hierarchy: squarified, nested with group headers, or as an icicle.
Playground
Installation
Treemap 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 {
TreemapChart,
TreemapChartProps,
TreemapChartVariant,
} from "@wunderui/react/charts"Then use it:
import { TreemapChart } from "@wunderui/react/charts"
const data = {
name: "Revenue",
children: [
{ name: "Software", children: [{ name: "Core", value: 340 }, { name: "Pro", value: 260 }] },
{ name: "Services", children: [{ name: "Onboarding", value: 120 }, { name: "Support", value: 150 }] },
],
}
export function RevenueByLine() {
return <TreemapChart data={data} label="Revenue by product line" />
}Expected result: Four rectangles in two colours, with the Software ones the largest.
When to use
Use Treemap for
- The parts of a hierarchy by size: revenue by line and product, disk usage
- Many leaves that would be too small for a pie
- A nested view with group headers, or an icicle by level
Use something else for
- Sunburst for a hierarchy as rings, with a centre value
- Pie Chart for five parts or fewer of one whole
- Stacked Chart for parts of a total per period
- Heatmap for a value for every pair of two categories
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.
Keyboard interactions
The plot is focusable. Arrow keys step through the leaves and the live region reads the part with its path and share of the total, such as Software › Core: 340, 23.3%.
- 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 leaf. 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 leaf.
- Escape
- Clears the active point.
- EnterorSpace
- On a legend badge of a ChartCard: switches its series off or on. The last visible series stays on.
Behavior and motion
What the chart does on its own and what the page decides.
- Data
- Data is a tree: a name, a value on the leaves, children and an optional colour. The top-level children are the coloured groups, and a tree with no positive values counts as empty.
- Labels
- Cells are rounded with a small gap. Labels appear only in cells with room for them, and the others rely on the tooltip.
- Variants
- The basic treemap shows the leaves, the nested treemap adds a header strip per group, and the icicle runs the levels as rows from the root.
- Legend
- Hidden series fade whole groups by name. Helpers for the hierarchy rows and group colours are exported for building a matching legend.
- Loading, empty and error
- The chart keeps its size while it loads, is empty or fails, so the card never jumps. Loading shows a muted shimmer, and an error shows a Try again button when you add a retry handler.
- Legend
- A ChartCard shows the series as badges. With two or more series each badge switches its series off and the last visible one stays on, and a legend of roles instead of series can be made static.
- 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.
- Fade in
- Cells fade in the first time the chart is in view, and again after loading, on a key change or when new data arrives. You can turn it off.
- Reduced motion
- With reduced motion the enter sequence is off and the chart appears at once. The tooltip, crosshair and every state still work.
Anatomy
TreemapChart shows a hierarchy as nested rectangles whose area is the value: a treemap of the leaves, a nested treemap with group headers, or an icicle. The top-level children are the coloured groups.
Revenue by line
| label | Path | Value | Share |
|---|---|---|---|
| Core | Software › Core | $340k | 21.3% |
| Pro | Software › Pro | $260k | 16.3% |
| Enterprise | Software › Enterprise | $410k | 25.6% |
| Onboarding | Services › Onboarding | $120k | 7.5% |
| Training | Services › Training | $80k | 5% |
| Support | Services › Support | $150k | 9.4% |
| Devices | Hardware › Devices | $180k | 11.3% |
| Accessories | Hardware › Accessories | $60k | 3.8% |
Takes a tree of data (name, a value on the leaves, children and an optional colour). Hidden series fade whole groups, and helpers are exported for building a matching legend.
- data-slot="chart-plot"
The focusable region. Arrow keys walk its marks and the live region reads each one the way the tooltip shows it.
- data-slot="chart-marks"
Rounded cells with a small gap, fading in. Labels appear only in cells with room for them, and the others rely on the tooltip.
In the nested treemap, a header strip per group with its name. In the icicle, the levels run as rows from the root.
- data-slot="chart-tooltip"
The cell's group with its value and its share of the total.
- data-slot="chart-message"
Muted bars under the shimmer. A tree with no positive values counts as empty.
A table of the leaves with their groups and values.
Best practices
- Do
Keep to two levels. Deeper trees make cells too small to label, so collapse the long tail into one Other leaf.
- Do
Colour by top-level group only and keep the groups to five or six. Colour per cell carries no information.
- Do
Pass positive values and a clear root name. Zero and negative leaves aren't drawn.
- Don’t
Don't use a treemap to compare close values. Area is read less accurately than length, and cells of similar size can't be ranked by eye.
- Don’t
Don't rely on labels inside small cells. Put the important numbers in the card or the table.
API reference
One table for TreemapChart. It is a plain component, so the table lists every prop, and the ChartCard around it takes its own props and works without one.
Takes a tree of data (name, a value on the leaves, children and an optional colour). Hidden series fade whole groups, and helpers are exported for building a matching legend.
Notes for AI
Give your coding assistant the Markdown reference instead of screenshots.
get_component("treemap")The full library index for assistants is at /llms.txt.