Treemap

Parts of a whole as rectangles whose areas are their values: storage by folder, spend by team. One level of nesting at most (a group and its parts). Suits a dozen to a few dozen tiles where the big ones matter and the small ones only need to be findable.

Anatomy

← x-ds-chart only when `interactive` legend: the groups and their totals (nested), or Less ▢▢▢▢ More (colourBy="value") Part, Group, Value, Share; the parts not drawn; Total
PartTokenWhy
Tile, colourBy="series"series-1 … series-8 by the top-level part's position as given (a group's children share its colour); a row's tone or series winsspecs/chart-series.md; position, not size, so a part keeps its colour when another drops out
Tile, colourBy="value"the tone hue (accent by default) in the heat steps 1–4, by value quartile, as fill-heat-Nthe activity heatmap's ramp, one hue
Gapsa 2px surface stroke on every tiletiles touch; the gap is the page showing through
Labeltext-meta, the name font-medium, the value or share below; ink from Ink::label()see Text on a tile
Heightsm 192, md 256, lg 320px
Focusdrawn inside the tile as strokes: from the edge in, the 1px gap, 2px of focus-ring, 2px of surfacea neighbour touches every side and the plot clips the edge; a CSS outline on an svg group is placed by its stroke box, so it lands on the neighbour

Layout

Squarified (Bruls, Huizing and van Wijk, 2000) in Support\ChartShapes::squarify(), a sibling of Support\Chart because that class is already past 1,300 lines: values sorted largest first (equal values keep their given order, so the same data always lays out the same way), rows added to the shorter side while they make the worst aspect ratio in the row no worse, and the last tile and the last row run exactly to the edge, so the box is filled to the pixel. Unit tested (tests/ChartShapesTest.php): the paper's own example, areas proportional to values, no overlaps, the box filled, equal values stable, dropped values.

The layout is computed for a box ratio times as wide as height, then drawn with x in percent and y in px, no viewBox: text keeps its size and the treemap takes its container's width. In a box wider or narrower than ratio assumes, every tile stretches the same way, so areas stay proportional and nothing overlaps; set ratio near the space the chart usually gets.

With groups, the groups are laid out first, then each group's children inside its rectangle.

Text on a tile

A tile's label is its name (cut with Chart::truncate() to the tile's width, the full name in a title) and, from 40px tall, its value or share. None at all on a tile narrower or shorter than minLabel (40px) at the shape the layout was made for, or shorter than 24px, or where less than two letters of the name would fit. The rest are hidden by container query (Chart::below()) while the chart is too narrow for them. Every tile is its own nested <svg>, which clips, so text never reaches a neighbour.

Ink. Text on a fill is whichever of zinc-950 and white has the higher contrast, per fill and per theme. Support\Ink computes it from the shipped tokens (the same OKLab mix the heat utilities use); the recipe keeps the answers as literal classes so a consumer's Tailwind sees them: text-ink-dark (dark in both themes), text-ink-light (light in both: the semantic pair for zinc-950 and white, since 2.0), text-fg (dark, then light) and text-on-inverse (light, then dark). tests/InkTest.php recomputes every entry from dist/tokens.json and fails when one is no longer the better ink, or when any pairing falls under 4.5:1. ds::funnel-chart uses the same table.

Light-mode contrast of every fill and ink (D dark, L light). Step 3 is the hue itself, the colour a series tile is drawn in; steps 1 to 4 are the colourBy="value" ramp and the funnel's fade.

HueStep 1Step 2Step 3Step 4
neutral (fg-muted)D 10.58D 6.48L 4.83L 8.92
accentD 10.38D 6.21L 5.26L 9.38
successD 11.63D 7.74D 5.43L 7.50
warningD 12.36D 8.62D 6.23L 6.73
dangerD 10.51D 6.43L 4.76L 9.01
series-1D 11.00D 6.94D 4.50L 8.39
series-2D 12.36D 8.63D 6.22L 6.72
series-3D 12.60D 8.96D 6.58L 6.45
series-4D 12.59D 8.93D 6.53L 6.48
series-5D 12.57D 8.92D 6.54L 6.48
series-6D 10.51D 6.39L 4.95L 9.05
series-7D 8.65L 4.54L 8.56L 12.62
series-8D 11.41D 7.45D 5.03L 7.79

The lowest is series-1 at step 3, 4.50:1; in the dark theme the lowest is 4.59 (danger, step 2). A tile being read is drawn at 85% opacity, which can take a dark-theme label just under 4.5 for the moment it is pointed at (4.34 on the docs' Tools tile); the readout says the same text beside it.

Data

CaseResult
['Photos' => 150, …], or rows with label, value, tone / series, hrefone tile each
a row with children (rows of the same shape)a group: its value is the sum of its children's, its children tiled inside it
a child with childrenthrows: one level of nesting
a value that is not a numberthrows, naming the row (Group › Part for a child)
a value of zero or lessdropped from the picture and from the total shares are taken of; listed in the table as "not drawn", counted in the summary
more than eight top-level parts with colourBy="series"throws, pointing at grouping, an "Other", or colourBy="value"
nothing above zerods::empty-state, bare

Shares are largest-remainder percentages of the drawn total, so they add to 100; a share that rounds to 0 reads "<1%".

The summary

Chart::whole() over the top-level parts: "Storage: 3 parts, total 306 GB; the largest is Photos at 49%." With groups it adds the tiles and the largest of them: "… 3 tiles; the largest is Salaries in Engineering at 70%." Dropped parts add "1 of zero or less not drawn." summary replaces it. table is on by default: small tiles carry no label, and the table is where every one of them is read.

Accessibility

Without the scriptWith it
Roleimg with the summary; with any href, group with the summary, each linked tile a link and each other tile img, each named "Salaries, Engineering: $420K (44%)"group, each tile img named as above
KeyboardTab to each link, when there are linksone Tab stop; the arrows move tile to tile in layout order (largest first), Home / End, Escape. With links, nothing roves: each link keeps its own Tab stop, and focus on one shows its readout
Pointerlinks are linksthe tile under the pointer
Readout—the tile's name, its group (with the group's swatch) and its value and share
Hidden tableevery tile, the ones too small to label included, then the parts not drawn and the totalthe same

A focused tile's ring is drawn inside it, so it is never covered by a neighbour or cut by the plot's edge, with or without the script. The surface band inside the ring keeps it legible on a tile of its own hue. The focus sweep in test-behaviour.mjs reads these strokes (data-ds-ring) as the ring.

Do not

  • Do not use a treemap to compare similar values. Areas are read roughly; a bar chart reads lengths exactly.
  • Do not nest more than one level. It throws; a second level would need a second colour channel the tiles do not have.
  • Do not zoom. There is no drill-down; link a tile to a page for its part instead.
  • Do not feed it negatives to mean something. They are dropped; a treemap only draws amounts.

From the package repo: edit this page on GitHub.

Was this page helpful?