问题描述
我一直在处理amCharts 4的几个图表,每次我都在努力地订阅返回"selected/clicked"元素的事件时.我不知道我在文档中缺少什么,但是例如,我需要从命中"事件中检索选定的项目,但我找不到任何地方(这里很简单,我想尝试一下在 https://codepen.io/team/amcharts/pen/erojQb )
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";
我尝试过:
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
but no dataItem attached
推荐答案
好,所以要复杂得多.要附加事件,您需要为特定级别实际创建系列,然后在其列模板上附加事件:
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);
});
这篇关于从TreeMap检索对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!