gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:15:45 26 4
gpt4 key购买 nike

我在柱形图中为每个系列绘制了一条情节线。目前该系列仅在图表可见时显示,但我想添加点击图例项目显示该项目及其绘图线但隐藏所有其他系列/绘图线的功能。这是当前代码的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com