Search code examples
javascriptgoogle-visualization

Long lived Google Visualization DataTable?


Is there an easy way to create a global / long lived instance of a google.visualization.DataTable()?

All the examples I've seen show it being created locally in a function, and that function then being a callback from the google.charts.load operation, either by specifying the callback directly in the load, or by using google.charts.setOnLoadCallback after the load.

But that then means that when the function ends the table goes out of scope and can't be used again.

There are operations for adding and removing rows from a DataTable, which suggests that they can be "long lived", but I haven't seen an example of how to achieve this.


Solution

  • you can create a global variable. then create the table after google loads...

    var dataTable;
    
    google.charts.load('current', {
      packages:['corechart']
    }).then(function () {
      dataTable = google.visualization.DataTable();
      ...
    });
    

    then dataTable would be available until the page unloads...