# Treemap

Treemap for the parts of a hierarchy: squarified, nested with group headers, or as an icicle.

- Category: Charts
- Page: https://wunderui.com/components/treemap
- Tier: Core and Pro

## Install

Treemap ships with WunderUI Core and Pro in the @wunderui/react package.

## Import

```tsx
import { TreemapChart, TreemapChartProps, TreemapChartVariant } from "@wunderui/react"
```

## Minimal usage

Then use it:

```tsx
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

- [Sunburst](https://wunderui.com/components/sunburst) for a hierarchy as rings, with a centre value
- [Pie Chart](https://wunderui.com/components/pie-chart) for five parts or fewer of one whole
- [Stacked Chart](https://wunderui.com/components/stacked-chart) for parts of a total per period
- [Heatmap](https://wunderui.com/components/heatmap) for a value for every pair of two categories

## Keyboard

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%.

| Key | Action |
|---|---|
| `Tab` or `Shift+Tab` | Moves focus to the plot and away from it. The plot is one tab stop; so is every legend badge. |
| `ArrowRight` or `ArrowLeft` | 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). |
| `Home` or `End` | Jumps to the first or last leaf. |
| `Escape` | Clears the active point. |
| `Enter` | With zoomable: opens the group of the active cell. Escape or Backspace goes back to the whole tree. |
| `Enter` or `Space` | On a legend badge of a ChartCard: switches its series off or on. The last visible series stays on. |

## Behavior

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.

- **Settle:** Tiles settle into place from their own centre as one diagonal wave, then their labels fade in, the first time the chart is in view, after loading, after a drill-down and when new data arrives. You can turn it off.
- **Hover:** The hovered cell and the frames it sits in keep their strength; the rest dims to --chart-dim.
- **Reduced motion:** With reduced motion the enter sequence is off and the chart appears at once. The tooltip, the hover dimming and every state still work.

## Anatomy

- `TreemapChart`: Takes a tree of data (name, a value on the leaves, children and an optional colour). Groups switched off in the prop or in the legend of a surrounding ChartCard fade; palette indigo paints the groups in indigo tones by size; zoomable opens a group on click.
- `Plot`: The focusable region. Arrow keys walk its marks and the live region reads each one the way the tooltip shows it.
- `Marks`: Rounded cells with a small gap that settle into place as a diagonal wave from the top left. Labels appear only in cells with room for them, and the others rely on the tooltip. Hover keeps the cell and its group frame and dims the rest.
- `Group headers`: In the nested treemap, a header strip per group with its name. In the icicle, the levels run as rows from the root.
- `Tooltip`: The cell's group with its value and its share of the total.
- `Loading, empty and error`: Muted bars under the shimmer. A tree with no positive values counts as empty.
- `Data table and live region`: A table of the leaves with their groups and values.
- `Chart Zoom Bar`

## Best practices

### When to use

- Use it for part-to-whole across many items, such as storage by folder or spend by vendor. Use BarChart when ranking exact values matters, and SunburstChart to keep the levels visible.

### Do

- Keep to two levels. Deeper trees make cells too small to label, so collapse the long tail into one Other leaf.
- Colour by top-level group only and keep the groups to five or six. Colour per cell carries no information.
- 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 rely on labels inside small cells. Put the important numbers in the card or the table.

## API reference

### TreemapChart

| Prop | Type | Default | Description |
|---|---|---|---|
| data* | HierarchyDatum |  | Root of the hierarchy; its children are the coloured groups. |
| activeIndex | number \| null |  | The active point (hovered or focused) as an index into `data`, to control it yourself. `null` shows none. |
| animate | boolean |  |  |
| animationKey | string \| number |  |  |
| className | string |  | Extra classes for the root element of the chart. |
| defaultActiveIndex | number \| null |  | The point that is active at first, for an uncontrolled chart. It shows the tooltip without a pointer. |
| description | string |  |  |
| height | number |  |  |
| hiddenSeries | readonly string[] |  | Group names switched off in the legend: their cells fade. |
| label | string |  |  |
| onActiveIndexChange | (index: number \| null) => void |  | Called with the index when the pointer or the arrow keys move to another point, and with `null` when they leave. Use it to show the hovered value in a headline. |
| onRetry | () => void |  | Shows a Try again button in the error state and calls this when it is pressed. |
| onZoomChange | (group: string) => void |  | Called with the group in focus, or an empty string for the whole tree. |
| palette | "groups" \| "indigo" | "groups" | groups paints each top-level group in the palette order (or its own colour); indigo paints the groups in indigo tones by size, 600 for the largest. |
| pattern | ChartPattern \| ChartPattern[] |  | One pattern form from the shared pattern library (`diagonal-lines`, `polka-dots`, `crosshatch` …) for every cell (in its group colour; labels on a pattern take the text colour), drawn in its colour on a tint of it, for print and colour-blind reading. A list keeps its first entry; a mark under 20 px on its shorter side stays solid. |
| state | "ready" \| "loading" \| "empty" \| "error" |  |  |
| valueFormatter | ChartValueFormatter |  |  |
| variant | "basic-treemap" \| "nested-treemap" \| "icicle" | "basic-treemap" | Figma variant. Default `basic-treemap`. |
| width | number |  |  |
| zoomable | boolean | false | Basic and nested treemap: a click on a cell (or Enter on it) opens its group so its parts fill the frame. The bar above them, Escape or Backspace goes back. |

## Examples

### States

### Drill-down

### Indigo tones

### Patterns

## In context

### Revenue by Line

Nested treemap of revenue with the top lines.

## For AI agents

`get_component("treemap")`

Hand your agent this page as Markdown or the WunderUI MCP server, not a screenshot.
