javascript - 任何可能为绘图带和绘图线高图表提供图例
问题描述
我需要为图带提供单个图例项,为样条高图中的图线提供多个图例项。参考这个例子:https ://jsfiddle.net/t98L5w7s/5/
xAxis: {
...,
plotBands: [{
from: Date.UTC(2018, 11, 25),
to: Date.UTC(2019, 1, 28),
color: 'rgba(68, 170, 213, .2)' //No I18N
}],
plotLines: [{
color: '#FF0000',
width: 2,
value: Date.UTC(2019, 00, 18)
}, {
color: '#FF1100',
width: 2,
value: Date.UTC(2019, 00, 21)
}, {
color: '#FF2200',
width: 2,
value: Date.UTC(2019, 00, 28)
}],
},
解决方案
最简单的解决方案是创建额外的空系列,它将以编程方式plotLines
与功能plotBands
相关联:legendItemClick
plotOptions: {
...,
series: {
allowPointSelect: false,
events: {
legendItemClick: function() {
var newPlotLines = [],
xAxis = this.chart.xAxis[0],
plotLinesIndex;
if (this.name === 'plot bands') {
if (this.visible) {
xAxis.update({
plotBands: []
});
} else {
xAxis.update({
plotBands: plotBands
});
}
} else if (H.isNumber(this.options.plotLinesIndex)) {
this.chart.series.forEach(function(s) {
plotLinesIndex = s.options.plotLinesIndex;
if (this !== s && H.isNumber(plotLinesIndex)) {
if (s.visible) {
newPlotLines.push(plotLines[plotLinesIndex])
}
} else if (this === s && !s.visible) {
newPlotLines.push(plotLines[plotLinesIndex])
}
}, this);
xAxis.update({
plotLines: newPlotLines
});
}
}
}
}
}
现场演示:https ://jsfiddle.net/BlackLabel/omn6fr2e/
API 参考:https ://api.highcharts.com/highcharts/series.line.events.legendItemClick
推荐阅读
- firebase - 使用 Firebase 的 JWT 令牌
- julia - 计算二项分布数据的可能性
- javascript - 如何搜索文本文件中的每一行并捕获以 DDMMM 格式开头的行?
- android - 没有 java.time 的 Android 中的日期/时间操作
- json - Json需要解析两次?
- python - how to interact with recaptcha audio button selenium python
- java-8 - RxJava objects assing from findAll are null
- machine-learning - Can someone explain why this scalar layer non-trainable?
- java - Using JasperFillManager.fillReport(jasperReport,parameters, dataSource), Can JasperReport be reused?
- arrays - filemaker api 传递数组对象