javascript - Highcharts:带有情节的系列。一次只显示一个系列

标签 javascript highcharts toggle hide show

我在柱形图中为每个系列绘制了一条情节线。目前该系列仅在图表可见时显示,但我想添加点击图例项目显示该项目及其绘图线但隐藏所有其他系列/绘图线的功能。这是当前代码的 fiddle :https://jsfiddle.net/nhrmc/5d46bL2f/

fiddle 可以在这里找到:https://jsfiddle.net/nhrmc/5d46bL2f/

var chart = Highcharts.chart('container', {
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
plotLines: [{
  value: 50,
  color: 'red',
  width: 2,
  id: 'plot-line-1'
}]
},

series: [{
events: {
  show: function() {
    chart.yAxis[0].addPlotLine({
      value: 7,
      color: 'red',
      width: 2,
      id: 'plot-line-1'
    });
  },
  hide: function() {
    chart.yAxis[0].removePlotLine('plot-line-1')
  }
},
 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],
type: 'column',
}, {
events: {
  show: function() {
    chart.yAxis[0].addPlotLine({
      value: 75,
      color: 'blue',
      width: 2,
      id: 'plot-line-2'
    });
  },
  hide: function() {
    chart.yAxis[0].removePlotLine('plot-line-2')
  }
},
data: [20, 70, 100, 125, 140, 176.0, 132, 145, 21.4, 191, 96, 54],
type: 'column',
visible: false
}
]
});

我希望单击图例中的“系列 2”并将其与关联的情节线一起显示,并隐藏系列 1 及其关联的情节线。因此,只应显示一个系列/情节组合。

最佳答案

legendItemClick 回调函数中,您可以隐藏所有其他系列和情节:

plotOptions: {
    series: {
        events: {
            legendItemClick: function() {
                this.chart.series.forEach(function(s) {
                    if (s !== this && s.visible) {
                        s.hide();
                    }
                });

                return !this.visible ? true : false
            }
        }
    }
}

现场演示: https://jsfiddle.net/BlackLabel/os86px7v/

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

关于javascript - Highcharts:带有情节的系列。一次只显示一个系列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56295593/

相关文章:

javascript - 在 JavaScript 中声明一个 const 有什么意义

javascript - 如何在每个单元格中动态显示图表?

javascript - 不正确的 JSON 数据格式

html - 隐藏切换按钮内的复选框

jQuery 使用 cookie 切换表行

Javascript:切换功能不起作用

javascript - 使用 .call(this) 调用 SetInterval 方法仅调用一次

javascript - 我将如何使用 alexa-sdk 在我的 Alexa 技能中使用 http?

javascript - 画架.js : Browser compatibility when placing an image within a container and adding mouse interactions

javascript - 如何在固定柱形图(highcharts)中动态添加点放置和点填充