I have the following code to load a Google Chart:
function drawChart1() {
var jsonData1 = $.ajax({
url: "library/json_netsales.php",
dataType:"json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var data1 = new google.visualization.DataTable(jsonData1);
var formatter = new google.visualization.NumberFormat(
{negativeParens: true, pattern: '$###,###'});
formatter.format(data1, 1);
// Instantiate and draw our chart, passing in some options.
var chart1 = new google.visualization.AreaChart(document.getElementById('chart_netsales'));
chart1.draw(data1, {width: 300, height: '100%', hAxis: { textPosition: 'none', baselineColor: '#fff' }, vAxis: { textPosition: 'none', baselineColor: '#fff', gridlines: {count: 0}, minValue: 0}, chartArea:{width:"100%",height:"80%"}, legend: {position: 'none' }, backgroundColor: '#232323', colors: ['#fff']});
}
Now the problem is the async flag has been turned off meaning I get page lock-ups. I want to load this asynchronously but I've failed in my attempts to get this to work.
I thought that moving everything past .responseText into a success handler and removing the async:false line would get this working, but I was wrong.
Any ideas of how to get a Google Chart to load asynchronously?
This should work:
function drawChart1() {
$.ajax({
url: "library/json_netsales.php",
dataType: "json",
success: function (json) {
// Create our data table out of JSON data loaded from server.
var data1 = new google.visualization.DataTable(json);
var formatter = new google.visualization.NumberFormat({
negativeParens: true,
pattern: '$###,###'
});
formatter.format(data1, 1);
// Instantiate and draw our chart, passing in some options.
var chart1 = new google.visualization.AreaChart(document.getElementById('chart_netsales'));
chart1.draw(data1, {
width: 300,
height: '100%',
hAxis: {
textPosition: 'none',
baselineColor: '#fff'
},
vAxis: {
textPosition: 'none',
baselineColor: '#fff',
gridlines: {count: 0},
minValue: 0
},
chartArea: {
width:"100%",
height:"80%"
},
legend: {position: 'none'},
backgroundColor: '#232323',
colors: ['#fff']
});
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With