alex37
alex37

Reputation: 21

How to get coordinates of a point when hovering over a chart in AmCharts 5?

I have this series:

 var series = chart.series.push(
      am5xy.SmoothedXLineSeries.new(am5root, {
        xAxis: xAxis,
        yAxis: yAxis,
        valueYField: "height",
        valueXField: "number",
        interactive: true,
        interactiveChildren: true,
        tooltip: am5.Tooltip.new(am5root, {
          labelText:"{valueX}, {valueY}",
          pointerOrientation:"horizontal"
        })
      })
    );

and this event:

 series.events.on("pointerover", function(ev) {
         let xPosition = ev.point.x;
         let yPosition = ev.point.y;
          
        
    });

but the variables xPosition and yPosition contain the coordinates of the cursor, not the coordinates of the point on the graph. How to get coordinates of a points on the graph?

 series.events.on("pointerover", function(ev) {
         let xPosition = ev.point.x;
         let yPosition = ev.point.y;
          
        
    });

Upvotes: 1

Views: 31

Answers (1)

alex37
alex37

Reputation: 21

I got the result I needed this way:

series.events.on("globalpointermove", function(ev) {
    if(typeof ev.target.getTooltip() === 'undefined' || typeof ev.target.getTooltip().dataItem==='undefined' || typeof ev.target.getTooltip().dataItem.dataContext === 'undefined')  { return; }
    else {
        if(activeCharts){
            let point = ev.target.getTooltip().dataItem.dataContext;
            
        }
    }
});

Upvotes: 1

Related Questions