简体   繁体   中英

Highcharts Treemap with multiple series: getting them all to show in legend

So I have 6 series I'm using in a single highcharts treemap chart where each series is it's own, smaller tree map. I can't get each of the names of the series to appear in the legend because the showInLegend property is a level above the data property.

I've tried two different configurations. First: series is set to my chartData variable, which looks like: [{ name: key, stack: key, id: key, showInLegend: true, color: colors[key], data: sliced }, {...}] And so forth. This gives me a single treemap chart with each series underneath the previous, aka they are stacked on top of each other.

If I combine them all into a single data array, I can get the proper chart format I'm looking for, where each individual series is grouped by color/parent and so forth. This was done using data-less data points, like so: { name, id, showInLegend, color }

and each of the other points then would be: { name parent value }

This large data array is then given to a series array that looks like: [{ type: 'treemap', layoutAlgorithm: 'squarified', data: dataArray from above, }]

Again, I want the chart to be multiple tree maps, not one jammed on top of the other. But i also need the legend to show all of those series' names

You can use unofficial legendType: 'point' property and remove unnecessary elements in afterGetAllItems legend event:

var H = Highcharts;

H.addEvent(H.Legend, 'afterGetAllItems', function(e) {
    e.allItems.splice(2, 6);
});

H.chart('container', {
    series: [{
        legendType: 'point',
        showInLegend: true,
        ...
    }]
});

Live demo: https://jsfiddle.net/BlackLabel/gp2qmvac/

API Reference: https://api.highcharts.com/highcharts/series.treemap.showInLegend

Docs: https://www.highcharts.com/docs/extending-highcharts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM