dan-klasson
dan-klasson

Reputation: 14230

Highcharts: Get visibility of series after legendItemClick

I have a chart with multiple series which I would like to modify the options of, if two of the series have been disabled by clicking on the legend.

The following won't work as visible has the value of the state before it was clicked. Is there another way to do what I am trying to accomplish below?

plotOptions: {
    series: {
        events: {
            legendItemClick: function(event) {
                if(this.yAxis.series[0].visible && this.yAxis.series[1].visible) {
                    // do some action
                }
            }
        }
    }
},

Upvotes: 6

Views: 3656

Answers (1)

Paweł Fus
Paweł Fus

Reputation: 45079

You can get this behavior a little modyfing your function:

plotOptions: {
  series: {
    events: {
      legendItemClick: function(event) {
          var series = this.yAxis.series,
              seriesLen = series.length,
              visible = this.visible ? 1 : -1; 
              // +1 when visible series, because it will be changed after that callback

          for(var i = 0; i < seriesLen; i++) {
            if(!series[i].visible) {
              visible++;
            }
          }
          if(visible >= 2){
            //do some action
          }
      }
    }
  }
},

Upvotes: 7

Related Questions