从TreeMap检索对象

时间:2019-05-03 12:33:03

标签: amcharts amcharts4

我一直在处理amCharts 4的几个图表,每次我努力地订阅返回“ selected / clicked”元素的事件时。我不知道我在文档中缺少什么,但是例如,我需要从“命中”事件中检索选定的项目,但我找不到任何地方(这里很简单,我想尝试一下在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";

我尝试过:

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

但未被调用,并且:

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

但未附加dataItem

1 个答案:

答案 0 :(得分:2)

好,所以要复杂得多。要附加事件,您需要为特定级别实际创建系列,然后在其列模板上附加事件:

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