Search code examples
dygraphs

How do I get the current x and y position of the mouse cursor in a Dygraph


I want to retrieve the x and y of the point over the mouse pointer. How can I do that? The callbacks are always about the points, and I may be with the cursor over a free region.


Solution

  • I've got it

    function getElementTop ( Elem ) 
    { 
     var elem;
    
    if ( document.getElementById ) 
    {   
        elem = document.getElementById ( Elem );
    } 
    else if ( document.all ) 
    {
        elem = document.all[Elem];
    }           
    
    yPos = elem.offsetTop;
    tempEl = elem.offsetParent;
    
    while ( tempEl != null ) 
    {
        yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }  
    
    return yPos;
    }   
    
    function getElementLeft ( Elem ) 
    {
    var elem;
    
    if ( document.getElementById ) 
    {
        var elem = document.getElementById ( Elem );
    } 
    else if ( document.all )
    {
        var elem = document.all[Elem];
    }           
    
    xPos = elem.offsetLeft;
    tempEl = elem.offsetParent;         
    
    while ( tempEl != null ) 
    {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }           
    return xPos;
    }
    
    
    var graphOnmousemove = function(arg,evt) {
    console.log(evt);
    var x = evt.pageX - getElementLeft("graph_data_div2");
    var y = evt.pageY - getElementTop("graph_data_div2");
    x = livePlotVtx2.toDataXCoord(x);
    y = livePlotVtx2.toDataYCoord(y);
    if (Ext.getCmp("graphTooltip") && Ext.getCmp("graphTooltip").body) { 
        Ext.getCmp("graphTooltip").body.dom.innerHTML = x.toFixed(2) + "," + y.toFixed(2);
        Ext.getCmp("graphTooltip").show();
    }
    };
    

    then I added the onmousemove event to the div onmousemove(this, event)