Skip to content

Treemap for React

A React treemap shows part-to-whole as nested rectangles, laid out by the squarified algorithm so tiles stay easy to compare. Pass data as readonly TreemapNode[]: nodes: leaves carry a value; groups carry children (any depth).

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/treemap.json

Cloud spend

Use the arrow keys to move between tiles. Press Enter to zoom into a group and Escape to zoom out.

  • Treemap
  • TreemapTable
  • squarify
  • treemapLegend
  • defaultTreemapLabels
  • +4 more
  • +1 more

Cloud spend, zoomable

Each team keeps one palette slot and its services share the hue. Click a team (or focus the chart, move with the arrow keys and press Enter) to zoom in; the breadcrumb or Escape zooms back out. Switching the quarter re-flows the tiles.

Cloud spend

Q4 2026, by team and service

$897K
  • Engineering
  • Data
  • Product
  • Security
Use the arrow keys to move between tiles. Press Enter to zoom into a group and Escape to zoom out.

Files to copy

12 folders · 27 files · 4 packages
From theme/src/
  • components/charts/{treemap.tsx, chart-export-menu.tsx, chart-export.ts, chart-figure.tsx, chart-fit.tsx, chart-icons.tsx, chart-parts.tsx, chart-patterns.tsx, chart-tip.tsx, chart-utils.ts, heatmap.tsx}
  • components/charts/chart-parts/
  • components/charts/heatmap/
  • components/drawer/parts/sheet.tsx
  • components/kbd/
  • components/menu/
  • components/skeleton/
  • components/status-icons/
  • hooks/{use-controllable.ts, use-intersection.ts, use-latest.ts, use-media-query.ts, use-preferences.ts}
  • motion/tokens.ts
  • lib/{cn.ts, csv.ts, form.ts, glide-highlight.tsx, heading.ts, math.ts, popup-a11y.ts, popup-motion.ts, warn-once.ts}
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/treemap.jsonRegistry item: treemap. The styles need the Tailwind v4 glass UI setup.
The examples also use
  • components/spinner/
  • components/toast/
  • hooks/use-direction.ts
  • lib/state-light.tsx
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/toast.json
Packages
pnpm add @base-ui/react clsx motion tailwind-merge

Next.js: samples here pass functions (formatters, seriesLegend, a dashboard's load), so put them in a file that starts with "use client".

Flat data, one hue

Without groups every tile is the same series, so it takes one hue and needs no legend; size alone carries the value. Tiny tiles stay unlabelled rather than overflow, and the tooltip and table still read them. Click a tile to handle it.

Bundle size

Gzipped kB by package

Use the arrow keys to move between tiles.

Treemap

PropTypeDefaultDescription
data (required)readonly TreemapNode[]NoneNodes: leaves carry a value; groups carry children (any depth).
heightnumber320Height of the treemap in px; the width follows the container.
colorChartColor1Hue for tiles that don't get a group colour (flat data: one series, one hue). Hierarchies colour each top-level group by its own slot instead.
patternsbooleantrue when there are more than 3 top-level groupsTell groups apart without colour (hatch fills). Default: on when the chart has more than 3 groups. A flat treemap is one hue, so it has no groups to tell apart.
nestedbooleantrueShow each group's children inside it, under a header.
drillDownbooleantrue when there are groupsClick / Enter on a group zooms into it, with a breadcrumb to zoom back out.
pathreadonly string[]NoneThe zoomed-in path (group labels from the top), controlled.
defaultPathreadonly string[][]Path of the node the chart starts zoomed into (uncontrolled).
onPathChange((path: string[]) => void)NoneCalled with the path of labels when the user zooms in or out (controlled path).
onNodeClick((node: { label: string; value: number; path: string[]; }) => void)NoneCalled on click / Enter on a tile that has nothing to zoom into.
format((n: number) => string)formatCompact in localeFormats a value for the labels, tooltips and data table: takes the number, returns the text shown.
formatPercent((ratio: number) => string)percentages in localeFormats a share of the total for the labels, tooltips and data table: takes the share, returns the text shown.
localestring"en"Locale of the default number formats.
emptyStateReactNodeNoneShown when there is nothing to plot.
labelsPartial<TreemapLabels>defaultTreemapLabelsEvery other string, for translation: labels={{ root: "…" }}; omitted keys keep the English of defaultTreemapLabels. All 10 keys of TreemapLabels are listed in the key table under the props table.
aria-label (required)stringNoneNames the chart for assistive tech (required).
classNamestringNoneAdds extra classes to the outer element.
TreemapLabels keys and English defaults (10)
Pass any key in labels; the rest keep the defaults of defaultTreemapLabels.
KeyEnglish default
rootAll
breadcrumbTreemap level
ofParent(parent) => `of ${parent}`
ofTotalof total
instructionsUse the arrow keys to move between tiles.
drillInstructionsPress Enter to zoom into a group and Escape to zoom out.
zoomedTo(label, count) => `${label}: ${count} ${count === 1 ? "item" : "items"}`
roleDescriptiontreemap
hintTap or hover a tile for its value
drillHintTap a tile for its value, again to zoom in

TreemapTable props, treemapLegend and squarify

PropTypeDefaultDescription
data (required)readonly TreemapNode[]NoneNodes: leaves carry a value; groups carry children (any depth).
format((n: number) => string)formatCompact in localeFormats a value for the labels, tooltips and data table: takes the number, returns the text shown.
localestring"en"Locale of the default number formats.
formatPercent((ratio: number) => string)percentages in localeFormats a share of the total for the labels, tooltips and data table: takes the share, returns the text shown.
captionstringNoneHidden caption that names the table for screen readers.
headersPartial<{ name: string; value: string; ofParent: string; ofTotal: string; }>NoneOverride the column headers: { name, value, ofParent, ofTotal }.
labelsPartial<TreemapTableLabels>defaultTreemapTableLabelsStrings for the cells that have no value. Merged over defaultTreemapTableLabels. Every other string, for translation: labels={{ notApplicable: "…" }}; omitted keys keep the English of defaultTreemapTableLabels. Its only key is listed in the key table under the props table.
emptyStateReactNodeNoneShown when there are no nodes.
treemapLegend(data)LegendItem[]NoneLegend items for the top-level groups, for ChartCard's legend.
squarify(values, rect)TreemapRect[]NoneThe layout on its own: one rect per value, in input order.
TreemapTableLabels keys and English defaults (1)
Pass any key in labels; the rest keep the defaults of defaultTreemapTableLabels.
KeyEnglish default
notApplicableNot applicable

Keyboard

KeysAction
TabFocus the chart. It is one tab stop; the arrow keys then read it.
Arrow keysMove spatially between tiles.
Home/EndLargest / smallest tile.
EnterZoom into the group (with drillDown).
Escape/BackspaceZoom back out.

Accessibility

  • The chart is a focusable figure named by its aria-label and described by the reading hint (and the drill-down hint when it is on); zooming is announced, and the breadcrumb is real buttons.
  • TreemapTable is the table view: pair it with ChartCard's table prop (Chart / Table tabs) so every value can be read without color or sight. It lists every node.
  • Reduced motion: the tiles appear at their final sizes and data changes jump instead of morphing.

All exports on this page

  • Treemap — Part-to-whole as nested rectangles, laid out with the squarified algorithm so tiles stay close to square (and comparable).
  • TreemapTable — Every node, depth first (groups in bold, children indented), with its value and shares.
  • defaultTreemapLabels — The built-in English strings of Treemap; spread it into your own labels and change what you need.
  • defaultTreemapTableLabels — English defaults of TreemapTableLabels.
  • squarify — Lays values out inside rect as rectangles whose areas are proportional to the values.
  • treemapLegend — Legend items for a hierarchical treemap's top-level groups (pass to ChartCard's legend).

When to use Treemap

  • Use it as a treemap chart in React for cloud spend by team and service, where each team keeps one hue, services share it, and tile area shows where the money goes.
  • It fits a portfolio or inventory with a natural hierarchy, such as product categories and SKUs, where clicking a group zooms in and the breadcrumb or Escape zooms back.
  • Flat data also works: without groups every tile uses one hue and needs no legend, so size alone carries the value.
  • Skip it when exact comparison or time trends matter, since area is hard to judge; use BarChart for comparisons, and SunburstChart when the path through the hierarchy matters more than area.
  • Pair it with ChartCard for the title, treemapLegend for the top-level group legend, and TreemapTable for every node as text with percent of group and total.

Frequently asked questions

48 charts pages in all: browse them on Charts.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .