Warm tip: This article is reproduced from stackoverflow.com, please click
highcharts

Show Series and colorAxis both in Legend

发布于 2020-03-27 10:17:01

Is it possible to have both colorAxis and series in the legend? http://jsfiddle.net/6k17dojn/ i see i can only show one at a time when I toggle this setting
colorAxis: { showInLegend: true, }

Questioner
user4772933
Viewed
73
ppotaczek 2019-07-03 21:25

Currently to show a basic legend with colorAxis, you need to add some code to Highcharts core. This plugin below allows you to add colorAxis to a legend if showInLegend property is set to false:

(function(H) {
    H.addEvent(H.Legend, 'afterGetAllItems', function(e) {
        var colorAxisItems = [],
            colorAxis = this.chart.colorAxis[0],
            i;

        if (colorAxis && colorAxis.options) {
            if (colorAxis.options.dataClasses) {
                colorAxisItems = colorAxis.getDataClassLegendSymbols();
            } else {
                colorAxisItems.push(colorAxis);
            }
        }

        i = colorAxisItems.length;
        while (i--) {
            e.allItems.unshift(colorAxisItems[i]);
        }
    });
}(Highcharts))

Live demo: http://jsfiddle.net/BlackLabel/hs1zeruy/

API Reference: https://api.highcharts.com/highcharts/colorAxis.showInLegend

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