Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an instance of AmChart using ID

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>
like image 344
sam360 Avatar asked Dec 12 '22 01:12

sam360


1 Answers

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.

like image 200
gerric Avatar answered Dec 24 '22 21:12

gerric