# Treemap Chart

> React squarified SVG treemap with grouped cells under header strips, labels that hide in small cells, a value tooltip on hover or focus and a data table.

- Kind: Component (Charts)
- Docs: https://ui.minidev.pro/docs/treemap-chart
- Source (registry JSON with the full file): https://ui.minidev.pro/r/treemap-chart.json
- License: MIT. One file, yours to edit after install.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/treemap-chart
```

Writes `components/ui/treemap-chart.tsx` and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/treemap-chart.json`.

### npm package

```bash
npm i minidev-ui-kit
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

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

From the npm package:

```tsx
import { TreemapChart } from "minidev-ui-kit/ui/treemap-chart"
```

## Usage

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

export function Example() {
  return (
    <TreemapChart />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `data` | `TreemapNode[]` | `SAMPLE` | no | Top level items. A node with `children` renders as a group with a header strip. |
| `title` | `string` | `"Revenue by product line"` | no | Heading above the chart and the accessible name. |
| `caption` | `string` | `"October, by plan"` | no | Shown next to the total. |
| `height` | `number` | `320` | no | Chart height in px. The width follows the container. |
| `format` | `(value: number) => string` | `defaultFormat` | no | Formats values in the tooltip, labels and table. Defaults to compact USD ("$48.2K"). |
| `valueLabel` | `string` | `"Revenue"` | no | Name of the measure in the tooltip, e.g. "Revenue", "Storage", "Users". |
| `minLabelSize` | `number` | `44` | no | Hide labels in cells narrower or shorter than this, in px. |
| `className` | `string` | - | no |  |

## Examples

### Flat data

Leaves without children render as one level; format rewrites every value so the chart can show gigabytes instead of money.

```tsx
const storage: TreemapNode[] = [
  { label: "Attachments", value: 48.2 },
  { label: "Invoice PDFs", value: 31.6 },
  { label: "Exports", value: 17.4 },
  { label: "Backups", value: 12.9 },
  { label: "Logos", value: 4.1 },
  { label: "Other", value: 2.3 },
]

<TreemapChart data={storage} title="Storage by type" caption="of 250 GB" height={240} format={(n) => `${n.toFixed(1)} GB`} />
```

### Group colours and counts

A color on a group tints all of its cells; here the values are customer counts, so format drops the currency.

```tsx
const customers: TreemapNode[] = [
  {
    label: "North America",
    color: "var(--chart-1)",
    children: [
      { label: "Pro", value: 412 },
      { label: "Business", value: 188 },
      { label: "Starter", value: 356 },
    ],
  },
  {
    label: "Europe",
    color: "var(--chart-2)",
    children: [
      { label: "Pro", value: 264 },
      { label: "Business", value: 97 },
      { label: "Starter", value: 231 },
    ],
  },
  {
    label: "Asia Pacific",
    color: "var(--chart-4)",
    children: [
      { label: "Pro", value: 118 },
      { label: "Starter", value: 142 },
    ],
  },
  { label: "Other", color: "var(--chart-5)", value: 76 },
]

<TreemapChart data={customers} title="Customers by region and plan" caption="active workspaces" format={(n) => n.toLocaleString("en-US")} />
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `Tab` | Moves focus into the treemap. All cells share one Tab stop. |
| `ArrowRight` or `ArrowDown` | Moves focus to the next cell and shows its tooltip. |
| `ArrowLeft` or `ArrowUp` | Moves focus to the previous cell. |
| `Home` or `End` | Moves focus to the first or last cell. |

## Accessibility

- The chart is a `<figure>` labelled by its title; the SVG is `role="group"` named with the cell count.
- Each cell is a focusable `role="img"` named with its group, item, formatted value and share of the total, in that order.
- The tooltip and in-cell text are `aria-hidden` because the cell's name carries the same numbers.
- A visually hidden `<table>` lists group, item, value and share for every cell, with a total row.

## FAQ

### Can TreemapChart nest more than one level deep?

No. A top-level item is either a leaf (`{ label, value }`) or a group with `children` leaves, and groups get a header strip when there is room. For deeper data, flatten it to two levels or let people pick a group and pass only its children.

### How are cell colours chosen?

Each top-level item takes the next chart token, `--chart-1` to `--chart-5`, unless you set `color`. Leaves inherit their group's colour at a strength set by their rank in the group, so the largest child is the strongest. A leaf's own `color` overrides that.

### Does clicking a cell drill down?

No. Cells respond to hover and focus with a tooltip; there is no click handler. Treat it as a read-only chart and put drill-down links in a table next to it.

### Why are some cells unlabelled?

Labels hide in cells narrower than `minLabelSize` (default 44px) or shorter than 26px, and the value line needs 44px of height, so text never overflows. Hover or focus the cell to see its tooltip; screen readers always get the full label.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Related

- [Area Chart](https://ui.minidev.pro/docs/area-chart/md): Dependency free SVG area chart for React with a smoothed line, dashed grid, gradient fill and an optional highlighted point. Strokes stay crisp at any size.
- [Bar Chart](https://ui.minidev.pro/docs/bar-chart/md): Lightweight React bar chart with rounded bars, dashed gridlines and labels. Hovering a bar dims the rest and shows its value; the peak is highlighted first.
- [Chart Card](https://ui.minidev.pro/docs/chart-card/md): Card wrapper for charts with a title, optional description and a minimum height body. Pairs with the area, bar and donut charts in a React dashboard.
- [Chart Legend](https://ui.minidev.pro/docs/chart-legend/md): Wrapping chart legend with small color swatches and labels. Pass colors per series or fall back to accent, success, warning and danger theme tokens.
- [Chart Tooltip Card](https://ui.minidev.pro/docs/chart-tooltip-card/md): Tooltip card for charts showing a title and label and value rows with tabular numbers. Drop it into any chart library's custom tooltip slot.
- [Donut Chart](https://ui.minidev.pro/docs/donut-chart/md): Single value donut chart in SVG that fills an accent ring to a percentage of max and prints the percent in the middle. Labelled as an image for screen readers.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
