Javascript 如何在Highcharts中获得一个点的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8507673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get index of a point in Highcharts?
提问by Morteza
I'm using "Time data with irregular intervals" chart of Highcharts. As you know when mouse moves over points of line the formatter function runs and shows some information. I want to know index of the point that mouse moves over it. So if mouse moves over first point of the line, tooltip shows "1" and the second point shows "2" and so on. thnx.
我正在使用 Highcharts 的“不规则间隔时间数据”图表。如您所知,当鼠标移过线点时,格式化程序功能会运行并显示一些信息。我想知道鼠标移过它的点的索引。因此,如果鼠标移动到线条的第一个点上,工具提示显示“1”,第二个点显示“2”,依此类推。谢谢。
回答by Edgar
This worked for me using v2.2:
这对我使用 v2.2 有用:
this.series.data.indexOf( this.point )
回答by eolsson
One way is to pre-process the data to contain a property with the index. In the Snow-depth example you could do a preparation like this:
一种方法是预处理数据以包含具有索引的属性。在 Snow-depth 示例中,您可以进行如下准备工作:
function prepare(dataArray) {
return dataArray.map(function (item, index) {
return {x: item[0], y: item[1], myIndex: index};
});
};
to convert the array of [x, y]
to be an object like { x: x, y: y, myIndex: i}
. Then its easy to pick up that index in the formatter with:
将 的数组转换为[x, y]
像{ x: x, y: y, myIndex: i}
. 然后很容易在格式化程序中获取该索引:
formatter: function() {
return 'point ' + this.point.myIndex;
}
Example on jsfiddle
jsfiddle示例
回答by user789148
For the record your can do it directly in a nice way
为了记录,您可以直接以一种很好的方式进行
It is store in:
它存储在:
this.points[0].point.x
回答by Saif
If you have access to your point then you can simple access,
this.point.index
or simply this.index
if this
refers to the point it self
to get access to its index,
如果你有机会到你的观点,那么你可以简单的访问,
this.point.index
或者干脆this.index
如果this
指的是点它自身以访问它的索引,
In my case I always use this because its simpler then @Edgar solution, which is also great.
就我而言,我总是使用它,因为它比 @Edgar 解决方案更简单,这也很棒。
回答by codeape
Since the data is sorted you can use a binary search.
由于数据已排序,您可以使用二分搜索。
A binary search should perform well even for large number of points (from the wikipedia article: "For example, to search a list of one million items takes as many as one million iterations with linear search, but never more than twenty iterations with binary search."
即使对于大量点,二分搜索也应该表现良好(来自维基百科文章:“例如,使用线性搜索搜索一百万个项目的列表需要多达一百万次迭代,但使用二分搜索不会超过二十次迭代.”
Example:
例子:
var bsComparator = function(a, b) {
if (a.x < b.x) { return -1; }
if (a.x > b.x) { return 1; }
return 0;
};
var binarySearch = function(series_data, point) {
var low = 0, high = series_data.length - 1,
i, comparison;
while (low <= high) {
i = Math.floor((low + high) / 2);
comparison = bsComparator(series_data[i], point);
if (comparison < 0) { low = i + 1; continue; }
if (comparison > 0) { high = i - 1; continue; }
return i;
}
return null;
};
tooltip: {
formatter: function() {
var pointIndex = binarySearch(this.series.data, this.point);
return "Point index: " + pointIndex;
}
}
(the binarySearch function above is inspired by http://www.dweebd.com/javascript/binary-search-an-array-in-javascript/)
(上面的 binarySearch 函数的灵感来自http://www.dweebd.com/javascript/binary-search-an-array-in-javascript/)
回答by danb201268326
Sounds like you only need the xAxis value (i.e. time). Use:
this.xData.indexOf(point.x)
听起来您只需要 xAxis 值(即时间)。用:
this.xData.indexOf(point.x)
this.points will be grouped in larger series so would need a deeper search through points[0]...points[n].
this.points 将被分组在更大的系列中,因此需要通过 points[0]...points[n] 进行更深入的搜索。
回答by coderama
This is all that worked for me on Highstock JS v4.2.4:
这就是在 Highstock JS v4.2.4 上对我有用的全部内容:
var index = this.points[0].point.dataGroup.start;
回答by Jamiec
This is about as hacky as it comes, and will get slow as hell with alot of points, but it'll work. The general idea is to look through all the points in the series' data until you find the one matching the current point:
这就像它来的那样笨拙,并且会因为很多点而变得很慢,但它会起作用。一般的想法是查看系列数据中的所有点,直到找到与当前点匹配的点:
tooltip: {
formatter: function() {
for(var i=0;i<this.series.data.length;i++){
var item = this.series.data[i];
if(item.x == this.x && item.y == this.y)
return 'point ' + i;
}
return 'not found'
}
}
Live example: http://jsfiddle.net/Fuv4h/
现场示例:http: //jsfiddle.net/Fuv4h/