How to registery event handlers on the individual categories of a AMCharts v4 CategoryAxis

59 views Asked by At

I have seen https://www.amcharts.com/docs/v4/concepts/event-listeners/ and https://www.amcharts.com/docs/v4/reference/categoryaxis/#Events

categoryAxis.events.on('hit', function (ev) {
      console.log('clicked on ', ev.target)
}, this)

works. However, this returns the complete CategoryAxis. I would like to distinguish on which catgeory the user clicked.

e.g. categoryAxis.category.template.events.on('hit', function (ev) does not exist.

1

There are 1 answers

0
xorspark On BEST ANSWER

You need to add the hit event listener on the axis renderer's label template in order to capture the category label that was clicked:

categoryAxis.renderer.labels.template.events.on('hit', function(ev) {
  alert(ev.target.dataItem.category)
})