【发布时间】:2019-02-27 13:54:17
【问题描述】:
我有一张基于 amCharts 网站上的 this 示例的地图。我想在这些饼图中添加一个可点击的共享图例。因此,每当有人单击图例元素时,它都会更新所有饼图以仅显示“活动”切片值。我曾尝试以这种方式创建自定义图例:
// Setting up legend
var legend = new am4maps.Legend();
legend.parent = chart.chartContainer;
legend.background.fill = am4core.color("#000");
legend.background.fillOpacity = 0.05;
legend.y = am4core.percent(100);
legend.verticalCenter = "bottom";
legend.padding(10,15,10,15)
legend.data = [{
"name": "Category #1",
"fill": chart.colors.getIndex(0)
}, {
"name": "Category #2",
"fill": chart.colors.getIndex(1)
}, {
"name": "Category #3",
"fill": chart.colors.getIndex(2)
}, {
"name": "Category #4",
"fill": chart.colors.getIndex(2)
}];
// Does something when legend marker is clicked
legend.itemContainers.template.events.on("hit", function(ev) {
var index = ev.target.dataItem.index;
if (ev.target.isActive) {
//????.hide();
}
else {
//????.show();
}
})
在这个例子中 (codepen) 我创建了一个自定义图例和一个在图例标记被“命中”时触发的函数。我在这个函数中尝试了很多不同的东西,但还没有走远。我只是不知道如何访问图表的不同切片元素(用“????”表示)以对图例标记“命中”进行操作。
我见过的另一种可能的方法是以某种方式将其直接插入图表中。但这似乎只适用于单个饼图。 (显示here)这个例子也准确地展示了我想要的那种行为。问题是我想要一个传奇,而不是每个派都有一个。
【问题讨论】:
标签: javascript amcharts