我在HighCharts设置上写了一个点击事件,它有两个不同的系列,一个在另一个之上.我希望事件始终在顶部系列上显示一条消息,即使它们单击底部系列也是如此.
我正在使用以下代码:
chart = new Highcharts.StockChart({
chart: {
renderTo: 'container',
events: {
click: function(event) {
// perform actions
this.chart.series[1].setData([{
x: event.point.x,
y: ??
}]);
}
...
Run Code Online (Sandbox Code Playgroud)
我想要做的是根据他们在图表上点击的位置使用第一系列的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;
}
Run Code Online (Sandbox Code Playgroud)
用法:
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));
}
Run Code Online (Sandbox Code Playgroud)
小提琴@ http://jsfiddle.net/jugal/3MVGF/