Uncaught Error: Container is not defined

3,359 views
Skip to first unread message

Giuseppe Ricci

unread,
May 1, 2017, 11:41:15 AM5/1/17
to Google Chart API
Hi,

I'm new with Google Chart. I'm retrieving with PHP weather data from OpenWeather API.
I pass to javascript the 2 data arrays with dates and temperatures to build a data array used to the google chart.
My code is:

<script type="text/javascript">
        var temp=new Array();
        <?php
        for($i=0;$i<count($temperature);$i++){?>
        temp[<?php echo $i; ?>]="<?php echo "$temperature[$i]"; ?>";
        <?php
        }?>
        
        var dataora=new Array();
        <?php
        for($i=0;$i<count($dataora);$i++){
        ?>
            dataora[<?php echo $i; ?>]="<?php echo "$dataora[$i]"; ?>";
        <?php
        }?>
        
          google.charts.load('current', {'packages':['line']});
          google.charts.setOnLoadCallback(drawChart);
        
          function drawChart() {
             var data2 = new Array();
            
             data2[0] = [ 'Date', 'Temperature' ];
             for (i = 0; i < temp.length; i++) {
                    data2[i + 1] = [ dataora[i], parseFloat(temp[i])  ];
             }
             var data = google.visualization.arrayToDataTable(data2, false);
            
             document.write(JSON.stringify(data));
            
             var options = {
                  chart: {
                  title: 'Previsioni Temperatura',
                  subtitle: 'in °C'
                 },
             width: 1000,
             height: 500,
            
             }
          };
                  
          var chart = new google.charts.Line(document.getElementById('chart_div'));
          chart.draw(data, google.charts.Line.convertOptions(options));
}

</script>
   
<div id="chart_div" style="50%"></div>
 
Opening the console I see this error:

Uncaught Error: Container is not defined

Can someone help me to solve this error?
Thanks.

Daniel LaLiberte

unread,
May 1, 2017, 11:55:40 AM5/1/17
to google-c...@googlegroups.com
Your container element, the "chart_div", appears to be what you should be using, but it appears your drawChart() function ends before you define the chart and draw it.

          };
                   
          var chart = new google.charts.Line(document.getElementById('chart_div'));
          chart.draw(data, google.charts.Line.convertOptions(options));

should be:
                   
            var chart = new google.charts.Line(document.getElementById('chart_div'));
            chart.draw(data, google.charts.Line.convertOptions(options));
          };

When your document.getElementById('chart_div') is executed outside the function, it happens even before the rest of the document is loaded, so that's why chart_div is undefined.


--
You received this message because you are subscribed to the Google Groups "Google Chart API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-chart-api+unsubscribe@googlegroups.com.
To post to this group, send email to google-chart-api@googlegroups.com.
Visit this group at https://groups.google.com/group/google-chart-api.
For more options, visit https://groups.google.com/d/optout.



--

Giuseppe Ricci

unread,
May 6, 2017, 4:10:37 PM5/6/17
to Google Chart API
Thank you Daniel, it works!
Reply all
Reply to author
Forward
0 new messages