简体   繁体   中英

Highcharts - how to toggle datalabels via js?

I would like to use Highcharts in such a way where clicking on a legend item enables datalabels for that particular category. Is this possible? I have tried the following to enable datalabels with no luck:

chart.series[0].data.dataLabels.enabled = true;

Here's a jsfiddle I am working with: http://jsfiddle.net/MXZgj/3/ (please search for legendItemClick to see where the applicable code is).

Thank you for your help!

这有效:

 chart.series[0].update({ dataLabels: { enabled:true }})

I ended up having to iterate over every point in the series and enable each individual data label.

series: {
    events: {
        legendItemClick: function(event) {                   
            var selected = this.index;
            var allSeries = this.chart.series;

            $.each(allSeries, function(index, series) {                
                if (selected == index) {
                    $.each(series.points, function(i, point) {
                        point.update({
                            dataLabels: {
                                enabled: true
                            }
                        });
                    });
                } else {
                    $.each(series.points, function(i, point) {
                        point.update({
                            dataLabels: {
                                enabled: false
                            }
                        });
                    });
                }
            });

            return false;
        }
    }
}

Could use a bit of code cleanup, but works great as is.

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