With a new event moved
of ChartCursor (added since 3.4.0) you can easily find out value at which your mouse currently is. This can be done with both XY and Serial charts.
To use this feature, you should add ChartCursor to the chart and listen for moved
event:
var chartCursor = new AmCharts.ChartCursor(); chartCursor.addListener("moved", handleMove); chart.addChartCursor(chartCursor);
The event moved
passes this object to event handler method:
{type:"moved", x:Number, y:Number, zooming:Boolean, chart:AmChart}
x
and y
are coordinates of the mouse, counting from top-left corner of plot area.
Most likely you are interested in value of the axis, not coordinate. You can use coordinateToValue(coordinate)
method to get it.
So the event handler function should look like this:
function handleMove(event){ var xValue = AmCharts.roundTo(xAxis.coordinateToValue(event.x), 2); var yValue = AmCharts.roundTo(yAxis.coordinateToValue(event.y), 2); console.log(xValue + ", " + yValue); }
As we mentioned, the same can be applied for serial chart - in case it's not rotated, use y
coordinate, otherwise use x
.