Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google.setOnLoadCallback does not work/does not wait until anything is loaded

That's pretty much it, the call seems to do absolutely nothing.

If I debug the page in Chrome, put a breakpoint right after the call to

google.load('visualization', '1.0', { packages: ['corechart', 'bar', 'table'] });

and literally wait 5-10 seconds for the loading to finish, then proceed, it is fine. Otherwise, if I let 'setOnLoadCallback' try to do it's thing, it will throw

'Uncaught TypeError: Cannot read property 'DataTable' of undefined'

The error is thrown on:

var data = new google.visualization.DataTable();

Edit:

I should also note that in all examples I've found, when calling

google.setOnLoadCallback(drawChart);

All the examples are as above, without including the brackets '()' at the end of the function, i.e.

google.setOnLoadCallback(drawChart());

However, the only way it actually works for me, is WITH the brackets, yet there are no examples where people include the brackets.

like image 475
Justin Avatar asked Jun 24 '15 20:06

Justin


People also ask

Can Google Charts be used offline?

NO! Your users' computers must have access to https://www.gstatic.com/charts/loader.js in order to use the interactive features of Google Charts.

What is setOnLoadCallback?

setOnLoadCallback to register the specified handler function to be called once the document loads instead of GSearch.

How do I use Google Charts?

The most common way to use Google Charts is with simple JavaScript that you embed in your web page. You load some Google Chart libraries, list the data to be charted, select options to customize your chart, and finally create a chart object with an id that you choose.


1 Answers

google.setOnLoadCallback(drawChart()) (with paranthesis) should not work at all, so that sounds a little bit strange. setOnLoadCallback expects the name of a function it can call, not the execution of a function.

setOnLoadCallback is a general google feature. It is also triggered by google.load("jquery", "1.9.1") etc. It could be the case that somewhere in your code another library is loaded by google.load() and by that triggering your setOnLoadCallBack prematurely.

So to be absolutely certain that your callback is actually triggered when the visualization library is loaded, and not by something else, you can set the callback directly on load() instead of relying on setOnLoadCallback() :

google.load('visualization', '1.0', 
     { packages: ['corechart', 'bar', 'table'], callback: drawChart });
like image 182
davidkonrad Avatar answered Sep 21 '22 21:09

davidkonrad