简体   繁体   English

编辑 Highcharts 图例上的点击事件

[英]Edit click event on Highcharts Legend

I'm trying to edit the click event for a Highcharts legend item.我正在尝试编辑 Highcharts 图例项目的点击事件。 The current setup allows that on click of legend item, it toggles true/false.当前设置允许在单击图例项时切换真/假。 Instead, I want on click of legend item to set visibility of all other series to false, except the item that was clicked (essentially the inverse of what it does now).相反,我希望单击图例项以将所有其他系列的可见性设置为 false,但单击的项目除外(基本上与现在的操作相反)。

I've tried build a JSfiddle to just toggle all series with no luck.我已经尝试构建一个 JSfiddle 来切换所有系列,但没有运气。 See link链接

Key section is here:关键部分在这里:

  legendItemClick: function() {
    var chart = Highcharts.chart;
    var series = chart.series;
    for (item in series) {
        if (series[item].visible) {
            series[item].hide();
        } else {
            series[item].show();
        }    
    };        
    return false
  }

On click of a legend item I try to grab the chart and all series, then cycle through each of them to toggle visibility.单击图例项时,我尝试抓取图表和所有系列,然后循环浏览它们以切换可见性。

You could try something like this:你可以尝试这样的事情:

var mychart = Highcharts.chart('container', {
  series: [{
    name: 'Installation',
    data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175],
    events: {
      legendItemClick: function() {
        return false
      }
    }
  }, {
    name: 'Manufacturing',
    data: [24916, 24064, 29742, 29851, 32490, 30282, 38121, 40434],
    events: {
      legendItemClick: function() {
        var numSeries = mychart.series.length;
        for (var i=0 ; i<numSeries ; i++) {
          if (mychart.series[i] != this) {
              if (mychart.series[i].visible == true) {
                mychart.series[i].hide();
              } else {
                mychart.series[i].show();
              }
          }
        }
        return false
      }
    }
  }, {
    name: 'Sales & Distribution',
    data: [11744, 17722, 16005, 19771, 20185, 24377, 32147, 39387]
  }]
});

http://jsfiddle.net/Lamjdhy3/1/ http://jsfiddle.net/Lamjdhy3/1/

Here is another approach how you can achieve wanted result for all series using the plotOptions.series.events.legendItemClick callback.这是另一种方法,您可以使用plotOptions.series.events.legendItemClick回调为所有系列实现想要的结果。

Demo: http://jsfiddle.net/BlackLabel/vsj96c5x/演示: http://jsfiddle.net/BlackLabel/vsj96c5x/

  plotOptions: {
    series: {
      events: {
        legendItemClick(e) {
          e.preventDefault();
          let chart = this.chart;

          chart.series.forEach(s => {
            if (s !== this && s.visible) {
              s.hide();
            } else {
              s.show();
            }
          })
        }
      }
    }
  },

API: https://api.highcharts.com/highcharts/plotOptions.series.events.legendItemClick API: https://api.highcharts.com/highcharts/plotOptions.series.events.legendItemClick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM