Autoloading Google Charts API

420 views
Skip to first unread message

Tong Sun

unread,
Feb 5, 2015, 11:41:39 AM2/5/15
to google-visua...@googlegroups.com
Hi, 

Would autoloading Google Charts API like the following work? 
It cease to work if I comment out the "google.load" line. Shouldn't it be still working? 

Thanks

```
<html>
  <head>
    <!--Load the AJAX Visualization API and the corechart package -->
    <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}" />
    <script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

      var data = new google.visualization.DataTable();
      data.addColumn('timeofday', 'Time of Day');
      data.addColumn('number', 'Motivation Level');

      data.addRows([
        [{v: [8, 0, 0], f: '8 am'}, 1],
        [{v: [9, 0, 0], f: '9 am'}, 2],
        [{v: [10, 0, 0], f:'10 am'}, 3],
        [{v: [11, 0, 0], f: '11 am'}, 4],
        [{v: [12, 0, 0], f: '12 pm'}, 5],
        [{v: [13, 0, 0], f: '1 pm'}, 6],
        [{v: [14, 0, 0], f: '2 pm'}, 7],
        [{v: [15, 0, 0], f: '3 pm'}, 8],
        [{v: [16, 0, 0], f: '4 pm'}, 9],
        [{v: [17, 0, 0], f: '5 pm'}, 10],
      ]);

      var options = {
        width: 1000,
        height: 563,
        hAxis: {
          title: 'Time of Day',
          format: 'h:mm a',
          gridlines: {count: 10}
        },
        vAxis: {
          title: 'Rating (scale of 1-10)'
        }
      };

      var chart = new google.visualization.ColumnChart(
        document.getElementById('ex0'));

      chart.draw(data, options);

      }
    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="ex0"></div>
  </body>
</html>
```

Tong Sun

unread,
Feb 5, 2015, 12:36:18 PM2/5/15
to google-visua...@googlegroups.com
Commenting out the "google.load" line from this jsfiddle still OK:
http://jsfiddle.net/jbj56vdz/

What's wrong with the following code then? 

Thanks

Sergey Grabkovsky

unread,
Feb 5, 2015, 1:39:38 PM2/5/15
to google-visua...@googlegroups.com
The problem you're seeing is because you didn't properly close your script tag. Note the "Tag omission" section on the MDN page: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script

Script tags must be explicitly closed, which means that your code would look like:

    <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>
--
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-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.

Tong Sun

unread,
Feb 5, 2015, 1:53:06 PM2/5/15
to google-visua...@googlegroups.com

On Thu, Feb 5, 2015 at 1:39 PM, 'Sergey Grabkovsky' via Google Visualization API <google-visua...@googlegroups.com> wrote:
Script tags must be explicitly closed

oh, yeah. Thanks a lot!
Reply all
Reply to author
Forward
0 new messages