Charts
Treemap
Parts of a whole by size, for many items or two levels. Every chart can also be saved as an image.
import '@reactloom/charts/styles.css';
import { Treemap, chartToSvg, downloadChart } from '@reactloom/charts';Examples
Two levels
Use the arrow keys to move between data points, Home and End to jump to the first and last.
import { Treemap } from '@reactloom/charts';
const storage = [
{ label: 'Design', children: [{ label: 'Assets', value: 420 }, { label: 'Prototypes', value: 160 }, { label: 'Archive', value: 90 }] },
{ label: 'Engineering', children: [{ label: 'Builds', value: 380 }, { label: 'Logs', value: 210 }] },
{ label: 'Finance', children: [{ label: 'Reports', value: 120 }, { label: 'Invoices', value: 60 }] },
];
export default function Example() {
return <Treemap title="Storage by team (GB)" data={storage} />;
}Download as PNG or SVG
downloadChart and chartToSvg work with every ReactLoom chart.
Use the arrow keys to move between data points, Home and End to jump to the first and last.
import { chartToSvg, downloadChart, Treemap } from '@reactloom/charts';
import { Button, HStack, Stack } from '@reactloom/ui';
import { useRef } from 'react';
const sales = [
{ label: 'Laptops', value: 540 },
{ label: 'Phones', value: 410 },
{ label: 'Tablets', value: 190 },
{ label: 'Accessories', value: 120 },
{ label: 'Services', value: 80 },
];
// Every chart can be saved as SVG or PNG, with theme colours resolved.
export default function Example() {
const ref = useRef<HTMLDivElement>(null);
const figure = () => ref.current?.querySelector('figure');
return (
<Stack gap="3" style={{ width: '100%' }}>
<HStack gap="2">
<Button size="sm" variant="secondary" onClick={() => figure() && void downloadChart(figure() as Element, { format: 'png', filename: 'sales' })}>
Download PNG
</Button>
<Button size="sm" variant="secondary" onClick={() => figure() && void navigator.clipboard?.writeText(chartToSvg(figure() as Element))}>
Copy SVG
</Button>
</HStack>
<div ref={ref}>
<Treemap title="Sales by category (k€)" data={sales} />
</div>
</Stack>
);
}API reference
| Prop | Type | Default |
|---|---|---|
datarequiredParents sum their children. | { label, value?, children? }[] | — |
titlerequiredNames the figure. | string | — |
descriptionThe takeaway; a factual summary is generated when omitted. | ReactNode | — |
tableThe data table alternative. | 'toggle' | 'visible' | 'none' | 'toggle' |
height · valueFormat · formatValue · messagesAs on the other charts. | | — |
Customization
Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.
State attributes
Target these in your CSS to style states without depending on class names.
| Attribute | Values |
|---|---|
[data-active] | present when on |
Theme tokens it reads
--pl-color-bg-surface --pl-color-fg-default --pl-color-fg-muted
Accessibility
- The chart is a figure named by its title and described by a summary; the drawing is hidden from assistive technology.
- The plot is one tab stop: arrow keys move between items and each is announced; the data table gives every value.
- Items are read with their parent, value and share.
| Key | Action |
|---|---|
| Tab | Focus the plot |
| ← → ↑ ↓ | Move between items |
| Home / End | First / last item |
| Escape | Clear the highlight |
Usage guidelines
Do
- Label the unit in the title.
Don’t
- Don't use a treemap to compare similar sizes precisely — use a BarChart.