I get the above message when I try and load a Google Charts script.
I'm pretty sure the issue is that I'm trying to load the dataTable using jQuery's getJSON
method, but I've read through the answers to this question and still can't connect the dots.
The data I'm trying to get comes from the url /api/formula
and looks like this:
[{"name": "the name", "amount": "the amount"},{...},{...}]
The script I use for the charts is this:
google.load('visualization', '1.0', {
'packages':['corechart']
});
google.setOnLoadCallback(drawChart);
function loadIngredients() {
var jsonData = {
"cols": [
{"id":"","label":"Ingredient","pattern":"","type":"string"},
{"id":"","label":"Amount","pattern":"","type":"number"}
],
"rows": []
};
$.getJSON('/api/formula', function(data){
$.each(data, function(key, item){
jsonData.rows.push({"c":[{"v":item.name,"f":null},{"v":item.amount,"f":null}]});
});
});
return jsonData;
}
function drawChart() {
var options = {
'title': 'Formula Breakdown By Weight',
'pieHole': 0.4
};
var data = new google.visualization.DataTable(loadIngredients());
var chart = new google.visualization.PieChart(document.getElementById('formula-chart-div'));
chart.draw(data, options);
}
And finally the jQuery I nest it inside looks like this:
$(document).ready(function(){
$('.formula-info').click(function(){
if (someStuffIsntEntered) {
$('#modal2').modal();
} else if(someOtherStuffIsntEnered) {
$('#modal3').modal();
} else {
$('#formula-info-div').fadeIn(750);
$('#formula-build-div').hide();
//draw chart from formula-chart.js
loadIngredients();
//grab json data for formula ingredients
$.getJSON('/api/formula', function(data){
$('.formula-breakdown-table > tbody').empty();
//fill in table with info provided in form at top of page
$.each(data, function(key, item){
//fill in a table with data from the JSON
);
});
}
});
//fade in top of page
$('.back-to-main-page').click(function(){
$('#formula-build-div').fadeIn(750);
$('#formula-info-div').hide();
});
});
And if it helps the files are loaded in this order:
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="../../../javascripts/formula.js"></script> //jquery
<script src="../../../javascripts/formula-chart.js"></script> //google charts
There might be a few things wrong with my code, but I believe my error message is because the data itself isn't being loaded.