React ChartsTreemap Series

Enterprise

A Treemap Series is used to render hierarchical data structures or trees. Each node in the tree is represented by a rectangle, with the area of the rectangle representing the value.

Simple Treemap Copy Link

The Treemap Series is designed to display a single series and is created using the treemap series type.

{
    series: [
        {
            type: 'treemap',
            labelKey: 'title',
        },
    ],
}

The data passed in should be an array of nodes, with each node optionally containing children.

let data = [
    {
        title: 'Pensions',
        children: [
            { title: 'Sickness and disability', total: 61.2, change: 8.7 },
            { title: 'Old age', total: 141.8, change: 17.9 },
            { title: 'Survivors', total: 1.4, change: 0 },
        ],
    },
    {
        title: 'Health Care',
        // ...
    },
    // ...
];

The labelKey defines what will appear as the title for each tile.

Sizing Copy Link

By default, each leaf node's rectangle will have approximately the same area.

However, the Treemap Series is best suited to providing size values to provide relative sizing between these rectangles.

The sizeKey can be used to provide a numeric value to adjust the relative sizing. Additionally, the optional sizeName property can be set to set the title that appears next to the value in tooltips.

{
    series: [
        {
            type: 'treemap',
            labelKey: 'title',
            sizeKey: 'total',
            sizeName: 'Total',
        },
    ],
}

Only the sizes of leaf nodes will be accounted for when computing the relative sizes. When sizes are used, the nodes will be re-ordered so larger nodes appear towards the top left corner.

Colour Scale Copy Link

Use colorScale to control how colorKey values map to colours.

{
    series: [
        {
            type: 'treemap',
            labelKey: 'title',
            colorKey: 'change',
            colorName: 'Change',
            colorScale: {
                mode: 'discrete',
                fills: [
                    { color: 'tomato', stop: -0.1, name: 'Decline' },
                    { color: 'gold', stop: 0.1, name: 'Flat' },
                    { color: 'seagreen', name: 'Growth' },
                ],
            },
        },
    ],
}

In this example:

  • Use the toggle to switch between discrete mode with named stops shown in a category legend, and a continuous gradient shown in a gradient legend.

See the Colour Scale page for the full range of colour scale options including discrete mode, named stops, fixed domains, missing data, and gradient legend customisation.

Other Colours Copy Link

It's possible to override the default colours, or the colours on a group or tile basis.

{
    series: [
        {
            type: 'treemap',
            labelKey: 'name',
            sizeKey: 'total',
            sizeName: 'Total',
            fills: ['#E64A19', '#F57C00', '#FFA000', '#FBC02D', '#AFB42B', '#689F38', '#388E3C', '#00796B', '#0097A7', '#0288D1'],
            strokes: ['#D84315', '#EF6C00', '#FF8F00', '#F9A825', '#9E9D24', '#558B2F', '#2E7D32', '#00695C', '#00838F', '#0277BD'],
        },
    ],
}

In this configuration:

  • fills and strokes are an array of colours to use for the fills and strokes, where node receives the colour indexed by the index of its root node

When a colorScale is used, the fills and strokes arrays are ignored.

Labels Copy Link

Both the labels for leaf and non-leaf nodes can be customised.

For leaf nodes only, they can contain secondary labels, and their labels can also be shrunk to fit in the available space.

Labels can be customised through the group and tile properties for non-leaf nodes and leaf nodes, respectively.

{
    series: [
        {
            type: 'treemap',
            labelKey: 'title',
            secondaryLabelKey: 'total',
            sizeKey: 'total',
            sizeName: 'Total',
            group: {
                label: {
                    fontSize: 18,
                    spacing: 2,
                },
            },
            tile: {
                label: {
                    fontSize: 24,
                    minimumFontSize: 9,
                    spacing: 8,
                },
                secondaryLabel: {
                    formatter: (params) => `£${params.value.toFixed(1)}bn`,
                },
            },
        },
    ],
}

In this configuration:

  • fontSize sets the size of the font
  • minimumFontSize will enable the font size to shrink down to the given value if there is not enough space (tiles only)
  • spacing controls the amount of space below a label
  • padding adds space between the edge of a group or tile and its contents
  • formatter allows customising the value of a label using a function

Layout Copy Link

Various spacing values can be adjusted to tweak the layout of the chart.

{
    series: [
        {
            type: 'treemap',
            labelKey: 'title',
            sizeKey: 'total',
            sizeName: 'Total',
            group: {
                padding: 12,
                gap: 5,
            },
            tile: {
                padding: 10,
                gap: 2,
            },
        },
    ],
}

In this configuration:

  • group.padding adjusts the padding between the edge of the group, its title, and the inner nodes
  • group.gap adjusts the gap between adjacent tiles where one or more nodes in the parent node are group nodes
  • tile.padding adjusts the padding between the edge of the tile and its labels
  • tile.gap adjusts the gap between adjacent tiles where all nodes in the parent node are leaf nodes

Hierarchy Levels Copy Link

Treemap Series supports multiple levels within a hierarchy.

Highlighting Copy Link

Treemaps leaf tiles are configured via tile.highlight, while group rectangles use group.highlight.

The leaf tiles support the following states:

  • highlightedItem – styles applied to the hovered tile.
  • unhighlightedItem – styles applied to other nodes within the same branch.
  • highlightedBranch – styles inherited by every node that shares the hovered root.
  • unhighlightedBranch – styles applied to nodes in other branches.

The groups support the following states:

  • highlightedItem – styles applied to the hovered group.
  • unhighlightedItem – styles applied to other groups.

When a group is hovered, its fillOpacity and strokeOpacity styles are inherited by its child leaf tiles.

{
    series: [
        {
            type: 'treemap',
            labelKey: 'title',
            sizeKey: 'total',
            group: {
                highlight: {
                    highlightedItem: { stroke: 'lightgreen', strokeWidth: 4 },
                    unhighlightedItem: { opacity: 0.1 },
                },
            },
            tile: {
                highlight: {
                    highlightedItem: { stroke: 'green' },
                    highlightedBranch: { strokeWidth: 2 },
                    unhighlightedBranch: { fill: 'grey' },
                },
            },
        },
    ],
}

In this configuration:

  • group.highlight only affects the group rectangles.
  • tile.highlight controls the tiles themselves. When a group is hovered, only the fillOpacity and strokeOpacity styles are inherited by its tiles.
  • Providing fill/stroke overrides allows highlight colours to take precedence over colour scales.

API Reference Copy Link