I'm using vis.js to diplay some dataset like on a 2dgraph :
var data_lat=[
{x:'2016-02-05T07:58:49.257',y:48.66081132,group:0},
{x:'2016-02-05T07:58:51.257',y:48.66081132,group:0},
...
I have put a draggable cursor on that graph with
graph_lat.addCustomTime(time[0].t, '1');
and I would ike to display the curent value of y depending of the position of my cursor.
I've looked though vis.js doc but I didn't find anything
//EDIT TO ANSWER//
This is the function i created to get the y value BUT : only for array with only one group in it
function closest (time, arr) {
var mid, time_mid;
var lo = 0;
var hi = arr.length - 1;
var time_number=moment(time).format('x');
//console.log(time_number);
while (hi - lo > 1) {
mid = Math.floor ((lo + hi) / 2);
time_mid=moment(arr[mid].x).format('x');
if (time_mid < time_number) {
lo = mid;
} else {
hi = mid;
}
}
if (time_number - moment(arr[lo].x).format('x') <= moment(arr[hi].x).format('x') - time_number) {
return arr[lo].y;
}
return arr[hi].y;
}
and here the one for array with many group (like many y
for one x
value) like this array :
{x:'2016-02-05T07:58:49.257',y:0,group:8},
{x:'2016-02-05T07:58:49.257',y:0,group:9},
{x:'2016-02-05T07:58:49.257',y:0,group:10},
{x:'2016-02-05T07:58:49.257',y:0,group:11},
{x:'2016-02-05T07:58:51.257',y:9,group:8},
{x:'2016-02-05T07:58:51.257',y:6,group:9},
{x:'2016-02-05T07:58:51.257',y:0,group:10},
{x:'2016-02-05T07:58:51.257',y:15,group:11},
{x:'2016-02-05T07:58:53.257',y:9,group:8}
function closest_multi_group (time, arr, groupID) {
var mid, time_mid;
var lo = 0;
var hi = arr.length - 1;
var time_number=moment(time).format('x');
//console.log(time_number);
while (hi - lo > 1) {
mid = Math.floor ((lo + hi) / 2);
time_mid=moment(arr[mid].x).format('x');
if (time_mid < time_number) {
lo = mid;
} else {
hi = mid;
}
}
console.log((time_number - moment(arr[lo].x).format('x'))+'<='+(moment(arr[hi].x).format('x') - time_number));
if (time_number - moment(arr[lo].x).format('x') <= moment(arr[hi].x).format('x') - time_number) {
console.log(arr[lo]);
if(arr[lo+1].group < arr[lo].group){
while(arr[lo].group!=groupID){
lo--;
console.log('lo-- '+lo);
}
}
else{
while(arr[lo].group!=groupID){
lo++;
console.log('lo++ '+lo);
}
}
return arr[lo].y;
}
if(arr[hi-1].group > arr[hi].group){
console.log(arr[hi]);
while(arr[hi].group!=groupID){
hi++;
console.log('hi++ '+hi);
}
}
else{
console.log(arr[hi]);
while(arr[hi].group!=groupID){
hi--;
console.log('hi-- '+hi);
}
}
return arr[hi].y;
}
The event timechange
gives you the current time of the custom time bar when being dragged. That is your x
. With this time, you can loop over your data_lat
to find the data point closest to x
and display the corresponding y
value somewhere.