Home Forums Chart Support How to redisplay crosshairs across renders Reply To: How to redisplay crosshairs across renders



What I would like to do is automatically display the crosshairs wherever the cursor is after the chart has been rendered without requiring the cursor to move first. Is there any way I can do this?

You can display the crosshairs at the last cursor position after rendering the chart by storing the last cursor position with respective to x and y axis in mousemove event callback function. After the chart render method is called, you can display the crosshair at last cursor position using showAt method. Please take a look at this below code snippet for the same.

setInterval(() => {
  	//Check if crosshair positions is within plot area
    if( chart.axisX[0].bounds.x1 <= crossHairPositionX && chart.axisX[0].bounds.x2 >= crossHairPositionX && chart.axisY[0].bounds.y1 <= crossHairPositionY && chart.axisY[0].bounds.y2 >= crossHairPositionY) {
      if(crossHairPositionX != null && crossHairPositionY != null) {
  }, 1000);

  $(".canvasjs-chart-canvas").last().on("mousemove", function(e) {
    var parentOffset = $(this).parent().offset();
    var relX = e.pageX - parentOffset.left;
    crossHairPositionX = relX;
    var relY = e.pageY - parentOffset.top;
    crossHairPositionY = relY;

Also check out this updated JSFiddle for complete working code.

Retaining crosshair value after chart render is called

The last note is that for whatever reason, the showAt method does not seem to work for the Y2 axis at all. I don’t understand why but if you look at this jsfiddle, which is the same in every way from the one above except I’m using the Y2 axis, you’ll see that the crosshair will not display for the Y2 axis using the showAt method

Thanks for reporting the use-case. It seems like a bug & we will fix it in our upcoming versions.

Manoj Mohan
Team CanvasJS