Rapster
Rapster

Reputation: 524

Retrieving object from TreeMap

I've been working with few charts of amCharts 4, and everytime I struggled to subscribe to the event that returns the "selected/clicked" element. I don't know what I've been missing from the doc, but for example, I need to retrieve the selected item from "hit" event but i can't find anywhere (here is a simple i'd like to try this on https://codepen.io/team/amcharts/pen/erojQb)

var chart = am4core.create("chartdiv", am4charts.TreeMap);
chart.data = [{
  "name": "First",
  "value": 190
}, {
  "name": "Second",
  "value": 289
}, {
  "name": "Third",
  "value": 635
}, {
  "name": "Fourth",
  "value": 732
}, {
  "name": "Fifth",
  "value": 835
}];

/* Set color step */
chart.colors.step = 2;

/* Define data fields */
chart.dataFields.value = "value";
chart.dataFields.name = "name";

I tried this:

chart.seriesTemplates.template.columns.events.on('hit', function(ev) {
  console.log('mlkmlz');
});

but not called, and this:

chart.seriesContainer.events.on('hit', function(ev) {
  console.log(ev.target.dataItem)
});

but no dataItem attached

Upvotes: 2

Views: 1002

Answers (1)

martynasma
martynasma

Reputation: 8595

OK, so it's a bit more complicated than that. To attach events, you'll need to to actually create series for the specific level, then attach events on its column template:

var series = chart.seriesTemplates.create("0");
series.columns.template.events.on('hit', function(ev) {
  console.log(ev.target.dataItem);
});

Upvotes: 2

Related Questions