2 Treemap
L4stIdi0t edited this page 2026-11-17 10:55:14 +01:00

Treemap

Sizes of things inside things, as boxes whose area is their value: a disk's folders, a budget's lines, a bundle's modules.

A node with children is a box with its name on a strip and its children inside, as deep as depth and the room allow; what is too small is drawn but not named. Each top-level box takes the next chart colour and its children take it too, more faintly the deeper they lie, so a branch reads as one patch. A node can bring its own colour — by kind, say — and then its children inherit that one. The colours tell neighbours apart; they are not a key, so name them in a legend of your own when they mean something. Hover a box, or focus one, for a tooltip with its value and share. A box with href is a link; with onSelect, a box holding children is a button and every other box answers a click. Those are the tab stops — small boxes are reached through the box they lie in. The top level is also a visually-hidden table, inside a clipped box so it never widens a phone page.

Charts · keyboard contract

<script lang="ts">
	import { Treemap } from '@l4st/driftwood';
</script>
Light Dark
treemap treemap in dark mode

Props

Prop Type Default What it does
data required TreemapNode[] — The top-level nodes, largest first reads best. Values must be non-negative.
height number — Height in pixels. Defaults to half the width, between 16rem and 30rem.
depth number 3 How many levels are drawn: 1 draws only the top level.
format (value: number) => string compact Formats every value: tooltip, labels and table.
onSelect (node: TreemapNode, trail: TreemapNode[]) => void — Called when a box without href is clicked, or a box holding children is activated, with the nodes it lies in.
onActive (node: TreemapNode | null, trail: TreemapNode[]) => void — Called with the box under the pointer or the focus, and null when there is none.
boxAttributes (node: TreemapNode, trail: TreemapNode[]) => Record<string, string | undefined> — Extra attributes for a box, such as a data- attribute a context menu reads.
partsLabel string 'Part' Heads the part column of the screen-reader table — what the boxes are ('Folder', 'Module').
valueLabel string 'Value' Heads the value column of the screen-reader table.
shareLabel string 'Share' Heads the share column of the screen-reader table.
aria-label required string — Names the map, and captions the table. Required.
class string — Merged with the component's own classes, last — so yours win.
classes Classes<TreemapPart> — Classes for the parts inside: body (the map's box), cell (every box, at any depth; the hovered or focused one carries data-active), label (a box's caption strip), title (its name), value (its number), tooltip, tooltipTitle (the swatch-and-name row), swatch, tooltipLabel (the name), tooltipValue (value and share), tooltipDetail. Merged last, so they win.

Keyboard

Key Does
Tab reach a box with href, or with onSelect a box holding children
Enter / Space follow the link, or call onSelect
focus the tooltip and the live readout name the box, its value and share

Example

The markup behind the screenshot above, lifted from src/routes/shots/ — so it compiles, and it is exactly what produced the image.

<Treemap
	data={[
		{
			label: 'Media',
			value: 5240,
			children: [
				{ label: 'Video', value: 3100 },
				{ label: 'Photos', value: 1500 },
				{ label: 'Music', value: 640 }
			]
		},
		{
			label: 'Backups',
			value: 3180,
			children: [
				{ label: 'Laptop', value: 2100 },
				{ label: 'Phone', value: 780 },
				{ label: 'Other', value: 300, muted: true }
			]
		},
		{ label: 'Code', value: 980, children: [{ label: 'Repositories', value: 700 }, { label: 'Builds', value: 280 }] },
		{ label: 'Archives', value: 620 }
	]}
	height={288}
	format={(v) => `${(v / 1000).toFixed(1)} TB`}
	partsLabel="Category"
	aria-label="Stored, by category and folder"
/>

Charts · LineChart · BarChart · DonutChart · ShareBar · Heatmap · Gauge · Sparkline · ChartLegend


← All components · Getting started · Theming · source