有什么方法可以使用元素 Id 获取 AmChart 的实例吗? 这在编写通用 API 以使用 AmCharts 时非常有用。
<div id="myChart"></div>
<div id="myChart2"></div>
<script>
function makeChart(id, settings) {
var ins = AmCharts.getChart(id) ?? //need a way to find the instance
if (ins) ins.clear();
AmCharts.makeChart(id,settings);
}
</script>
最佳答案
你可以这样解决这个问题:
function getChart(id) {
var allCharts = AmCharts.charts;
for (var i = 0; i < allCharts.length; i++) {
if (id == allCharts[i].div.id) {
return allCharts[i];
}
}
}
现在只需调用 getChart("myChart")
即可返回实例。
关于javascript - 使用 ID 获取 AmChart 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28384411/