Is there any way to get an instance of the AmChart using the element Id? This is very useful when writing generic API to work with 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>
You can solve this problem like this:
function getChart(id) {
var allCharts = AmCharts.charts;
for (var i = 0; i < allCharts.length; i++) {
if (id == allCharts[i].div.id) {
return allCharts[i];
}
}
}
Now just call getChart("myChart")
and it returns the instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With