Treemap
Overview
The Treemap component draws a tree of values as nested tiles, each sized by its share of the whole.
It wraps Recharts' Treemap and comes from the
@allxsmith/bestax-charts package, which the Charts guide
shows how to install. You give it a tree of data, each node a name with a value or
children, and it draws the tree with Bulma's colors, writes each name in a color readable on
its tile, shows a tooltip in a Bulma box, and renders a data table screen readers read.
Import
import { Treemap } from '@allxsmith/bestax-charts';
Usage
Basic Usage
Each node of data has a name, and either a value or children, whose values it adds up.
label names the chart; it's required, because it's what a screen reader announces.
function example() { const budget = [ { name: 'Housing', children: [ { name: 'Rent', value: 1400 }, { name: 'Utilities', value: 180 }, ], }, { name: 'Food', children: [ { name: 'Groceries', value: 520 }, { name: 'Dining out', value: 240 }, ], }, { name: 'Transport', value: 400 }, { name: 'Savings', value: 600 }, ]; return <Treemap label="Monthly budget" data={budget} />; }
One Level at a Time
type="nest" draws only the top level. Choosing a group, with the pointer or with Enter or
Space once Tab has reached it, draws its children in its place, and the breadcrumb above the
chart goes back up. A group's name ends in › to say it opens.
function example() { const budget = [ { name: 'Housing', children: [ { name: 'Rent', value: 1400 }, { name: 'Utilities', value: 180 }, { name: 'Insurance', value: 60 }, ], }, { name: 'Food', children: [ { name: 'Groceries', value: 520 }, { name: 'Dining out', value: 240 }, ], }, { name: 'Savings', value: 600 }, ]; return <Treemap label="Monthly budget" type="nest" data={budget} />; }
Names on the Tiles
Each tile that has room for its name shows it, in the -invert color of the tile's Bulma color
so it stays readable in light and dark mode. In a flat treemap only the innermost nodes are
named, since they cover their groups. showLabels={false} leaves the names off.
function example() { const budget = [ { name: 'Rent', value: 1400 }, { name: 'Groceries', value: 520 }, { name: 'Savings', value: 600 }, ]; return ( <Columns> <Column> <Treemap label="With names" showLabel data={budget} height={200} /> </Column> <Column> <Treemap label="Without names" showLabel showLabels={false} data={budget} height={200} /> </Column> </Columns> ); }
Tile Shape
Recharts lays the tiles out to keep each one near a width to height ratio, the golden ratio
unless tileAspectRatio sets another. Recharts calls it aspectRatio, a name bestax's helper
props already give Bulma's aspect ratio classes.
function example() { const files = [ { name: 'src', value: 820 }, { name: 'docs', value: 410 }, { name: 'tests', value: 380 }, { name: 'scripts', value: 120 }, { name: 'assets', value: 90 }, ]; return <Treemap label="Lines by folder" data={files} tileAspectRatio={1} />; }
Formatting Values
Values are written as useNumberFormat writes a number, in the
LocaleProvider's locale. valueFormatter writes them your way instead, in the tooltip and the
data table, and unit is written after each one. Shares are written as the locale writes a
percentage.
function example() { const dollars = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0, }); return ( <Treemap label="Revenue by product" data={[ { name: 'Pro plan', value: 48200 }, { name: 'Team plan', value: 31900 }, { name: 'Add-ons', value: 9400 }, ]} valueFormatter={value => dollars.format(value)} showTable /> ); }
Colors
A node's color takes a Bulma color name, a shade of one of Bulma's six palette colors
(primary-30, info-dark), or any CSS color. A top-level node with no color takes the next of
Bulma's palette colors, and a node below takes its parent's, so each branch draws in one color,
its tiles parted by the chart's stroke color. A name on a CSS color is written in the chart's
text color, since the chart can't tell which text reads on it.
function example() { const teams = [ { name: 'Design', color: 'link-30', children: [ { name: 'Research', value: 4 }, { name: 'Visual', value: 6, color: 'link-45' }, ], }, { name: 'Engineering', color: 'success-30', children: [ { name: 'Web', value: 12 }, { name: 'Platform', value: 8, color: 'success-45' }, ], }, ]; return <Treemap label="People by team" data={teams} height={240} />; }
Showing the Data Table
The data table is always there for screen readers: a row per node, depth first, headed by its
path, with its value and its share of the whole. showTable puts it on screen too, below the
chart, and showLabel shows the chart's label above it as a caption. labels translates the
table's and the tooltip's headers.
function example() { return ( <Treemap label="Haushalt" showLabel showTable data={[ { name: 'Wohnen', children: [ { name: 'Miete', value: 1250 }, { name: 'Strom', value: 95 }, ], }, { name: 'Essen', value: 480 }, ]} labels={{ name: 'Posten', value: 'Betrag', share: 'Anteil' }} unit=" €" height={200} /> ); }
Recharts Props and Children
chartProps goes to Recharts' Treemap after the chart's own props (here, nodeGap for room
between the tiles), and tooltipProps to its Tooltip. children adds more elements inside
the drawing, such as <defs> for a pattern a node names with url(#id).
function example() { return ( <Treemap label="Planned and unplanned work" data={[ { name: 'Planned', value: 700, color: 'info' }, { name: 'Unplanned', value: 300, color: 'url(#work-stripes)' }, ]} chartProps={{ nodeGap: 4 }} height={200} > <defs> <pattern id="work-stripes" width="8" height="8" patternUnits="userSpaceOnUse" patternTransform="rotate(45)" > <rect width="8" height="8" fill="var(--bulma-warning)" /> <line x1="0" y1="0" x2="0" y2="8" stroke="var(--bulma-scheme-main)" strokeWidth="4" /> </pattern> </defs> </Treemap> ); }
Size
The chart fills its container's width and is 300 pixels tall. width and height take pixels
or a percentage of the container. A chart sized in pixels draws on the server as well; one sized
in percent draws once the browser has measured its container, and follows it as it changes. Its
data table renders on the server either way.
<Treemap label="Two items" data={[ { name: 'Rent', value: 1400 }, { name: 'Food', value: 760 }, ]} width={320} height={180} />
Theming
The tiles are parted by --bulma-chart-stroke-color, which falls back to Bulma's
--bulma-scheme-main. The BarChart's Theming section lists the other
variables the charts draw with.
:root {
--bulma-chart-stroke-color: var(--bulma-scheme-main); /* between tiles */
}
Accessibility
- The chart is a
<figure>named bylabel, which is also its data table's caption.descriptionis read with the name. A flat treemap's drawing is a picture (role="img") named and described by the same caption and description. - A data table of every node renders beside the drawing, visually hidden with Bulma's
is-sr-only, so a screen reader reads the numbers the tiles show (WAI: complex images). Each row is headed by the node's path, such as "Housing / Rent", and gives its value and its share of the whole.showTableputs it on screen. - Recharts' keyboard layer doesn't reach a treemap's tiles, so a flat treemap is not a tab stop and its tooltip follows the pointer only. The data table holds every value the tooltip shows.
- In a nest treemap each group is a button. Tab reaches it, and Enter or Space opens it; the
focus then moves to the breadcrumb's last step, which says where you are
(
aria-current="location"), and the steps before it are buttons that go back up. The breadcrumb is a navigation landmark named by the chart'slabel. A focused group is ringed in a color readable on it. - The names written on the tiles take the
-invertcolor of a Bulma color, so they read on it in light and dark mode. A name that doesn't fit its tile is left off; the data table names every node. - Branches are told apart by color in the drawing. The names on the tiles and the paths in the data table say which branch a tile is in; a pattern fill (see Recharts Props and Children) can tell them apart too.
Related Components
SunburstChart: The same tree of values drawn as rings.Sankey: Flows between nodes.BarChart: Values drawn as bars.Breadcrumb: The Bulma breadcrumb a nest treemap's levels are drawn with.Table: The Bulma table the chart's data table is drawn with.LocaleProvider: The locale the chart writes its numbers in.- Helper Props: Bulma helper props for spacing, color, etc.
Additional Resources
Props
| Prop | Type | Default | Description |
|---|---|---|---|
data | readonly ChartNode[] | — | Required. The nodes to draw, each a tile sized by its value. A node's children are drawn inside it. |
type | 'flat' | 'nest' | 'flat' | How much of the tree it draws at once. flat draws every node, each group's children inside it. nest draws one level at a time, a group's name marked with ›: choosing a group, with the pointer or with Enter or Space once Tab has reached it, draws its children in its place, and a breadcrumb above the chart goes back up. The data table lists every node either way. |
tileAspectRatio | number | 1.618 (the golden ratio) | The width to height ratio the tiles keep near. Recharts calls it aspectRatio, a name the helper props already give Bulma's aspect ratio classes. |
showLabels | boolean | true | Writes each node's name inside its tile, in a color readable on it, where the name fits. In a flat treemap only the innermost nodes are named, since they cover their groups. |
labels | { name?: string; value?: string; share?: string } | — | The header text of the data table and the tooltip, to translate. |
chartProps | Partial<React.ComponentProps<typeof RechartsTreemap>> | — | Props passed to Recharts' Treemap, after the chart's own, such as nodeGap. |
tooltipProps | Partial<TooltipProps> | — | Props passed to Recharts' Tooltip, after the chart's own. |
className | string | — | Additional CSS classes to apply. |
label | string | — | Required. What the chart shows, such as "Monthly sales". It names the chart for assistive tech, and captions its data table. Shown above the chart with showLabel. |
description | string | — | A sentence or two on what the chart says, such as the trend it shows. A screen reader reads it with the chart's name. Not shown on screen. |
showLabel | boolean | false | Shows label above the chart as its caption. |
showTable | boolean | false | Shows the chart's data table below it. The table is always there for screen readers, which read the numbers from it; this puts it on screen as well. |
hasTooltip | boolean | true | Shows a tooltip with the values under the pointer, or under the keyboard focus as the arrow keys move through the chart. |
valueFormatter | ChartValueFormatter | — | Writes each value on the axes, in the tooltip, on the labels and in the data table. Without one, values are written as useNumberFormat writes a number, in the LocaleProvider's locale. |
unit | string | — | Written after every value, such as % or kg. |
width | ChartSize | '100%' | The chart's width, in pixels or as a percentage of its container. A percentage makes the chart follow its container's size as the browser measures it. Recharts draws in the browser either way: a server render holds the figure, its caption, legend and data table, and the drawing appears once the page hydrates. |
height | ChartSize | 300 | The chart's height, in pixels or as a percentage of its container, which then needs a height of its own. |
children | React.ReactNode | — | More Recharts elements to draw inside the chart, such as a Brush, a ReferenceArea, or <defs> for a pattern a color names with url(#id). |
ref | React.Ref<HTMLElement> | — | Ref forwarded to the root <figure>. |
... | All standard HTML attributes and Bulma helper props | — | See Helper Props |
Types:
ChartColor: Bulma color |`${(typeof paletteColors)[number]}-${(typeof validColorShades)[number]}`|(string & {})— A color for a series, a slice or a mark. A Bulma color name such asprimaryorgrey-light, a shade of one of Bulma's six palette colors such asprimary-60orinfo-dark, or any CSS color, such as#3e8ed0,var(--brand)orurl(#stripes)for a pattern the chart's children define. A Bulma name draws with Bulma's own CSS variable, so it followsThemeand dark mode.ChartNode: One node of a tree chart'sdata, such as a category or one of its parts. Its fields:name:string— Required. What the node is called. The chart writes it on the node, and the data table heads the node's row with it, after its ancestors' names.value:number— The node's size. A node with children takes the sum of theirs instead, and a missing or negative value counts as zero.color:ChartColor— Its color. Without one, a top-level node takes the next of Bulma's palette colors, and a node below takes its parent's, so each branch draws in one color.children:{ name: string; value?: number; color?: ChartColor; children?: ChartNode[] }[]— The nodes inside this one.
ChartSize:number|`${number}%`— A chart's width or height: pixels, or a percentage of its container.ChartValueFormatter:(value: number) => string— Writes a value for the axes, the tooltip, the labels and the data table.