I'm trying to add a google visualization asynchronous, but I am running into problems. I've narrowed it down to the google.load causing the problem. When google.load part of the js runs, I get an empty screen/dom. Any one know what I am doing wrong.
I've also tried using the google.setOnLoadCallback, I get the same result.
Any help would be great
Relevant code :
$(document).ready(function () {
google.load('visualization', '1', { 'packages': ['geomap'] }, { 'callback': drawVisualization });
function drawVisualization() {
$.ajax({
type: "POST",
data: "{'monitorId':'" + monitor + "','monitorName':'" + name + "','context':'" + context + "'}",
dataType: "json",
url: "WebService.asmx/LoadMonitorToolGeo",
contentType: "application/json; charset=utf-8",
processData: true,
success: function (msg) {
var obj = jQuery.parseJSON(msg.d);
// $(msg.d).hide().appendTo("#sortable").fadeIn();
$("#" + obj.context).find(".toolContent").hide().html(obj.data).fadeIn();
DrawWorldMap(obj.map, obj.context);
},
error: function (req, status, error) {
},
complete: function (req, status) {
}
});
function DrawWorldMap(response, id) {
var data = new google.visualization.DataTable();
data.addRows(response.d.length);
data.addColumn('string', 'Country');
data.addColumn('number', 'Popularity');
for (var i = 0; i < response.d.length; i++) {
data.setValue(i, 0, response.d[i].Country);
data.setValue(i, 1, response.d[i].Popularity);
}
var options = {};
options['dataMode'] = 'regions';
var container = document.getElementById(id);
var geomap = new google.visualization.GeoMap(container);
geomap.draw(data, options);
}
});
Use noData() method to enable "No data" label: chart. noData().
arrayToDataTable()This helper function creates and populates a DataTable using a single call. Advantages: Very simple and readable code executed in the browser. You can either explicitly specify the data type of each column, or let Google Charts infer the type from the data passed in.
The Google Chart API is an extremely simple tool that lets you easily create a chart from some data and embed it in a webpage. You embed the data and formatting parameters in an HTTP request, and Google returns a PNG image of the chart.
Double-click the chart you want to change. At the right, click Setup. Select the cells you want to include in your chart. Optional: To add more data to the chart, click Add another range.
This article helped me:
How to dynamically load the Google Maps javascript API (On demand loading)
What made the difference here was to define the callback
in the load
method options attribute:
var options = {packages: ['corechart'], callback : myCallback};
google.load('visualization', '1', options);
I believe this way google.setOnLoadCallback(myCallback)
won't be needed anymore and fortunately, it seems when callback
is added the method won't cleanup the page.
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