javascript - HighCharts: One Legend, Two Charts - Stack Overflow

admin2025-04-09  0

I have four different HighChart spline charts. All contain six series representing the six New England states. I want to click any legend and hide/show that series in all charts. I have tried legendclickitem but cannot get it to effect both charts. Is what i am asking possible, if so can you point me in the right direction, thanks.

Answer:

Using Paweł FusIn code and in order to keep a legend on each chart I used the following code. You can click on any legend item and it updates all charts.

plotOptions: {
series: {
events: {
legendItemClick: function(event) {
if (this.visible) {
$('#container1').highcharts().series[this.index].hide(); 
$('#container2').highcharts().series[this.index].hide(); 
$('#container3').highcharts().series[this.index].hide(); 
$('#container4').highcharts().series[this.index].hide(); 
} 
else {
$('#container1').highcharts().series[this.index].show(); 
$('#container2').highcharts().series[this.index].show(); 
$('#container3').highcharts().series[this.index].show(); 
$('#container4').highcharts().series[this.index].show();
}
return false;
}
}
}

I have four different HighChart spline charts. All contain six series representing the six New England states. I want to click any legend and hide/show that series in all charts. I have tried legendclickitem but cannot get it to effect both charts. Is what i am asking possible, if so can you point me in the right direction, thanks.

Answer:

Using Paweł FusIn code and in order to keep a legend on each chart I used the following code. You can click on any legend item and it updates all charts.

plotOptions: {
series: {
events: {
legendItemClick: function(event) {
if (this.visible) {
$('#container1').highcharts().series[this.index].hide(); 
$('#container2').highcharts().series[this.index].hide(); 
$('#container3').highcharts().series[this.index].hide(); 
$('#container4').highcharts().series[this.index].hide(); 
} 
else {
$('#container1').highcharts().series[this.index].show(); 
$('#container2').highcharts().series[this.index].show(); 
$('#container3').highcharts().series[this.index].show(); 
$('#container4').highcharts().series[this.index].show();
}
return false;
}
}
}
Share Improve this question edited Jun 23, 2014 at 16:49 user3767675 asked Jun 23, 2014 at 13:48 user3767675user3767675 331 silver badge4 bronze badges 2
  • There is a more recent answer here: stackoverflow./questions/15926516/… (By using the linkedTo property.) – Kev Commented Jun 20, 2017 at 13:37
  • I do not believe the more recent answer actually solves this problem. This question is about two actual chart objects and how to share a legend between them. Paweł Fus's solution below still works great. Using linkedTo only appears to link two series' in a single chart. – Pez Commented May 9, 2018 at 0:01
Add a ment  | 

1 Answer 1

Reset to default 6

It's possible, take a look: http://jsfiddle/teEQ3/

$('#container1').highcharts({
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    legend: {
        enabled: false
    },

    series: [{
        id: 'someId',
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]
});
$('#container2').highcharts({
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    plotOptions: {
        series: {
            events: {
                legendItemClick: function (event) {
                    var XYZ = $('#container1').highcharts(),
                        series = XYZ.get(this.options.id); //get corresponding series

                    if (series) {
                        if (this.visible) {
                            series.hide();
                        } else {
                            series.show();
                        }
                    }
                }
            }
        }
    },

    series: [{
        id: 'someId',
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
    }]
});

So the idea is to enable only in one chart legend, then in all respective charts hide corresponding series.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744202346a235858.html

最新回复(0)