5

I want to know how to get the X\Y item details of a clicked line item in amcharts 4.

I have the code here: https://stackblitz.com/edit/angular-playground-3qpqlq

series2.segments.template.events.on("hit", (ev) => {
alert('line clicked');//this gets triggered
//but how to i get the line item details here, like X axis and Y axis 
//value of the clicked point of the line?

}, this); 
Deepak
  • 1,038
  • 4
  • 18
  • 31

3 Answers3

15

LineSeries data items aren't as straightforward to get from the hit event as columns are. You have to look at the event's target.dataItem.component.tooltipDataItem.dataContext object to get at the clicked bullet's information:

series2.segments.template.interactionsEnabled = true;
series2.segments.template.events.on(
  "hit",
  ev => {
    var item = ev.target.dataItem.component.tooltipDataItem.dataContext;
    alert("line clicked on: " + item.country + ": " + item.marketing);
  },
  this
);

Codepen

xorspark
  • 15,749
  • 2
  • 29
  • 38
3

You can do it without bullets too. Just add Cursor to the chart, listen to "hit" event and series.tooltipDataItem is the one cursor is currently hovering.

chart.cursor = new am4charts.XYCursor();
chart.events.on("hit", function(){
  console.log(series.tooltipDataItem);
})

https://codepen.io/team/amcharts/pen/de5de2f65752517c07afb4ffe7e110ef

zeroin
  • 5,863
  • 6
  • 31
  • 42
2

Maybe things have improved since because you can now access this via

bullet.events.on('over', (ev: any) => {
                    const val = ev.target.dataItem.dataContext.value;
                    this.dosomethingwith(val)
                }, this);
MrBen
  • 41
  • 4