Tuesday, 27 June 2017

javascript - Highcharts: Is it possible to show Sunburst legend similar to Pie chart legend?

In my opinion the answer is no.


Please refer to this demo: http://jsfiddle.net/kkulig/u3p1usz9/


I tried to implement this functionality by setting legendType = 'point' (not docummented in the API, but it works) and overwriting H.Legend.prototype.getAllItems, but it seems that hiding points is not supported for sunburst. There's no method that will do it - check out the console.log output. Switching visibility of the point by using visible property doesn't work either. Legend behaves properly, but there's no effect on the plot area.


Workaround


This simple example shows how to mimic desired legend behavior: http://jsfiddle.net/kkulig/kn10kb7L/


First I added additional two series that have no data:


{
type: 'line',
name: 'p1',
color: 'blue'
}, {
type: 'line',
name: 'p2',
color: 'blue'
}

The color of the legend item markers needs to be handled manually by setting the color of the 'fake' series.
I created visible flag for every leaf for controlling its visibility.
Then I used their legendItemClick callback function to filter the full data set and perform setData on the first series using the filtered data.


    legendItemClick: function(e) {
series = this;
data.forEach(function(leaf) {
if (leaf.id === series.name || leaf.parent === series.name) {
leaf.visible = !leaf.visible;
}
});
this.chart.series[0].setData(data.filter((leaf) => leaf.visible));
}

API reference: https://api.highcharts.com/highcharts/plotOptions.sunburst.point.events.legendItemClick




If you think that hiding points should be implemented in sunburst series you can share this idea here: https://highcharts.uservoice.com/forums/55896-highcharts-javascript-api




Update


If you want an animation to happen use addPoint and removePoint instead of setData.


API references:

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...