Treemap
This tutorial will look at how to configure treemap series.
Layout algorithm
Section titled “Layout algorithm”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" }));var 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" |
![]() |
Margins
Section titled “Margins”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 }));var series = root.container.children.push( am5hierarchy.Treemap.new(root, { valueField: "value", categoryField: "name", childDataField: "children", nodePaddingOuter: 20, nodePaddingInner: 10 }));

nodePaddingOuter: 20nodePaddingInner: 10
nodePaddingInner: 10nodePaddingTop: 100nodePaddingBottom: 20nodePaddingLeft: 20nodePaddingRight: 20Node appearance
Section titled “Node appearance”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});series.rectangles.template.setAll({ fillOpacity: 0.7, cornerRadiusTL: 4, cornerRadiusTR: 4, cornerRadiusBL: 4, cornerRadiusBR: 4});Hover behavior
Section titled “Hover behavior”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.rectangles.template.states.create("hover", { fill: am5.color(0x677935), fillOpacity: 1});Labels
Section titled “Labels”Configuring labels
Section titled “Configuring labels”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}"});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.
Label content
Section titled “Label content”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});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).
Parent node headers
Section titled “Parent node headers”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:
nodePaddingTopreserves 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;});let series = root.container.children.push( am5hierarchy.Treemap.new(root, { valueField: "value", categoryField: "name", childDataField: "children", topDepth: 1, initialDepth: 1, nodePaddingTop: 24 }));
function showsChildren(label: am5.Label): boolean { const dataItem = label.dataItem as am5.DataItem<am5hierarchy.ITreemapDataItem> | undefined; return !!dataItem && !!dataItem.get("children") && !dataItem.get("disabled");}
series.labels.template.adapters.add("y", (y, target) => { return showsChildren(target) ? 0 : y;});
series.labels.template.adapters.add("centerY", (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.
treemap-parent-headersThe example also drills one level per click. See “Drilling one level per click”.
Disabling labels
Section titled “Disabling labels”To disable series labels, we can set forceHidden setting to true in their template:
series.labels.template.set("forceHidden", true);series.labels.template.set("forceHidden", true);Tooltips
Section titled “Tooltips”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}[/]");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).
Example
Section titled “Example”treemap-chart-2



