Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AmCharts.ready is not ready when loaded asynchronously

Initially I put all AmChart configs in AmCharts.ready is everything worked fine.

<script src="amcharts/amcharts.js" type="text/javascript"></script>
<script src="amcharts/serial.js" type="text/javascript"></script>
<script>
    AmCharts.ready(function() {
      console.log("this works");
      /* Other configs */
    })
</script>

Somehow it stops working when I introduce RequireJS as instructed in amCharts meets requireJS. Whatever code inside AmChart.ready's callback will not be executed. (Oddly it was executed one time during the debugging)

like image 417
Andy Avatar asked Nov 29 '22 11:11

Andy


2 Answers

After a few tests I realized AmChart.ready only pushes the callback into the onReadyArray which is later popped for execution after the window.load/onload event. In other words, if AmChart is loaded after window.onload event, AmChart.ready is useless. My workaround is as followed:

<script>
    configChart = function() {
      /* Create charts stuff */
    };
    if (AmCharts.isReady) {
      configChart();
    } else {
      AmCharts.ready(configChart);
    }
</script>
like image 181
Andy Avatar answered Dec 09 '22 22:12

Andy


Still doesn't work in newer versions (version 3.14.1 at this time), but what fixes it is calling the handleLoad method manually:

AmCharts.handleLoad();

Calling it multiple times doesn't seem to do any harm, either.

like image 31
skerit Avatar answered Dec 09 '22 21:12

skerit