Draw Crosshairs / Tooltip on Mouse Position Instead of Snapping to Data Point - Javascript highcharts

Javascript examples for highcharts:Crosshair Chart

Description

Draw Crosshairs / Tooltip on Mouse Position Instead of Snapping to Data Point

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>Crosshairs</title> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
      <script type="text/javascript" src="https://code.highcharts.com/highcharts.src.js"></script> 
      <script type="text/javascript">
$(function () {/*from ww  w . ja v a  2s  .c o  m*/
    function addChartCrosshairs() {
        var chart = this;
        var crosshairX = chart.renderer.path(['M', chart.plotLeft, 0, 'L', chart.plotLeft + chart.plotWidth, 0]).attr({
            stroke: 'black',
                'stroke-width': 1,
            zIndex: 0
        }).add()
            .toFront()
            .hide();
        var crosshairY = chart.renderer.path(['M', 0, chart.plotTop, 'L', 0, chart.plotTop + chart.plotHeight]).attr({
            stroke: 'black',
                'stroke-width': 1,
            zIndex: 0
        }).add()
            .toFront()
            .hide();
        $(chart.container).mousemove(function (event) {
            crosshairX.translate(0, event.offsetY);
            crosshairY.translate(event.offsetX, 0);
            if (event.offsetX > chart.plotLeft && event.offsetX < chart.plotLeft + chart.plotWidth &&
                event.offsetY > chart.plotTop && event.offsetY < chart.plotTop + chart.plotHeight) {
                crosshairX.show();
                crosshairY.show();
            } else { 
                crosshairX.hide();
                crosshairY.hide();
            }
        });
    }
    $('#container').highcharts({
        chart: {
            events: {
                load: addChartCrosshairs
            }
        },
        tooltip: {
            shared: true,
            followPointer: true
        },
        series: [{
            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
        }]
    });
});

      </script> 
   </head> 
   <body> 
      <div id="container" style="height: 400px"></div>
      http://jsfiddle.net/MhcqF/#run  
   </body>
</html>

Related Tutorials