javascript - 修复 Highchart 图表上的最小/最大值标志
问题描述
我有一个 xAxis 为日期时间的 Highchart 图,在 yAxis 上我有不同的数据。我想在最小值和最大值上放置标记,但是每个数据系列都会显示标志,如何修复它。谢谢。
我想像这样显示 Highchart
这是我的代码:
var newArr = [];
Highcharts.chart('container', {
chart: {
type: 'spline',
events: {
load: function(){
var chart = this;
for(var i = 0; i < chart.series[0].processedXData.length; i++){
newArr.push({
y: chart.series[0].processedYData[i],
x: chart.series[0].processedXData[i]
})
var maxY = Math.max.apply(Math, newArr.map(function(o){
return o.y
}));
var minY = Math.min.apply(Math, newArr.map(function (o) {
return o.y;
}));
maxIndex = newArr.findIndex(x => x.y == maxY);
minIndex = newArr.findIndex(x => x.y == minY);
this.addSeries({
type: 'flags',
data: [{
x: newArr[maxIndex].x,
y: newArr[maxIndex].y,
title: 'Max:' + newArr[maxIndex].y,
}, {
x: newArr[minIndex].x,
y: newArr[minIndex].y,
title: 'Min: ' + newArr[minIndex].y,
}],
});
}
}
}
},
title: {
text: 'Steps by Day'
},
xAxis: {
type: 'datetime',
tickInterval: 3600000
},
yAxis: {
title: {
text: 'Steps'
},
},
series: [{
name: 'Steps',
data: [[1560297675000, 4.0], [1560300046000, 4.0], [1560302158000, 1.0], [1560302865000, 1.0], [1560303544000, 3.0], [1560305303000, 11.0], [1560305640000, 10.0], [1560306856000, 17.0], [1560307167000, 10.0], [1560308973000, 24.0], [1560309641000, 12.0], [1560309941000, 22.0], [1560310243000, 6.0], [1560310886000, 5.0], [1560312466000, 19.0], [1560313206000, 155.0], [1560314359000, 26.0], [1560316400000, 343.0], [1560318218000, 64.0], [1560319682000, 353.0], [1560325074000, 96.0], [1560326743000, 1037.0]]
}]});
解决方案
您只需要添加一个flag
系列:
chart: {
type: 'spline',
events: {
load: function() {
var series = this.series[0],
yData = series.yData,
min = Math.min(...yData),
max = Math.max(...yData),
x1 = series.points[yData.indexOf(min)].x,
x2 = series.points[yData.indexOf(max)].x;
this.addSeries({
type: 'flags',
data: [{
x: x1,
y: min,
title: 'Min: ' + min
}, {
x: x2,
y: max,
title: 'Max:' + max
}]
});
}
}
}
现场演示:http: //jsfiddle.net/BlackLabel/wdtxbnrj/
推荐阅读
- excel - 从 xls 导入范围
- jquery - 关闭时间必须大于打开时间
- react-native - usestate 挂钩与计时器在 useeffect 挂钩内无法正常工作
- mysql - MySQL LOAD XML LOCAL INFILE:忽略特定标签
- google-cloud-platform - 云运行服务调用
- laravel - Laravel 作业正在处理但没有做任何事情
- c++ - SSD1306 显示屏闪烁
- vb.net - 使用包含常量大小数组的结构调用非托管函数
- java - 如何使用 JavaParser 将注释放在新行上?
- git - 如何在变基之前恢复一个 git 提交?