# Treemap (/ui/components/react/treemap)

A treemap divides 1 rectangle among the parts of a whole, sized by value and grouped by parent. Like every chart here it is a recipe: [Recharts](https://recharts.github.io) draws it and the [chart layer](/ui/components/react/charts) themes it. Each example below is a complete file to paste and adapt.

## Installation [#installation]

Recharts is the only extra dependency, with `react-is` at the same version as `react`:

```bash
pnpm add recharts react-is   # or: npm install recharts react-is / yarn add recharts react-is / bun add recharts react-is
```

The examples import the chart layer from `@/components/chart`. If your project doesn't have it yet, copy it from the [Charts](/ui/components/react/charts#the-chart-layer) page first.

## Example [#example]

The data is nested: each group has `children`, and the leaves carry the value. Here the theme hands out the colors, 1 per top-level group in the order of the data, which is the same fixed order as `chartColors`. `nodeGap` separates the tiles and `label` prints the names that fit. Like `Sankey`, a `Treemap` takes no `responsive`: `Chart` sizes it.

```tsx
'use client'

import { Tooltip, Treemap } from 'recharts'
import { Chart, ChartTooltip, useChartMotion } from '@/components/chart'

const data = [
  {
    name: 'Apparel',
    children: [
      { name: 'Shirts', sales: 48200 },
      { name: 'Jackets', sales: 22300 },
      { name: 'Coats', sales: 12800 },
    ],
  },
  {
    name: 'Accessories',
    children: [
      { name: 'Bags', sales: 39600 },
      { name: 'Belts', sales: 26900 },
      { name: 'Pins', sales: 9300 },
    ],
  },
  {
    name: 'Knitwear',
    children: [
      { name: 'Beanies', sales: 31400 },
      { name: 'Socks', sales: 18700 },
    ],
  },
]

const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })

export default function TreemapDefault() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart className="h-72">
        <Treemap
          data={data}
          dataKey="sales"
          nameKey="name"
          nodeGap={2}
          label
          aria-label="Sales by category and product"
          {...motion.series}
        >
          <Tooltip content={<ChartTooltip formatValue={(value) => money.format(value)} />} />
        </Treemap>
      </Chart>
    </div>
  )
}
```

## Accessibility [#accessibility]

- Pass `aria-label` to the chart with what it shows (`"Sales by category and product"`). It becomes the accessible name of the SVG.
- Keep to 5 groups, the number of series colors. The labels and the tooltip name every tile, so nothing depends on color alone.
- A tile too small for its label still has a tooltip. When readers need every value, show a [`Table`](/ui/components/react/table) beside the chart.
- Nothing animates under reduced motion.
