# Treemap chart

Sizes as area, grouped: spend by team and feature, storage by project. Rounded, gapped tiles saturate when pointed at, and a group grows to fill the chart when opened.

Also called: hierarchy chart.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Treemap chart. March AI spend by team and feature. Click Engineering to look inside. Preview: https://chatcn.dev/preview/treemap-chart-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/treemap-chart
```

## Usage

```tsx
import { TreemapChart } from "@/components/ui/chatcn/pro/treemap-chart"

<TreemapChart
  title="Where AI spend went in March"
  data={[
    { id: "eng", label: "Engineering", children: [{ id: "agent", label: "Coding agent", value: 9840 }, …] },
    { id: "support", label: "Support", children: […] },
  ]}
  valueFormat="currency"
/>

// Leaves with previous, tinted green or red by how far they moved
<TreemapChart data={withPrevious} colorBy="change" increase="bad" valueFormat="currency" />
```

A squarified layout keeps tiles close to square so areas compare honestly. Tiles are rounded with a 2 px gap, and groups sit twice as far apart under their name and total. Each group takes a palette color and its items are calm tints of it; point at a tile and it saturates, its group is outlined and everything else steps back, with a tooltip card of its amount, its share of the group and of the total. Labels appear only where they fit: the amount drops before the name shortens, and a number is never cut. The headline is the total in view. Click a group, or press Enter, to look inside: it grows from where it sat to fill the chart and the labels fade in once it settles; the breadcrumb, Escape or Backspace zoom back out the same way. With `previous` on the leaves, `colorBy="change"` tints tiles green or red by how far they moved and says the change in each tile, the headline, the tooltip and the table; `increase="bad"` flips the tones for spend. One level of data works too.

## Examples

### States

Example: States. One level of storage by kind, and loading. Preview: https://chatcn.dev/preview/treemap-chart-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `data` | `TreemapItem[]` | `{ id, label, value?, children? }`; groups sum their children |
| `title`, `description` | `ReactNode` | The caption |
| `headline` | `ReactNode` | Default: the total in view. `null` hides it |
| `colorBy` | `"group" | "change"` | Default `group`. `change` tints by the move since `previous` on each leaf |
| `increase` | `"good" | "bad"` | Whether growth reads green (default) or red |
| `unit`, `valueFormat` |  | How amounts read |
| `height` | `number` | Default 320 |
| `loading`, `error`, `onRetry` |  |  |
| `labels` | `Partial<TreemapChartLabels>` | Every string, for translation |

## Keyboard

| Key | Action |
| --- | --- |
| Tab | Focus the chart; the first tile is read out |
| Arrow keys | Move to the nearest tile in that direction |
| Enter | Look inside the tile's group |
| Escape, Backspace | Back to all |

## Accessibility

- The chart is an image summarized with the total and the largest tile, with a table of every item.
- The breadcrumb is a real button.
- The readout says the amount and share of whatever is pointed at or focused.

## Motion

Tiles fade in once over 560 ms. Looking inside a group grows it from its place over 420 ms with the kit's ease out, by transform, and zooming out shrinks the overview back the same way; labels fade in as it settles. Pointing eases tile colors over 200 ms. Under reduced motion all of it is instant.

Source: https://chatcn.dev/docs/components/treemap-chart