# Treemap

> A composable treemap for showing how a total divides into categories and their parts.

Source: https://www.honestui.com/docs/charts/treemap/static

### Basic Treemap

```tsx
"use client";

import { Treemap, type ChartConfig, type TreemapNode } from "honestui/charts";

// Scenario: Cloud spend by service
const data: TreemapNode[] = [
  {
    name: "compute",
    children: [
      { name: "Web servers", value: 18400 },
      { name: "Background jobs", value: 9200 },
      { name: "Build runners", value: 6100 },
      { name: "Preview envs", value: 3300 },
    ],
  },
  {
    name: "storage",
    children: [
      { name: "Object storage", value: 11200 },
      { name: "Backups", value: 4800 },
      { name: "Logs", value: 3900 },
    ],
  },
  {
    name: "database",
    children: [
      { name: "Primary", value: 12600 },
      { name: "Read replicas", value: 5400 },
      { name: "Analytics", value: 4100 },
    ],
  },
  {
    name: "network",
    children: [
      { name: "Egress", value: 6900 },
      { name: "CDN", value: 3600 },
    ],
  },
];

const chartConfig = {
  compute: { label: "Compute", colors: { light: ["#2563eb"], dark: ["#3b82f6"] } },
  storage: { label: "Storage", colors: { light: ["#059669"], dark: ["#10b981"] } },
  database: { label: "Database", colors: { light: ["#d97706"], dark: ["#f59e0b"] } },
  network: { label: "Network", colors: { light: ["#7c3aed"], dark: ["#8b5cf6"] } },
} satisfies ChartConfig;

const usd = (value: number) => `$${value.toLocaleString()}`;

export function ExampleTreemap() {
  return (
    <Treemap data={data} config={chartConfig} className="h-full w-full p-4">
      <Treemap.Legend />
      <Treemap.Tooltip valueFormatter={usd} />
      <Treemap.Tiles valueFormatter={usd} />
    </Treemap>
  );
}

```

## Overview [#overview]

Use a treemap to show which parts of a large total matter most: spend by service, bundle size by package, or tickets by topic. Each tile's area is proportional to its value, and nested tiles show how a category breaks down. A treemap holds far more items than a pie chart, but area is harder to compare precisely than length, so reach for a bar chart when exact ranking is the question.

## Anatomy [#anatomy]

`Treemap` owns the hierarchical data, colors, and loading state. Each top-level node is a category with its own `config` entry; its children take lighter steps of the same color, ordered by size, so a family of tiles reads as one group. Add `Tiles` to control radius, gaps, value labels, and drill-down, `Legend` to name the categories, and `Tooltip` for each tile's value and its share of its parent.

## Accessibility [#accessibility]

When `ariaLabel` is omitted, the generated name gives the total and the largest categories with their values. Labels pick near-black or near-white text from each tile's fill, so they stay readable on light and dark tints. Small tiles hide their labels instead of overflowing; pair the chart with a table when every value must be readable. Drill-down and the tooltip are pointer-operated.

## Installation [#installation]

<CommandBlock commands="[&#x22;honestui&#x22;]" />

## Usage [#usage]

The data is an array of nodes. A leaf carries a `value`; a parent's value is the sum of its `children`.

```tsx
import { Treemap, type ChartConfig, type TreemapNode } from "honestui/charts";
```

```tsx
<Treemap data={data} config={chartConfig}>
  <Treemap.Legend />
  <Treemap.Tooltip />
  <Treemap.Tiles />
</Treemap>
```

Parent tiles get a tinted frame with a header strip naming the group. Turn it off with `showParentLabels={false}` to draw only the leaves.

### Drill Down [#drill-down]

### isDrillable

```tsx
"use client";

import { Treemap, type ChartConfig, type TreemapNode } from "honestui/charts";

// Scenario: Bundle size by package
const data: TreemapNode[] = [
  {
    name: "app",
    children: [
      { name: "routes", children: [{ name: "dashboard", value: 148 }, { name: "settings", value: 64 }, { name: "billing", value: 52 }] },
      { name: "components", children: [{ name: "charts", value: 96 }, { name: "forms", value: 58 }, { name: "tables", value: 41 }] },
      { name: "lib", value: 38 },
    ],
  },
  {
    name: "vendor",
    children: [
      { name: "react-dom", value: 132 },
      { name: "echarts", value: 318 },
      { name: "date-fns", value: 44 },
      { name: "zod", value: 57 },
    ],
  },
  {
    name: "assets",
    children: [
      { name: "fonts", value: 86 },
      { name: "icons", value: 34 },
    ],
  },
];

const chartConfig = {
  app: { label: "App code", colors: { light: ["#0891b2"], dark: ["#22d3ee"] } },
  vendor: { label: "Dependencies", colors: { light: ["#db2777"], dark: ["#f472b6"] } },
  assets: { label: "Assets", colors: { light: ["#65a30d"], dark: ["#a3e635"] } },
} satisfies ChartConfig;

const kb = (value: number) => `${value.toLocaleString()} kB`;

export function ExampleTreemap() {
  return (
    <Treemap data={data} config={chartConfig} className="h-full w-full p-4">
      <Treemap.Tooltip valueFormatter={kb} />
      <Treemap.Tiles isDrillable valueFormatter={kb} />
    </Treemap>
  );
}

```

With `isDrillable`, the treemap shows one level at a time. Selecting a tile zooms into it, and a breadcrumb under the chart leads back out. Use it for deep trees, where showing every level at once leaves tiles too small to label.

### Flat Data [#flat-data]

### Flat categories

```tsx
"use client";

import { Treemap, type ChartConfig, type TreemapNode } from "honestui/charts";

// Scenario: Support tickets by topic
const data: TreemapNode[] = [
  { name: "billing", value: 412 },
  { name: "login", value: 287 },
  { name: "exports", value: 164 },
  { name: "integrations", value: 133 },
  { name: "mobile", value: 92 },
  { name: "other", value: 58 },
];

const chartConfig = {
  billing: { label: "Billing", colors: { light: ["#1d4ed8"], dark: ["#60a5fa"] } },
  login: { label: "Login", colors: { light: ["#2563eb"], dark: ["#3b82f6"] } },
  exports: { label: "Exports", colors: { light: ["#3b82f6"], dark: ["#2563eb"] } },
  integrations: { label: "Integrations", colors: { light: ["#60a5fa"], dark: ["#1d4ed8"] } },
  mobile: { label: "Mobile", colors: { light: ["#93c5fd"], dark: ["#1e40af"] } },
  other: { label: "Other", colors: { light: ["#cbd5e1"], dark: ["#334155"] } },
} satisfies ChartConfig;

export function ExampleTreemap() {
  return (
    <Treemap data={data} config={chartConfig} className="h-full w-full p-4">
      <Treemap.Tooltip valueFormatter={(value) => `${value} tickets`} />
      <Treemap.Tiles radius={4} gap={3} />
    </Treemap>
  );
}

```

Nodes without children make a flat treemap: one tile per category. This example uses a single-hue ramp in the config, a larger radius, and wider gaps.

### Loading State [#loading-state]

### isLoading='true'

```tsx
"use client";

import { Treemap, type ChartConfig } from "honestui/charts";

const chartConfig = {
  compute: { label: "Compute", colors: { light: ["#2563eb"], dark: ["#3b82f6"] } },
} satisfies ChartConfig;

export function LoadingTreemap() {
  return (
    <Treemap data={[]} config={chartConfig} isLoading className="h-full w-full p-4">
      <Treemap.Tooltip />
      <Treemap.Tiles />
    </Treemap>
  );
}

```

Pass `isLoading` to reserve the layout with neutral tiles and the same loading treatment as the other Honest UI charts.

## API Reference [#api-reference]

<ApiHeading>
  Treemap
</ApiHeading>


  ### `data`

type: `TreemapNode[]`

Top-level nodes. Each has a `name` and either a `value` or `children`.

  ### `config`

type: `ChartConfig`

Labels and theme colors keyed by top-level node name.

  ### `className`

type: `string`

Additional CSS classes for the chart container. Give the chart an explicit height.

  ### `animation`

type: `boolean` · default: `true`

Enables the intro and zoom animation. Reduced-motion preferences disable it automatically.

  ### `isLoading`

type: `boolean` · default: `false`

Replaces the data with neutral placeholder tiles and a progress label.

  ### `ariaLabel`

type: `string`

Accessible name for the canvas chart. A summary of the largest categories is generated when omitted.

  ### `onTileClick`



void">
    Fires when a tile is selected and `Tiles` has `isClickable`. The tile carries its value, path, and category.

  ### `chartOptions`



">
    Escape hatch merged over the generated ECharts option object.


<ApiHeading>
  Tiles
</ApiHeading>


  ### `radius`

type: `number` · default: `2`

Tile corner radius in pixels.

  ### `gap`

type: `number` · default: `2`

Space between tiles in pixels.

  ### `showValues`

type: `boolean` · default: `true`

Shows each tile's value under its name.

  ### `valueFormatter`



string">
    Formats values in tile labels.

  ### `isDrillable`

type: `boolean` · default: `false`

Shows one level at a time and zooms into a tile when it is selected, with a breadcrumb back out.

  ### `showParentLabels`

type: `boolean` · default: `true`

Frames parent tiles with a header strip naming the group.

  ### `rootLabel`

type: `string` · default: `&#x22;All&#x22;`

The breadcrumb's first item, which returns to the top level.

  ### `isClickable`

type: `boolean` · default: `false`

Enables the pointer cursor and `onTileClick`.


<ApiHeading>
  Legend
</ApiHeading>


  ### `variant`

type: `LegendVariant` · default: `&#x22;rounded-square&#x22;`

Indicator shape, shared with the other Honest UI charts.

  ### `align`

type: `&#x22;left&#x22; | &#x22;center&#x22; | &#x22;right&#x22;` · default: `&#x22;center&#x22;`

Horizontal alignment of the legend.

  ### `verticalAlign`

type: `&#x22;top&#x22; | &#x22;bottom&#x22;` · default: `&#x22;bottom&#x22;`

Places the legend above or below the tiles.


<ApiHeading>
  Tooltip
</ApiHeading>


  ### `variant`

type: `&#x22;default&#x22; | &#x22;frosted-glass&#x22;` · default: `&#x22;default&#x22;`

Tooltip surface treatment.

  ### `roundness`

type: `&#x22;sm&#x22; | &#x22;md&#x22; | &#x22;lg&#x22; | &#x22;xl&#x22;` · default: `&#x22;lg&#x22;`

Tooltip corner radius.

  ### `position`

type: `&#x22;fixed&#x22; | &#x22;variable&#x22;` · default: `&#x22;variable&#x22;`

Pins the tooltip to the top or lets it follow the pointer.

  ### `valueFormatter`



string">
    Formats the value shown in the tooltip.
