JavaScript Google visualization undefined for setOnLoadCallback google.visualization undefined,google.visualization.datatable undefined,google.visualization.arraytodatatable

I am using Google Geocharts and following Google's documentation (https://developers.google.com/chart/interactive/docs/gallery/geochart?hl=en) to setup a chart like this:

google.charts.load('current', {'packages':['geochart']});
google.setOnLoadCallback(drawRegionsMap);

function drawRegionsMap() {

 ...
  var data = google.visualization.arrayToDataTable(a1);
  var options = {};

  var chart = new google.visualization.GeoChart(document.getElementById('div_geochart'));

  chart.draw(data, options);
};

array a1 is defined globally. The script include lines are present in the header. The problem is, sometimes, I get an error saying:

Uncaught TypeError: Cannot read property 'arrayToDataTable' of undefined

and the chart doesn't load. The real problem is that it's hard to pinpoint the problem since it doesn't happen always. It seems that sometimes the google.visualization hasn't as yet been set up? But isn't the whole point of calling drawRegionsMap using setOnLoadCallback that, it will be called after google.visualization has been successfully set up?

I did try changing the location of the setOnLoadCallback lines as mentioned here: Google Visualization - TypeError: Cannot read property 'DataTable' of undefined [Chrome specific]. I also put those 2 lines in the <head> section of the page but the error still occurs sometimes (tested in Chrome and IE).

Could someone give me pointers as to how to avoid getting this error?

Answer:1

I typically wait for the load event, before loading google.
You can also define the callback function, within the google.charts.load statement.

var a1 = [
  ['Country', 'Popularity'],
  ['Germany', 200],
  ['United States', 300],
  ['Brazil', 400],
  ['Canada', 500],
  ['France', 600],
  ['RU', 700]
];

// wait for page to load
window.addEventListener('load', loadGoogle, false);

function loadGoogle() {
  // define callback in load statement
  google.charts.load('current', {'packages':['geochart'], 'callback': drawRegionsMap});
}

function drawRegionsMap() {
  var data = google.visualization.arrayToDataTable(a1);
  var options = {};
  var chart = new google.visualization.GeoChart(document.getElementById('div_geochart'));
  chart.draw(data, options);
};
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://www.google.com/jsapi"></script>
<div id="div_geochart"></div>
Answer:2

I am using a site that allows its users an admin panel for which we can add HTML/CSS/Javascript/Jquery to the site after their DOM is loaded. Since i am a user i can not change what is being loaded ...

I am using a site that allows its users an admin panel for which we can add HTML/CSS/Javascript/Jquery to the site after their DOM is loaded. Since i am a user i can not change what is being loaded ...

  1. remove content page
  2. remove page content sharepoint 2013
  3. remove outdated content page
  4. remove outdated content page google
  5. remove outdated content page google images
  6. google remove content page
  7. remove update content page
  8. remove content facebook page
  9. remove content web page
  10. drupal remove content page
  11. sharepoint remove page content section
  12. genesis remove page content
  13. javascript remove page content
  14. squarespace remove page content
  15. sharepoint remove page content
  16. squarespace remove page content homepage
  17. remove page number from content page
  18. sharepoint 2013 remove page content section
  19. remove-page-content-share

Trying to format/parse the following date stamp from the server into the users local time zone using momentjs library. Added fiddle: http://jsfiddle.net/2hgyb490/ I've tried moment.utc(date) ...

Trying to format/parse the following date stamp from the server into the users local time zone using momentjs library. Added fiddle: http://jsfiddle.net/2hgyb490/ I've tried moment.utc(date) ...

I have a bootstrap modal that trigger additional actions when opened or closed manually. These actions are hooked with show.bs.modal and hide.bs.modal events. Now I want to also be able to open or ...

I have a bootstrap modal that trigger additional actions when opened or closed manually. These actions are hooked with show.bs.modal and hide.bs.modal events. Now I want to also be able to open or ...

  1. open close bootstrap
  2. sidebar open close bootstrap
  3. open close panel bootstrap
  4. open and close bootstrap modal jquery
  5. open close div bootstrap
  6. open close icon bootstrap
  7. open and close bootstrap modal
  8. open close accordion bootstrap
  9. toggle open close bootstrap
  10. open close tab bootstrap
  11. open close button bootstrap
  12. open close side menu bootstrap
  13. bootstrap open close section
  14. bootstrap open close menu
  15. bootstrap open close card
  16. bootstrap open close list
  17. bootstrap open close form

When working on client-side DOM manipulation, we usually use JavaScript or jQuery. I am wondering if there is any other way to do this without JavaScript or JavaScript-based libraries?

When working on client-side DOM manipulation, we usually use JavaScript or jQuery. I am wondering if there is any other way to do this without JavaScript or JavaScript-based libraries?

  1. dom manipulation without javascript