Aaronius
Aaronius

Reputation: 4953

Snapping vertical line cursor to data point in jqPlot

I have just started using jqPlot for a line chart with multiple series. It seems great.

I have added the Cursor plugin with the intention of showing a vertical line on the nearest data point on the x axis. In other words, it snaps to the nearest point. The Cursor plugin, however always shows the vertical cursor right where the mouse is.

It seems like I just want to "override" or replace moveLine to change the current functionality.

What's the most appropriate way of doing so?

It seems a little much to copy/past all of the cursor plugin just to modify a very small subset.

Thanks!

Upvotes: 3

Views: 1911

Answers (2)

supershwa
supershwa

Reputation: 41

Try a bar graph series on top of everything else that has alpha set to 0.

Upvotes: 0

AnthonyLeGovic
AnthonyLeGovic

Reputation: 2335

I know I'm a kind of archaeologist by edited this post but I think the following can be useful for someone (I hope).

I've made a piece of code which allow to draw a vertical line following the cursor and displaying a tooltip on the nearest point (according to the cursor). You can find a demo of it on this JSFiddle.

I also post the corresponding code below (only the part which calculate nearest point and display tooltip):

//Show nearest point's tooltip
$("#chart1").bind('jqplotMouseMove', function(ev, gridpos, datapos, neighbor, data){
var c_x = datapos.xaxis;
var index_x = -1;
var pos_index = 0;
var low = 0;
var high = data.data[0].length-1;
while(high - low > 1){
    var mid = Math.round((low+high)/2);
    var current = data.data[0][mid][0];
    if(current <= c_x)
        low = mid;
    else
        high = mid;
}
if(data.data[0][low][0] == c_x){
    high = low;
    index_x = high;
}else{
    var c_low = data.data[0][low][0];
    var c_high = data.data[0][high][0];
    if(Math.abs(c_low - c_x) < Math.abs(c_high - c_x)){
        index_x = low;
    }else{
        index_x = high;   
    }
}
//Display marker and tooltip
if(data.series[0].data[index_x]){
    var x = data.series[0].gridData[index_x][0];
    var y = data.series[0].gridData[index_x][1];
    var r = 5;
    var highlightCanvas = $(".jqplot-highlight-canvas")[0];
    var context = highlightCanvas.getContext('2d');
    context.clearRect(0,0,highlightCanvas.width,highlightCanvas.height);
    context.strokeStyle = 'rgba(47,164,255,1)';
    context.fillStyle = 'rgba(47,164,255,1)';
    context.beginPath();
    context.arc(x,y,r,0,Math.PI*2,true);
    context.closePath();
    context.stroke();
    context.fill();
    //Display tooltip on nearest point
    var highlightTooltip = $(".jqplot-highlighter-tooltip");
    var val_x = data.data[0][index_x][0];
    var val_y = data.data[0][index_x][1];
    highlightTooltip.html("X : "+val_x+"<br/>Y : "+val_y);
    highlightTooltip.css({'left': x+'px', 'top': (y-10)+'px', 'display': 'block'});           
}
});

Feel please to use it and to modify it as you need it.

Upvotes: 2

Related Questions