# Treemap
Package: @mantine/charts
Import: import { Treemap } from '@mantine/charts';
Description: Treemap chart component

## Usage

`Treemap` is based on the [Treemap recharts component](https://recharts.github.io/en-US/api/Treemap).
It displays data as rectangles with areas proportional to their values.
This example shows which modules contribute most to a JavaScript bundle's size:

```tsx
// Demo.tsx
import { Treemap } from '@mantine/charts';
import { Stack, Text } from '@mantine/core';
import { data } from './data';

function Demo() {
  return (
    <Stack>
      <div>
        <Text fw={500}>JavaScript bundle size</Text>
        <Text size="sm" c="dimmed">Illustrative build · minified size</Text>
      </div>
      <Treemap data={data} valueFormatter={(value) => `${value} KB`} />
    </Stack>
  );
}

// data.ts
export const data = ${JSON.stringify(data, null, 2)};
```


## Nested data

Each data item can have a `children` array.
Here, modules are grouped by output chunk and inherit their chunk's color:

```tsx
// Demo.tsx
import { Treemap } from '@mantine/charts';
import { Stack, Text } from '@mantine/core';
import { data } from './data';

function Demo() {
  return (
    <Stack>
      <div>
        <Text fw={500}>Bundle size by chunk</Text>
        <Text size="sm" c="dimmed">Illustrative build · minified size</Text>
      </div>
      <Treemap data={data} withLegend valueFormatter={(value) => `${value} KB`} />
    </Stack>
  );
}

// data.ts
export const data = ${JSON.stringify(nestedData, null, 2)};
```


## Legend

Set `withLegend` to display a legend for top-level nodes.
Hover over a legend item to highlight its branch:

```tsx
// Demo.tsx
import { Treemap } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return <Treemap data={data} valueFormatter={(value) => `${value} KB`} withLegend={true} />;
}

// data.ts
export const data = ${JSON.stringify(data, null, 2)};
```


## Legend position

The legend is displayed below the chart by default.
Set `legendProps={{ position: 'top' }}` to display it above the chart:

```tsx
// Demo.tsx
import { Treemap } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return (
    <Treemap
      data={data}
      withLegend
      legendProps={{ position: 'top' }}
      valueFormatter={(value) => `${value} KB`}
    />
  );
}

// data.ts
export const data = ${JSON.stringify(data, null, 2)};
```


## Node color

`color` accepts theme colors and any valid CSS color.
Nodes without `color` get a color from a default palette:

```tsx
// Demo.tsx
import { Treemap } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return <Treemap data={data} valueFormatter={(value) => `${value} KB`} />;
}

// data.ts
export const data = ${JSON.stringify(data, null, 2)};
```


## Text color

Set `textColor` to use the same label color on all nodes:

```tsx
// Demo.tsx
import { Treemap } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return <Treemap data={data} textColor="blue.9" valueFormatter={(value) => `${value} KB`} />;
}

// data.ts
export const data = ${JSON.stringify(data, null, 2)};
```


## Value formatter

Use `valueFormatter` to format values in the tooltip:

```tsx
// Demo.tsx
import { Treemap } from '@mantine/charts';
import { Stack, Text } from '@mantine/core';
import { data } from './data';

function Demo() {
  return (
    <Stack>
      <Text fw={500}>Sales by product · October 2026</Text>
      <Treemap
        data={data}
        valueFormatter={(value) =>
          value.toLocaleString('en-US', {
            style: 'currency',
            currency: 'USD',
            maximumFractionDigits: 0,
          })
        }
      />
    </Stack>
  );
}

// data.ts
export const data = ${JSON.stringify(data, null, 2)};
```


## Data key

By default, node values are read from the `value` key. Set `dataKey` to use another key:

```tsx
import { Treemap } from '@mantine/charts';

const data = [
  { name: 'Charts', sizeKb: 240, color: 'blue.7' },
  { name: 'Editor', sizeKb: 180, color: 'blue.7' },
  { name: 'React DOM', sizeKb: 130, color: 'blue.7' },
];

function Demo() {
  return (
    <Treemap
      data={data}
      dataKey="sizeKb"
      valueFormatter={(value) => `${value} KB`}
    />
  );
}
```

## Node stroke

Use `strokeWidth` and `strokeColor` props to change the stroke around nodes:

```tsx
// Demo.tsx
import { Treemap } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return (
    <Treemap
      data={data}
      strokeColor="gray.3"
      strokeWidth={2}
      valueFormatter={(value) => `${value} KB`}
    />
  );
}

// data.ts
export const data = ${JSON.stringify(data, null, 2)};
```


## Disable tooltip

To disable the tooltip, set `withTooltip={false}`:

```tsx
// Demo.tsx
import { Treemap } from '@mantine/charts';
import { data } from './data';

function Demo() {
  return <Treemap data={data} withTooltip={false} />;
}

// data.ts
export const data = ${JSON.stringify(data, null, 2)};
```



#### Props

**Treemap props**

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| aspectRatio | number | 1.618033988749895 | The treemap will try to keep every single rectangle's aspect ratio near the aspectRatio given |
| autoContrast | boolean | theme.autoContrast | Determines whether label color should be black or white depending on the node fill to improve contrast. Ignored when `textColor` is set |
| children | React.ReactNode | - | Additional elements rendered inside `Treemap` component |
| data | TreemapData[] | required | Data used to render chart |
| dataKey | string | 'value' | Key in data object for the value |
| height | number | 300 | Controls chart height |
| legendProps | HierarchyChartLegendOptions | - | Props passed down to recharts `Legend` component, supports top and bottom positions |
| strokeColor | MantineColor | - | Controls color of the node stroke, by default depends on color scheme |
| strokeWidth | number | 1 | Controls width of node stroke |
| textColor | MantineColor | - | Controls text color of labels. If not set, labels are black or white depending on `autoContrast` |
| tooltipAnimationDuration | number | 0 | Tooltip animation duration in ms |
| tooltipProps | RechartsProps | - | Props passed down to `Tooltip` recharts component |
| treemapProps | Partial<Omit<Props<TreemapDataType, any>, "data" \| "ref" \| "dataKey">> | - | Props passed down to recharts `Treemap` component |
| valueFormatter | (value: number) => string | - | A function to format values inside the tooltip |
| withLegend | boolean | false | Determines whether the legend for top-level nodes should be displayed |
| withTooltip | boolean | true | Determines whether the tooltip should be displayed when a node is hovered |


#### Styles API

Treemap component supports Styles API. With Styles API, you can customize styles of any inner element. Follow the documentation to learn how to use CSS modules, CSS variables and inline styles to get full control over component styles.

**Treemap selectors**

| Selector | Static selector | Description |
|----------|----------------|-------------|
| root | .mantine-Treemap-root | Root element |
| legendWrapper | .mantine-Treemap-legendWrapper | Wrapper around the legend |
| legend | .mantine-Treemap-legend | Legend root element |
| legendItem | .mantine-Treemap-legendItem | Legend item representing a top-level node |
| legendItemColor | .mantine-Treemap-legendItemColor | Legend item color |
| legendItemName | .mantine-Treemap-legendItemName | Legend item name |
| tooltip | .mantine-Treemap-tooltip | Tooltip root element |
| tooltipBody | .mantine-Treemap-tooltipBody | Tooltip wrapper around all items |
| tooltipItem | .mantine-Treemap-tooltipItem | Tooltip item representing data series |
| tooltipItemBody | .mantine-Treemap-tooltipItemBody | Tooltip item wrapper around item color and name |
| tooltipItemColor | .mantine-Treemap-tooltipItemColor | Tooltip item color |
| tooltipItemName | .mantine-Treemap-tooltipItemName | Tooltip item name |
| tooltipItemData | .mantine-Treemap-tooltipItemData | Tooltip item data |
| tooltipLabel | .mantine-Treemap-tooltipLabel | Label of the tooltip |

**Treemap CSS variables**

| Selector | Variable | Description |
|----------|----------|-------------|
| root | --chart-stroke-color | Controls color of the chart stroke |
| root | --chart-height | Controls height of the chart |
