Series - show/hide all EXCEPT selected series - Javascript highcharts

Javascript examples for highcharts:Chart Configuration

Description

Series - show/hide all EXCEPT selected series

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>ElementStacks</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://code.jquery.com/jquery-1.4.2.js"></script> 
      <script type="text/javascript" src="http://highcharts.com/js/testing.js"></script> 
      <script type="text/javascript">
    $(function(){/*ww  w . ja v  a  2s. c o m*/
var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container'
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    plotOptions: {
        series: {
            events: {
                  legendItemClick: function(e) {
                        var seriesIndex = this.index;
                        var series = this.chart.series;
                              var allElseVisible = series.every(
                          s => (s.index == seriesIndex ? true : s.visible),
                        );
                        var allElseNotVisible = series.every(
                          s => (s.index == seriesIndex ? true : !s.visible),
                        );
                        if (allElseVisible || allElseNotVisible) {
                             series.map(s => {
                                  if (s.index != seriesIndex) {
                                   s.visible ? s.hide() : s.show();
                                  }
                             });
                        } else {
                               return true;
                        }
                        return false; // overrides default behavior
                   },
            }
        }
    },
    series:[{name: 'apples',
            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]},
    {name:'pears',
    data: [19.9, 81.5, 96.4, 119.2, 124.0, 166.0, 155.6, 138.5, 116.4, 144.1, 95.6, 54.4]},
           {name:'oranges',
    data: [119.9, 181.5, 46.4, 219.2, 24.0, 66.0, 255.6, 238.5, 16.4, 44.1, 95.6, 54.4]}
           ]
});
    });

      </script> 
   </head> 
   <body> 
      <script src="https://code.highcharts.com/highcharts.js"></script> 
      <script src="https://code.highcharts.com/modules/exporting.js"></script> 
      <div id="container" style="height: 400px"></div>  
   </body>
</html>

Related Tutorials