Treemap
What is a treemap?
A treemap (also called a tree map or treemap chart) is a chart that shows how a total splits into parts and groups, as nested rectangles. Each item is a rectangle with an area that matches its value, and items in the same group sit together inside their group's rectangle. Colour usually shows the group, or a second value.

A treemap of one year's sales for an imaginary online shop, in millions of dollars (sample data). Area shows sales; colour shows the department. Try the interactive version.
- A treemap shows parts of a whole that sit in groups (a hierarchy), as rectangles inside rectangles.
- Area is the value. The biggest items are the biggest rectangles, so they stand out at once.
- Colour shows the group, or a second value such as growth. Say which in a legend or caption.
- Treemaps fit hundreds of items in one rectangle, but they're poor for exact comparisons: label the main values.
- If there's no hierarchy, or only a few items, a bar chart is clearer.
How to read a treemap
- Check what the area measures. Find the value and its unit: sales in dollars, file sizes in gigabytes, population in people. The whole chart is the total.
- Find the groups. The large outlined blocks are the top level of the hierarchy, usually with a header showing the group's name and total. A group's area is the sum of everything inside it.
- Compare the rectangles by area. A rectangle with twice the area holds twice the value. Rectangles of different shapes are hard to compare by eye, so use the labels or a tooltip for exact numbers.
- Find out what the colour means. It either repeats the group (each group has its own colour) or shows a second value on a colour scale (for example, red for falling and green for rising). Check the legend or caption.
- Look for the big and the small. Note which few items fill most of the chart, and which groups are made of many small pieces.
- Zoom in, or hover, for the small ones. Tiny rectangles have no room for a label. On an interactive treemap, hover to see their values, or zoom in until the labels appear.
A worked example: an online shop's sales
The treemap at the top of this page shows one year's sales for an imaginary online shop: $100 million in total, so every value in millions of dollars is also a percentage of the whole. The numbers are made up for this example.
| Department | Sales ($M) | Product categories ($M) |
|---|---|---|
| Electronics | 38 | Phones 14, laptops 10, TV and audio 7, accessories 4, cameras 3 |
| Home and garden | 24 | Furniture 9, kitchen 6, garden 4, lighting 3, bedding 2 |
| Fashion | 21 | Women's clothing 8, men's clothing 5, shoes 5, bags 2, jewellery 1 |
| Sports | 17 | Fitness 6, bikes 5, camping 3, team sports 2, water sports 1 |
| Total | 100 | 20 categories |
Three things stand out:
- Electronics is the biggest department, with 38% of sales, more than twice the size of sports (17%).
- Phones alone ($14M) are almost as big as the whole sports department ($17M). That's easy to see in the treemap, and easy to miss in a list of departments.
- The long tail is small. The five smallest categories (jewellery, water sports, bedding, bags and team sports) add up to only $8M, or 8% of sales.
The table is better for looking up one number. The treemap shows the structure, the biggest items and the long tail in one look.
How to describe a treemap
Use this template when you report a treemap:
Of the [total and unit] in [the data], the largest group was [group], with [value or share]. The largest single item was [item] in [its group], at [value or share], and the [number] smallest items together made up only [value or share].
Worked example: "Of the shop's $100M of sales last year, the largest department was electronics, with 38%. The largest single category was phones, at $14M, and the five smallest categories together made up only 8%."
What colour means in a treemap
Size always shows the main value. Colour is free for one more job, and a treemap should use it for only one:
| Colour shows | How it's coloured | Good for | Example |
|---|---|---|---|
| The group | One colour per top-level group | Seeing which group each small item belongs to, at a glance | The shop treemap on this page |
| A second value | A colour scale, often diverging (red, through grey, to green) | Showing size and change together | A stock market map: size is company value, colour is today's price change |
| Nothing | One colour for everything | When the groups' outlines are enough and colour would distract | A disk-space treemap of one folder |
When colour shows a second value, add a legend with the scale. When it shows the group, label the groups. Colour that means nothing, such as a random colour per rectangle, makes readers look for a pattern that isn't there.
How does a treemap work?
A treemap is drawn by a layout algorithm that turns the hierarchy into rectangles. Most tools do it for you; knowing the steps helps you understand why a treemap looks the way it does.
- Add up the values. Each group's value is the sum of its children, all the way up to the total.
- Give the whole chart to the top level. The total fills the chart's rectangle. Each group gets a share of the area that matches its share of the total.
- Split each rectangle among its children, the same way, leaving room for the group's header and a small gap.
- Keep the rectangles close to square. The simplest layout, slice and dice, cuts each rectangle into strips, which makes small items into thin slivers. The squarified layout sorts the items from largest to smallest and places them in rows along the shorter side, adding an item to the current row only if that makes the row's rectangles more square.
Near-square rectangles are easier to compare, easier to label and easier to point at. But the squarified layout orders items by size, not by name or date, so the same item can move when the data changes.
When to use a treemap
Use a treemap when your data is a total split into parts that sit in groups, and the most important question is which parts are biggest. Typical uses:
- money: sales by department and product, a budget by department and line item, a portfolio by sector and holding;
- storage and software: disk space by folder and file, code size by module and file;
- markets: a stock market by sector and company, sized by company value and coloured by price change;
- any long list of parts that would need hundreds of bars, where you want the big items and the groups to stand out.
| Chart | Best for |
|---|---|
| Treemap | Many parts of a total in groups, using all the space; the biggest items stand out |
| Sunburst chart | The same kind of data as rings around a centre, which shows the levels of the hierarchy more clearly but fits fewer items |
| Bar chart | Comparing values exactly, when there's no hierarchy or only a few items |
| Pie chart | One total split into a few parts, with no groups |
| Heatmap | Values on a grid of two categories (rows and columns), shown by colour only |
Common treemap mistakes
- Using it for data without a hierarchy, or with few items. Five values in a treemap are harder to compare than five bars. Treemaps pay off with many items in groups.
- Negative or zero values. An area can't be negative. Show losses or falls with colour, or use a different chart, such as a waterfall or a bar chart.
- Expecting exact comparisons. Readers judge area far less precisely than length, and different shapes make it harder still. Label the main values, add tooltips, or give the numbers in a table.
- Too many levels. Beyond two or three levels, the groups become hard to tell apart. Show the top levels and let readers zoom or click into the rest.
- Labels that don't fit. Text cut off at the edge of a rectangle is worse than no label. Hide labels that don't fit and show them on hover or when zoomed in.
- Forgetting the gaps. Headers and padding take a little space from each group, so the areas are slightly smaller than the values say. Keep the padding small, or leave headers out when exact areas matter.
How to create a treemap in JavaScript, React or Angular
SciChart.js draws a treemap with its rectangle series, and a short layout helper turns your hierarchy into rectangles. Because SciChart.js draws with WebGL and WebGPU, the chart stays smooth to zoom and pan, even with thousands of rectangles. Try the treemap below: hover over a rectangle to see its value and share, scroll to zoom into the small ones until their labels appear, drag to pan and double-click to reset.
Here is the code for this treemap. The data is plain JSON: groups with a list of children, and leaves with a name and a value:
{
"name": "All sales",
"children": [
{ "name": "Electronics", "color": "#50C7E0", "children": [{ "name": "Phones", "value": 14 }, { "name": "Laptops", "value": 10 }] }
]
}
- JavaScript
- React
- Angular
// Create a JavaScript treemap with SciChart.js: nested rectangles sized by value and coloured by group
const {
SciChartSurface,
NumericAxis,
NumberRange,
XyxyDataSeries,
FastRectangleRenderableSeries,
EColumnMode,
EColumnYMode,
NativeTextAnnotation,
BoxAnnotation,
EHorizontalAnchorPoint,
EVerticalAnchorPoint,
EMultiLineAlignment,
Thickness,
CustomChartModifier2D,
MouseWheelZoomModifier,
ZoomPanModifier,
ZoomExtentsModifier,
SciChartJsNavyTheme
} = SciChart;
// or, for npm, import { SciChartSurface, ... } from "scichart"
// 1. LAYOUT HELPER (plain JavaScript, no SciChart code)
// Takes a hierarchy { name, children: [{ name, color?, value? , children? }] } and returns one rectangle per node
// in a box `width` x `height`, with y = 0 at the bottom. Leaves need a `value`; a group's value is the sum of its children.
// Uses the squarified layout (Bruls, Huizing and van Wijk, 2000), which keeps rectangles close to square.
function computeTreemapLayout(root, options) {
const {
width = 1000,
height = 600,
padding = 4, // gap inside each group, around its children
headerHeight = 34 // room at the top of each group for its name
} = options || {};
// Work out every node's value, depth, colour and path, bottom up
function prepare(node, parent, depth) {
const prepared = {
name: node.name,
value: node.value || 0,
depth,
parent,
color: node.color || parent?.color || "#50C7E0",
children: [],
x0: 0,
x1: 0,
y0: 0,
y1: 0
};
(node.children || []).forEach((child) => prepared.children.push(prepare(child, prepared, depth + 1)));
if (prepared.children.length) prepared.value = prepared.children.reduce((sum, child) => sum + child.value, 0);
prepared.children.sort((a, b) => b.value - a.value); // the squarified layout needs largest first
return prepared;
}
const tree = prepare(root, null, 0);
// How "un-square" a row of rectangles is when laid along a side of length `side`: 1 is perfect, higher is worse
function worstAspectRatio(rowValues, side, scale) {
const areas = rowValues.map((value) => value * scale);
const total = areas.reduce((sum, area) => sum + area, 0);
const largest = Math.max(...areas);
const smallest = Math.min(...areas);
return Math.max((side * side * largest) / (total * total), (total * total) / (side * side * smallest));
}
// Squarify: fill the box with rows of rectangles, adding to the current row while that makes it more square.
// Coordinates here run from the top-left corner, y downwards.
function squarify(nodes, x, y, w, h) {
const total = nodes.reduce((sum, node) => sum + node.value, 0);
if (total <= 0 || w <= 0 || h <= 0) return;
const scale = (w * h) / total; // area per unit of value
let remaining = nodes.slice();
while (remaining.length) {
const side = Math.min(w, h);
const row = [remaining[0]];
let i = 1;
while (
i < remaining.length &&
worstAspectRatio([...row, remaining[i]].map((n) => n.value), side, scale) <=
worstAspectRatio(row.map((n) => n.value), side, scale)
) {
row.push(remaining[i]);
i++;
}
remaining = remaining.slice(i);
// Lay the row along the shorter side of the space that's left, then shrink that space
const rowArea = row.reduce((sum, node) => sum + node.value * scale, 0);
if (w >= h) {
const rowWidth = rowArea / h; // a column on the left
let top = y;
row.forEach((node) => {
const nodeHeight = (node.value * scale) / rowWidth;
Object.assign(node, { x0: x, x1: x + rowWidth, y0: top, y1: top + nodeHeight });
top += nodeHeight;
});
x += rowWidth;
w -= rowWidth;
} else {
const rowHeight = rowArea / w; // a row along the top
let left = x;
row.forEach((node) => {
const nodeWidth = (node.value * scale) / rowHeight;
Object.assign(node, { x0: left, x1: left + nodeWidth, y0: y, y1: y + rowHeight });
left += nodeWidth;
});
y += rowHeight;
h -= rowHeight;
}
}
}
// Lay out each level inside its parent: the root fills the box, groups keep a header strip for their name
function layoutChildren(node) {
if (!node.children.length) return;
const header = node.depth === 0 ? 0 : headerHeight;
const inset = node.depth === 0 ? 0 : padding;
squarify(
node.children,
node.x0 + inset,
node.y0 + header,
node.x1 - node.x0 - 2 * inset,
node.y1 - node.y0 - header - inset
);
node.children.forEach((child) => {
// Leave a small gap between neighbouring groups
if (child.children.length) Object.assign(child, { x0: child.x0 + padding / 2, x1: child.x1 - padding / 2, y0: child.y0 + padding / 2, y1: child.y1 - padding / 2 });
layoutChildren(child);
});
}
Object.assign(tree, { x0: 0, x1: width, y0: 0, y1: height });
layoutChildren(tree);
// Flatten the tree and flip y so y = 0 is at the bottom, as on a chart axis
const nodes = [];
(function collect(node) {
nodes.push(node);
node.children.forEach(collect);
})(tree);
nodes.forEach((node) => {
const top = height - node.y0;
node.y0 = height - node.y1;
node.y1 = top;
});
return {
root: tree,
groups: nodes.filter((node) => node.depth === 1),
leaves: nodes.filter((node) => !node.children.length),
width,
height
};
}
// 2. TURN THE LAYOUT INTO SCICHART SERIES
// One FastRectangleRenderableSeries for the group backgrounds, and one per group for its leaves,
// so each group has its own colour. Each rectangle is x0..x1 by y0..y1 (start/end in X, top/bottom in Y).
function addTreemapSeries(sciChartSurface, wasmContext, layout, options) {
const { formatValue = (value) => `${value}` } = options || {};
function rectangleSeries(nodes, fill, stroke) {
return new FastRectangleRenderableSeries(wasmContext, {
dataSeries: new XyxyDataSeries(wasmContext, {
xValues: nodes.map((node) => node.x0),
x1Values: nodes.map((node) => node.x1),
yValues: nodes.map((node) => node.y1),
y1Values: nodes.map((node) => node.y0)
}),
columnXMode: EColumnMode.StartEnd,
columnYMode: EColumnYMode.TopBottom,
fill,
stroke,
strokeThickness: 1
});
}
// Group backgrounds, with the group's name and total in the header strip
sciChartSurface.renderableSeries.add(rectangleSeries(layout.groups, "#FFFFFF14", "#FFFFFF33"));
layout.groups.forEach((group) => {
sciChartSurface.annotations.add(
new NativeTextAnnotation({
x1: group.x0 + 6,
y1: group.y1 - 6,
text: `${group.name} ${formatValue(group.value)}`,
textColor: group.color,
fontSize: 18,
horizontalAnchorPoint: EHorizontalAnchorPoint.Left,
verticalAnchorPoint: EVerticalAnchorPoint.Top
})
);
});
// Leaves: one series per group, filled with the group's colour. Colour shows the group; area shows the value.
const leafSeries = layout.groups.map((group) => {
const series = rectangleSeries(group.children, group.color + "CC", "#14233C");
sciChartSurface.renderableSeries.add(series);
return { series, leaves: group.children };
});
// A label in each leaf: name and value. Hidden while the rectangle is too small to hold it.
const labels = layout.leaves.map((leaf) => {
const label = new NativeTextAnnotation({
x1: (leaf.x0 + leaf.x1) / 2,
y1: (leaf.y0 + leaf.y1) / 2,
text: `${leaf.name}\n${formatValue(leaf.value)}`,
textColor: "#FFFFFF",
fontSize: 16,
horizontalAnchorPoint: EHorizontalAnchorPoint.Center,
verticalAnchorPoint: EVerticalAnchorPoint.Center,
multiLineAlignment: EMultiLineAlignment.Center
});
sciChartSurface.annotations.add(label);
return { label, leaf };
});
// Show or hide each label for the current zoom, from the rectangle's size in pixels
function updateLabels() {
const xCalc = sciChartSurface.xAxes.get(0).getCurrentCoordinateCalculator();
const yCalc = sciChartSurface.yAxes.get(0).getCurrentCoordinateCalculator();
labels.forEach(({ label, leaf }) => {
const widthPx = Math.abs(xCalc.getCoordinate(leaf.x1) - xCalc.getCoordinate(leaf.x0));
const heightPx = Math.abs(yCalc.getCoordinate(leaf.y1) - yCalc.getCoordinate(leaf.y0));
const longestLine = Math.max(leaf.name.length, formatValue(leaf.value).length);
const tooSmall = widthPx < longestLine * 7.5 + 6 || heightPx < 44;
if (label.isHidden !== tooSmall) label.isHidden = tooSmall; // only change it when needed, to avoid extra redraws
});
}
sciChartSurface.rendered.subscribe(updateLabels);
// Tooltip text for a leaf: its value, its share of its group and its share of the total
function describe(leaf) {
const group = leaf.parent;
const percent = (part, whole) => `${Math.round((part / whole) * 100)}%`;
return [
`${group.name}: ${leaf.name}`,
`${formatValue(leaf.value)}`,
`${percent(leaf.value, group.value)} of ${group.name.toLowerCase()}`,
`${percent(leaf.value, layout.root.value)} of the total`
].join("\n");
}
return { leafSeries, describe };
}
// 3. HOVER TOOLTIP WITH SCICHART'S HIT-TEST API
// series.hitTestProvider.hitTest(x, y) says whether the mouse is inside one of a rectangle series' rectangles,
// and which one (dataSeriesIndex). The modifier outlines that rectangle and shows its tooltip.
class TreemapTooltipModifier extends CustomChartModifier2D {
treemap;
tooltip;
outline;
constructor(treemap) {
super();
this.treemap = treemap;
this.tooltip = new NativeTextAnnotation({
text: "",
isHidden: true,
textColor: "#FFFFFF",
fontSize: 15,
background: "#0F1A2B",
multiLineAlignment: EMultiLineAlignment.Left,
padding: new Thickness(6, 10, 6, 10),
lineSpacing: 4,
verticalAnchorPoint: EVerticalAnchorPoint.Bottom
});
this.outline = new BoxAnnotation({ isHidden: true, fill: "#FFFFFF22", stroke: "#FFFFFF", strokeThickness: 2 });
}
onAttach() {
super.onAttach();
this.parentSurface.annotations.add(this.outline, this.tooltip);
}
onDetach() {
this.parentSurface?.annotations.remove(this.outline);
this.parentSurface?.annotations.remove(this.tooltip);
super.onDetach();
}
// Returns the leaf under the mouse and the hit-test result, or null
hitTestLeaf(mousePoint) {
for (const { series, leaves } of this.treemap.leafSeries) {
const hitTestInfo = series.hitTestProvider.hitTest(mousePoint.x, mousePoint.y);
if (hitTestInfo.isHit) return { leaf: leaves[hitTestInfo.dataSeriesIndex], hitTestInfo };
}
return null;
}
modifierMouseMove(args) {
super.modifierMouseMove(args);
const hit = this.hitTestLeaf(args.mousePoint);
if (hit) {
const { leaf } = hit;
Object.assign(this.outline, { x1: leaf.x0, x2: leaf.x1, y1: leaf.y0, y2: leaf.y1 });
// Place the tooltip just above the mouse, flipped to the left near the right-hand edge
const { x, y } = hit.hitTestInfo.hitTestPointValues;
const xRange = this.parentSurface.xAxes.get(0).visibleRange;
const nearRightEdge = x > xRange.min + xRange.diff * 0.65;
this.tooltip.text = this.treemap.describe(leaf);
this.tooltip.horizontalAnchorPoint = nearRightEdge ? EHorizontalAnchorPoint.Right : EHorizontalAnchorPoint.Left;
this.tooltip.x1 = x + xRange.diff * (nearRightEdge ? -0.012 : 0.012);
this.tooltip.y1 = y + this.parentSurface.yAxes.get(0).visibleRange.diff * 0.03;
}
this.tooltip.isHidden = !hit;
this.outline.isHidden = !hit;
}
modifierMouseLeave(args) {
super.modifierMouseLeave(args);
this.tooltip.isHidden = true;
this.outline.isHidden = true;
}
}
// 4. THE CHART
async function treemap(divElementId) {
const { wasmContext, sciChartSurface } = await SciChartSurface.create(divElementId, {
theme: new SciChartJsNavyTheme()
});
// Your data: a hierarchy as JSON. Leaves have values; groups add up their children.
const layout = computeTreemapLayout(getShopSalesData(), { width: 1000, height: 600 });
// The axes only set the drawing area. They're hidden: a treemap has no scales.
// Extra space at the bottom keeps the lowest rectangles clear of the chart's bottom-left corner.
sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { isVisible: false, growBy: new NumberRange(0.005, 0.005) }));
sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { isVisible: false, growBy: new NumberRange(0.07, 0.01) }));
const treemapSeries = addTreemapSeries(sciChartSurface, wasmContext, layout, { formatValue: (value) => `$${value}M` });
// Hover for values, scroll to zoom into small rectangles, drag to pan, double-click to reset
sciChartSurface.chartModifiers.add(
new TreemapTooltipModifier(treemapSeries),
new MouseWheelZoomModifier(),
new ZoomPanModifier(),
new ZoomExtentsModifier()
);
}
import { SciChartReact } from "scichart-react";
import {
SciChartSurface,
NumericAxis,
NumberRange,
MouseWheelZoomModifier,
ZoomPanModifier,
ZoomExtentsModifier,
SciChartJsNavyTheme
} from "scichart";
// computeTreemapLayout, addTreemapSeries and TreemapTooltipModifier from the JavaScript tab, saved as treemap.ts
import { computeTreemapLayout, addTreemapSeries, TreemapTooltipModifier } from "./treemap";
// initChart builds the chart; SciChartReact creates it, sizes it and cleans it up
const initChart = async (rootElement: string | HTMLDivElement) => {
const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
theme: new SciChartJsNavyTheme()
});
// { name, children: [{ name, color, children: [{ name, value }] }] }
const layout = computeTreemapLayout(getYourTreemapData(), { width: 1000, height: 600 });
sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { isVisible: false, growBy: new NumberRange(0.005, 0.005) }));
sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { isVisible: false, growBy: new NumberRange(0.07, 0.01) }));
const treemap = addTreemapSeries(sciChartSurface, wasmContext, layout, { formatValue: (value: number) => `$${value}M` });
sciChartSurface.chartModifiers.add(
new TreemapTooltipModifier(treemap),
new MouseWheelZoomModifier(),
new ZoomPanModifier(),
new ZoomExtentsModifier()
);
return { sciChartSurface };
};
export const Treemap = () => (
<SciChartReact initChart={initChart} style={{ width: "100%", height: 500 }} />
);
import { Component } from "@angular/core";
import { ScichartAngularComponent } from "scichart-angular";
import {
SciChartSurface,
NumericAxis,
NumberRange,
MouseWheelZoomModifier,
ZoomPanModifier,
ZoomExtentsModifier,
SciChartJsNavyTheme
} from "scichart";
// computeTreemapLayout, addTreemapSeries and TreemapTooltipModifier from the JavaScript tab, saved as treemap.ts
import { computeTreemapLayout, addTreemapSeries, TreemapTooltipModifier } from "./treemap";
@Component({
selector: "app-treemap",
standalone: true,
imports: [ScichartAngularComponent],
template: `<scichart-angular [initChart]="initChart" style="display: block; width: 100%; height: 500px"></scichart-angular>`
})
export class TreemapComponent {
// initChart builds the chart; scichart-angular creates it, sizes it and cleans it up
initChart = async (rootElement: string | HTMLDivElement) => {
const { wasmContext, sciChartSurface } = await SciChartSurface.create(rootElement, {
theme: new SciChartJsNavyTheme()
});
// { name, children: [{ name, color, children: [{ name, value }] }] }
const layout = computeTreemapLayout(getYourTreemapData(), { width: 1000, height: 600 });
sciChartSurface.xAxes.add(new NumericAxis(wasmContext, { isVisible: false, growBy: new NumberRange(0.005, 0.005) }));
sciChartSurface.yAxes.add(new NumericAxis(wasmContext, { isVisible: false, growBy: new NumberRange(0.07, 0.01) }));
const treemap = addTreemapSeries(sciChartSurface, wasmContext, layout, { formatValue: (value: number) => `$${value}M` });
sciChartSurface.chartModifiers.add(
new TreemapTooltipModifier(treemap),
new MouseWheelZoomModifier(),
new ZoomPanModifier(),
new ZoomExtentsModifier()
);
return { sciChartSurface };
};
}
Key points:
computeTreemapLayoutis plain JavaScript with no SciChart code, so you can reuse it anywhere. It adds up the values, then uses the squarified layout to give every group and leaf a rectangle, leaving a header strip and a small gap in each group. It works for any number of levels.- Every rectangle is drawn by a FastRectangleRenderableSeries with an
XyxyDataSeries:columnXMode: EColumnMode.StartEndreadsxValuesandx1Valuesas the left and right edges, andcolumnYMode: EColumnYMode.TopBottomreadsyValuesandy1Valuesas the top and bottom. One series per group gives each group its own colour. - The tooltip uses the series' hit-test API:
series.hitTestProvider.hitTest(x, y)returnsisHit: trueand the rectangle'sdataSeriesIndexwhen the mouse is inside one.TreemapTooltipModifierextendsCustomChartModifier2D(custom chart modifiers) to run it on every mouse move. - Labels are NativeTextAnnotations. After each redraw, each label is shown only if its rectangle is big enough on screen, so zooming in reveals the small ones. The axes are hidden, but they still set the drawing area, so zoom and pan work as on any chart.
See it in the demo app: JavaScript treemap chart · React treemap chart · Angular treemap chart
Go deeper: SciChart.js treemap documentation, which walks through the demo app's stock market treemap, coloured by price change, and the SciChart.js rectangle series documentation.
Frequently asked questions
What is a treemap used for?
A treemap is used to show how a total is made up when the parts sit in groups, and to find the biggest items quickly. Typical examples are sales by department and product, a budget by department and line item, disk space by folder and file, and a stock market by sector and company. Because it fills a rectangle completely, it can show hundreds of items in a small space.
Is a treemap the same as a heatmap?
No. In a treemap, each rectangle's size shows a value and the rectangles are arranged by group, so position carries no meaning. In a heatmap, every cell is the same size and sits in a grid of rows and columns, and colour alone shows the value. A treemap can use colour for a second value, which makes it look like a heatmap, but its sizes still show the main value.
What is the difference between a treemap and a tree diagram?
Both show a hierarchy, but in different ways. A tree diagram draws each item as a node with lines to its parent and children, so it shows the structure clearly but not the size of each item. A treemap draws each item as a rectangle inside its parent's rectangle, with area matching its value, so it shows the sizes but makes the levels harder to follow.
Who invented the treemap?
The treemap was invented by Ben Shneiderman at the University of Maryland in 1990, to see which folders and files were filling up a shared hard disk. He published the method in 1992. In 2000, Mark Bruls, Kees Huizing and Jarke van Wijk published the squarified layout, which keeps the rectangles close to square and is widely used today.
How do I make a treemap in JavaScript?
Store the data as a hierarchy, where each leaf has a name and a value, and each group has a list of children. A layout step, such as the squarified algorithm, turns it into one rectangle per item. With SciChart.js you draw the rectangles with a FastRectangleRenderableSeries, using start and end values for X and top and bottom values for Y, and use the series' hit-test to add tooltips. The same code works in React with scichart-react and in Angular with scichart-angular.
Sources
- Shneiderman, B. (1992), Tree visualization with tree-maps: 2-d space-filling approach. ACM Transactions on Graphics, 11(1), 92–99. The paper that introduced the treemap.
- Bruls, M., Huizing, K. and van Wijk, J. J. (2000), Squarified treemaps. In Data Visualization 2000, Eurographics, 33–42. The squarified layout used on this page.
- Shneiderman, B. and Plaisant, C. (1998, last updated 2014), Treemaps for space-constrained visualization of hierarchies. University of Maryland Human-Computer Interaction Lab. Shneiderman's own history of the treemap, from its start in 1990 as a map of a full hard disk.