How a whole splits into many parts, as tiles sized by value, in groups, that you can press to drill in.
- 5.3 kB JS 5.4 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 13 tests
11 partsTotal $61,800.00
Press a service to open its costs.
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 constconst 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 serviceexport 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 ?? ""}.`), ),})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 instead when the parts aren't nested and are few.
Usage
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
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.
8 partsTotal 405 GB
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 folderexport 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 |
|---|---|---|---|
labelrequired | string | — | What the whole is: "Cloud spend, November" |
itemsrequired | 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
pnpm dlx shadcn@latest add checkfu/uicontext/treemap