Suggestion on Google Charts examples

23 views
Skip to first unread message

Jayme Jeffman

unread,
Jul 12, 2017, 9:44:16 AM7/12/17
to Google Visualization API
Hello,

Recently I had a problem caused by the time my page spent to load from the server.

The chart object was being created while the page was not fuly loaded yet , so the container object didn't exists at that time and the chart was not created due to JavaScript error.

As you can see at Chart Gallery, all chart examples are simple and the chart object is created while the page is being loaded.

I suggest you split the code in two parts, one containing the options definitions and other containig the call to the library and the callback method as bellow:

  <html>
 
<head>
   
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   
<script type="text/javascript">
     
// Options and draw methods definition
     
function drawChart() {
       
var data = google.visualization.arrayToDataTable([
         
['Year', 'Sales', 'Expenses'],
         
['2004',  1000,      400],
         
['2005',  1170,      460],
         
['2006',  660,       1120],
         
['2007',  1030,      540]
       
]);

       
var options = {
          title
: 'Company Performance',
          curveType
: 'function',
          legend
: { position: 'bottom' }
       
};

       
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

        chart
.draw(data, options);
     
}
     
function pageLoad()
     
{     
google
.charts.load('current', {'packages':['corechart']});
        google.charts.setOnLoadCallback(drawChart);
}



 
   
</script>

  </head>
 
<body onload="pageLoad();">
   
<div id="curve_chart" style="width: 900px; height: 500px"></div>
 
</body>
</html>

This way the library will be loaded after the page is fully loaded and all html objects will have been already created.

Please let me know if I have made any code heresy.

Best regards.

Jayme Jeffman



Daniel LaLiberte

unread,
Jul 12, 2017, 3:01:48 PM7/12/17
to Google Visualization API
Hi Jayme,

The loader does (or should) always wait for the window (and document) to be loaded before the callbacks are called.  So there *should* be no need for you to check for document onload before proceeding.  

But I actually just added a new setting for the 45.2 loader that allows you to ignore the window onload, since some applications need that flexibility.  And it appears this was not working properly and it was ignoring the onload always.  That may be what you were seeing.

I just uploaded a fix for this problem, so it should again be waiting for the window onload.  

--
You received this message because you are subscribed to the Google Groups "Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-visualization-api+unsubscr...@googlegroups.com.
To post to this group, send email to google-visualization-api@googlegroups.com.
Visit this group at https://groups.google.com/group/google-visualization-api.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-visualization-api/b5094f11-7d92-46ea-b214-145bc634ae80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Reply all
Reply to author
Forward
0 new messages