# Treemap

> How a whole splits into many parts, as tiles sized by value, in groups, that you can press to drill in.

Page: https://uicontext.com/docs/components/treemap

Group: Charts.

## When to use

How a whole splits into many parts, as tiles sized by value, in groups, that you can press to drill in. Use [Bar Chart](https://uicontext.com/docs/components/bar-chart.md) instead when the parts aren't nested and are few.

## Usage

```ts
import { Treemap } from "./ui/uicontext/components/charts/Treemap.ts"

export const Revenue = <M>(h: Html.H<M>) =>
	Treemap(h, { label: "Revenue by product", items: [{ id: "core", label: "Core", value: 820 }, { id: "ai", label: "AI add-on", value: 210 }, { id: "api", label: "API", value: 95 }] })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Cloud spend by service

The example the page opens with: Treemap in “Cloud spend by service”.

```ts title="src/demo.ts" source="www/src/examples/treemap/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Treemap } from "./ui/uicontext/components/charts/Treemap.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	wrap: { display: "grid", gap: space.sm },
	picked: { margin: 0, fontSize: font.sm, color: color.muted },
})

const usd = { style: "currency", currency: "USD" } as const

const groups = [
	{ id: "compute", label: "Compute" },
	{ id: "data", label: "Data" },
	{ id: "network", label: "Network" },
	{ id: "other", label: "Other" },
]

const items = [
	{ id: "ec2", label: "Instances", value: 18_420, group: "compute" },
	{ id: "lambda", label: "Functions", value: 3_910, group: "compute" },
	{ id: "k8s", label: "Kubernetes", value: 7_260, group: "compute" },
	{ id: "rds", label: "Postgres", value: 9_840, group: "data" },
	{ id: "s3", label: "Object storage", value: 4_120, group: "data" },
	{ id: "redis", label: "Redis", value: 1_980, group: "data" },
	{ id: "warehouse", label: "Warehouse", value: 6_350, group: "data" },
	{ id: "cdn", label: "CDN", value: 2_870, group: "network" },
	{ id: "egress", label: "Egress", value: 3_440, group: "network" },
	{ id: "lb", label: "Load balancers", value: 1_150, group: "network" },
	{ id: "logs", label: "Logs and secrets", value: 2_460, group: "other" },
]

const Message = UiSchema.TaggedEnum({ Picked: { id: Schema.String } })

// a month's cloud bill by service, grouped; press a tile to open that service
export const CloudSpend = Html.component({
	name: "CloudSpend",
	model: Schema.Struct({ picked: Schema.NullOr(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ picked: null }),
	update: { Picked: ($, { id }) => $.evolve({ picked: id }) },
	view: (h, model) =>
		h.div.attrs(sx(styles.wrap)).children(
			Treemap(h, { label: "Cloud spend, November", items, groups, format: usd, onSelect: (id) => Message.Picked({ id }) }),
			h.p.attrs({ role: "status", ...sx(styles.picked) }).children(model.picked === null ? "Press a service to open its costs." : `Opened ${items.find((i) => i.id === model.picked)?.label ?? ""}.`),
		),
})
```

### Without groups

Each part takes the next chart color. Tiles too small for a name still say it on hover and to a screen reader.

```ts title="src/disk.ts" source="www/src/examples/treemap/disk.ts"
import type { Html } from "effect/ui/html"
import { Treemap } from "./ui/uicontext/components/charts/Treemap.ts"

const gb = { style: "unit", unit: "gigabyte" } as const

// what fills a laptop's disk, one color per folder
export const DiskUsage = <M>(h: Html.H<M>) =>
	Treemap(h, {
		label: "Disk usage",
		height: 260,
		format: gb,
		items: [
			{ id: "photos", label: "Photos", value: 142 },
			{ id: "xcode", label: "Developer", value: 88 },
			{ id: "mail", label: "Mail", value: 21 },
			{ id: "music", label: "Music", value: 36 },
			{ id: "movies", label: "Movies", value: 64 },
			{ id: "downloads", label: "Downloads", value: 17 },
			{ id: "documents", label: "Documents", value: 9 },
			{ id: "system", label: "System", value: 28 },
		],
	})
```

## API reference

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` (required) | `string` | — | What the whole is: "Cloud spend, November" |
| `items` (required) | `TreemapItem[]` | — | — |
| `groups` | `TreemapGroup[]` | — | The groups items belong to, in legend order; without them each item takes the next color |
| `format` | `ChartFormat` | — | — |
| `height` | `number` | `320` | Pixels; 320 by default |
| `ratio` | `number` | `1.6` | How wide the box is for its height, for the layout; 1.6 by default. The tiles stretch to the real box. |
| `onSelect` | `(id: string) => Message` | — | Makes each tile a button |
| `locale` | `string` | — | — |
| `style` | `Style` | — | — |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/treemap
```

With npm: `npx shadcn@latest add checkfu/uicontext/treemap`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/treemap`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/treemap`.

It writes these files:

- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/charts/Data.ts`
- `src/ui/uicontext/components/charts/Format.ts`
- `src/ui/uicontext/components/charts/Frame.ts`
- `src/ui/uicontext/components/charts/Scale.ts`
- `src/ui/uicontext/components/charts/Treemap.ts`
- `src/ui/uicontext/components/charts/chart.stylex.ts`
- `src/ui/uicontext/components/charts/slots.ts`
- `src/ui/uicontext/components/domain/Format.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/charts/Treemap.ts](https://github.com/checkfu/uicontext/blob/main/src/charts/Treemap.ts)

## Related

- [Chart Foundations](https://uicontext.com/docs/components/chart-foundations.md) (`chart-foundations`): The scales, formats, legend and tooltip every chart shares.
- [Line Chart](https://uicontext.com/docs/components/line-chart.md) (`line-chart`): Values over time for one or many series, with dashed comparisons and forecasts.
- [Area Chart](https://uicontext.com/docs/components/area-chart.md) (`area-chart`): Totals over time and what makes them up, stacked or as shares of 100%.
- [Bar Chart](https://uicontext.com/docs/components/bar-chart.md) (`bar-chart`): Compare categories with grouped, stacked or sideways bars.
- [Combo Chart](https://uicontext.com/docs/components/combo-chart.md) (`combo-chart`): Bars and a line on one time axis, each read against its own scale.
- [Donut Chart](https://uicontext.com/docs/components/donut-chart.md) (`donut-chart`): Shares of one total, with the total in the middle.
