我正在 HighCharts 设置上编写一个点击事件,该设置有 2 个不同的系列,一个在另一个之上。我希望事件始终在顶部系列上显示一条消息,即使他们单击底部系列也是如此。
我使用的代码如下:
chart = new Highcharts.StockChart({
chart: {
renderTo: 'container',
events: {
click: function(event) {
// perform actions
this.chart.series[1].setData([{
x: event.point.x,
y: ??
}]);
}
...
我想要做的是根据他们在图表上单击的位置的 X 值使用第一个系列中的 Y 值。如何根据这些信息查找 Y 值?
基本上,如果我点击图表上的一个点,如何仅根据它的 X 值在图表上的一系列点中查找该点。
最佳答案
系列对象有一个点对象数组,它基本上包含该系列中的所有点,一旦从事件对象中获取给定系列的 xValue,您就可以迭代这些点以找到最接近的x
并返回其对应的y
值
代码片段:
function getYValue(chartObj,seriesIndex,xValue){
var yValue=null;
var points=chartObj.series[seriesIndex].points;
for(var i=0;i<points.length;i++){
if(points[i].x>=xValue)break;
yValue=points[i].y;
}
return yValue;
}
用法:
click:function(evt){
var seriesIndex=1;
var xAxisIndex=0;
// All you need is the index of the series that you want
// And the index of the xAxis bound to that series
// default to 0 if you have only one series
alert(getYValue(this,seriesIndex,evt.xAxis[xAxisIndex].value));
}
fiddle @ http://jsfiddle.net/jugal/3MVGF/
关于HighCharts:根据另一个系列的 X 值获取一个系列中的 Y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12014412/