React ChartsTreemap Series
react logo
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

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

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

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

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: 'name',
        sizeKey: 'size',
        sizeName: 'Size',
    },
],

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 Scales

Tiles can also be coloured using a scale. This lets you add an extra dimension of information to your chart.

Colouring can be customised by the colorKey and colorRange values in the series options.

series: [
    {
        type: 'treemap',
        labelKey: 'name',
        sizeKey: 'size',
        sizeName: 'Size',
        colorKey: 'change',
        colorName: 'Change',
        colorRange: ['#43A047', '#FF5722'],
    },
],

In this configuration:

  • colorKey supplies numeric values for the colour scale
  • colorName sets the title that appears next to the colour value in tooltips
  • colorRange supplies the colours for the colour scale (optional)

Other Colours

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 colorRange is used, the fills and strokes arrays are ignored

Labels

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: 'name',
        secondaryLabelKey: 'size',
        sizeKey: 'total',
        sizeName: 'Total',
        group: {
            label: {
                fontSize: 18,
                spacing: 2,
            },
        },
        tile: {
            label: {
                fontSize: 32,
                minimumFontSize: 18,
                spacing: 12,
            },
            secondaryLabel: {
                formatter: (params) => formatSize(params.datum),
            },
        },
    },
],

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

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

Treemap Series supports multiple levels within a hierarchy.

Gradient Legend

The Gradient Legend aids in matching the colour coding of the Treemap Series to the underlying values, and is enabled by default.

series: [
    {
        type: 'treemap',
        colorKey: 'change',
        // ...
    },
],
gradientLegend: {
    enabled: true,
},

Position

By default the Gradient Legend is placed at the bottom of the chart. Use the position option to change this.

gradientLegend: {
   position: 'right',
   reverseOrder: true
},

In the above example, reverseOrder is used to change the direction of the Gradient Legend to display the values from top to bottom.

Size

gradientLegend: {
    gradient: {
        thickness: 50,
        preferredLength: 400,
    },
},

In the above configuration:

  • thickness controls the thickness (or width) of the gradient bar.
  • preferredLength sets the initial length of the gradient bar. It is only preferred, as the Gradient Legend is constrained by the container edges.

Labels

It is possible to customise the font, colour and padding of the labels by using the scale options.

gradientLegend: {
    scale: {
        label: {
            fontSize: 20,
            fontStyle: 'italic',
            fontWeight: 'bold',
            fontFamily: 'serif',
            color: 'red',
        },
        padding: 20,
    },
},

API Reference