Skip to content

Treemap

This tutorial will look at how to configure treemap series.

Treemap can use different ways to layout algorithms.

It’s controlled by the setting layoutAlgorithm:

let series = root.container.children.push(
am5hierarchy.Treemap.new(root, {
valueField: "value",
categoryField: "name",
childDataField: "children",
layoutAlgorithm: "sliceDice"
})
);

Available settings are as follows:

Setting value Example
"binary"
"squarify"
"slice"
"dice"
"sliceDice"

By default, the rectangles of the nodes are plotted without any gaps between them.

We do have a few settings at our disposal that we can use to control the gaps. All accept numeric value in pixels.

Setting Comment
nodePaddingInner Gap between nodes.
nodePaddingOuter Gap between a node’s edges and the nodes inside it. For the top node, that’s the gap to the chart’s edges. Overrides the four settings below.
nodePaddingTop Gap between a node’s top edge and the nodes inside it. Ignored if nodePaddingOuter is set. Use it for parent node headers.
nodePaddingBottom Gap between a node’s bottom edge and the nodes inside it. Ignored if nodePaddingOuter is set.
nodePaddingLeft Gap between a node’s left edge and the nodes inside it. Ignored if nodePaddingOuter is set.
nodePaddingRight Gap between a node’s right edge and the nodes inside it. Ignored if nodePaddingOuter is set.
let series = root.container.children.push(
am5hierarchy.Treemap.new(root, {
valueField: "value",
categoryField: "name",
childDataField: "children",
nodePaddingOuter: 20,
nodePaddingInner: 10
})
);

The appearance of node rectangles can be configured using their template, accessible on series property: rectangles.template.

The template can have contain any RoundedRectangle setting.

series.rectangles.template.setAll({
fillOpacity: 0.7,
cornerRadiusTL: 4,
cornerRadiusTR: 4,
cornerRadiusBL: 4,
cornerRadiusBR: 4
});

We can set setting values to apply to a rectangle when it is hovered by creating a “hover” state for it.

series.rectangles.template.states.create("hover", {
fill: am5.color(0x677935),
fillOpacity: 1
});

Series label configuration is done via its template, accessible via series property labels.template.

series.labels.template.setAll({
fontSize: 20,
fill: am5.color(0x550000),
text: "{category}"
});

Partition series uses Label for its labels. Check out its class reference for all the possible settings.

Node labels are pre-set to display name of the category and its percent value.

We can modify contents of the tooltips using text setting on a series label template:

series.labels.template.setAll({
text: "{category}: [bold]{sum}[/]",
fontSize: 14
});

Contents of the tooltip can include data placeholders (codes in curly brackets that will be replaced by actual data) and in-line formatting blocks (formatting instructions enclosed in square brackets).

Every label is centered on its tile, so when child tiles are shown, they cover their parent’s label. There’s no header setting. To show parent names in a header band:

  • nodePaddingTop reserves a band at the top of every parent tile.
  • Adapters move the label of each tile whose children are shown into that band. Other labels stay centered.
var series = root.container.children.push(
am5hierarchy.Treemap.new(root, {
valueField: "value",
categoryField: "name",
childDataField: "children",
topDepth: 1,
initialDepth: 1,
nodePaddingTop: 24
})
);
function showsChildren(label) {
var dataItem = label.dataItem;
return dataItem && dataItem.get("children") && !dataItem.get("disabled");
}
series.labels.template.adapters.add("y", function(y, target) {
return showsChildren(target) ? 0 : y;
});
series.labels.template.adapters.add("centerY", function(centerY, target) {
return showsChildren(target) ? 0 : centerY;
});

When you drill in, the selected tile’s name stays in the band above its children.

With topDepth: 1, the hidden root node keeps its band too, which leaves an empty strip at the top. To label it instead, show the root with topDepth: 0.

Open in:New tabChart Maker
Treemap with parent headers and one-level drill-downtreemap-parent-headers

The example also drills one level per click. See “Drilling one level per click”.

To disable series labels, we can set forceHidden setting to true in their template:

series.labels.template.set("forceHidden", true);

Rectangles series are pre-set to display a tooltip on hover containing name of the category and its value.

We can modify contents of the tooltips using tooltipText on node’s template:

series.nodes.template.set("tooltipText", "{category}: [bold]{sum}[/]");

Contents of the tooltip can include data placeholders (codes in curly brackets that will be replaced by actual data) and in-line formatting blocks (formatting instructions enclosed in square brackets).

Open in:New tabChart Maker
Treemap charttreemap-chart-2