Search code examples
eventsamchartsamcharts4

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


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.


Solution

  • 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)
    })