I'm beginning to use the Google Charts API and am seeing some curious aspects about its behavior. I first noticed that my sample chart wouldn't load when I placed the google chart code inside of the document.ready of jQuery. So then I played around and did the following:
google.load('visualization', '1.0', { 'packages': ['corechart'] });
//jquery part
$(document).ready(function () {
google.setOnLoadCallback(drawStuff);
function drawStuff() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onion', 1],
['Olives', 1],
['Zucchini', 3],
['Pepperoni', 2]
]);
//set the chart options
var options = {
title: 'Pizza Consumed',
width: 400,
height: 500
};
//instantiate and draw our chart, passing in the options
var chart = new google.visualization.ColumnChart(document.querySelector('#chart'));
chart.draw(data, options);
$('#chart').fadeIn();
};
});
This works as I hoped it would but when I open up developer tools I see that the GET request for google.com/jsapi apparently failed (indicated by the red X in Chrome dev tools). The chart certainly comes up on the page and works as I would expect it would, however. Why does this current iteration work whereas placing everything inside of the document.ready does not? If I wanted to use Google charts on a project along with jQuery would this be a viable method of doing so?