Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a click\hit event for LineSeries in amcharts 4?

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); 
like image 563
Deepak Avatar asked Jul 23 '18 10:07

Deepak


People also ask

How to handle double click events on a chart?

Just add the listener and be done with it: chart.addListener ("clickGraphItem", function (event) { // click event occurred // let's do something cool }); However, sometimes you need to do something else on double click. Handling double click events in JavaScript is problematic, unreliable and it differs from browser to browser.

What are event listeners in amCharts?

Event listeners are custom functions that are invoked whenever some action or event, like for instance an object is clicked, or series is hidden, or axis is zoomed, is triggered. Every object in amCharts 4 has a property events which is an "event dispatcher".

What is event dispatcher in amCharts 4?

Every object in amCharts 4 has a property events which is an "event dispatcher". Event dispatcher is responsible for registering and de-registering custom functions, as well as executing them whenever certain event occurs in the dispatchers parent object.

What is callback function in amCharts?

A callback function that will be called whenever event occurs. A scope in which callback function will be executed. Here's an example that attaches click event to a column in a Column series: amCharts 4 uses "hit" event to indicated universal click or touch event.


3 Answers

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

like image 140
xorspark Avatar answered Nov 15 '22 08:11

xorspark


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

like image 22
zeroin Avatar answered Nov 15 '22 08:11

zeroin


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);
like image 40
MrBen Avatar answered Nov 15 '22 10:11

MrBen