Diego Sarmiento
Diego Sarmiento

Reputation: 581

'null' is not an object with Google chart (issue with select menu, onchange event)

I'm using the following Script:

<header>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
</header>

<body>
<script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Header');
    data.addColumn('number', '');
    data.addColumn('number', '');
    data.addRows([
      ['Monday',300,43],
      ['Tuesday',250,545],
      ['Wednesday',122,78],
      ['Thursday',348,92],
      ['Friday',23,61],
      ['Saturday',39,93]
    ]);
    var options = {
      title: '',
      hAxis: {title: '',  titleTextStyle: '#efede9'},
      backgroundColor: '#efede9',
      legend: 'none'
    };
    var chart = new google.visualization.AreaChart(document.getElementById('area_chart_google'));
    chart.draw(data, options);
  }
</script>

<div id="area_chart_google" class="area-chart"></div>
<body>

I cannot use the script inside Header because I'm using dynamic content.

I figure it out to work when I first load the page, and after refreshing it. My problem now is that I'm using a select menu with an onchange event, similar to this:

<select id="form_frame1" name="frame1" onchange="getChart(this);">
 <option value="area_chart_google" >Area Chart</option>
 <option value="area_chart_2" selected="selected">Stacked Chart</option>
</select>

My getChart function is:

function getChart(selection) {
 if (selection.value == "area_chart_2") {
    document.getElementById('area_chart_2').style.display = 'block';
    document.getElementById('area_chart_google').style.display = 'none';
}

 else {   
    document.getElementById('area_chart_2').style.display = 'none';
    document.getElementById('area_chart_google').style.display = 'block';
   }
}

So, once my area_chart_google is not displayed (display: none) and I select it, I get the error:

*Firebug* google-visualization-errors-all-1 
*Chrome* Cannot read property 'length' of null
*Safari* 'null' is not an object

I guess is something with getElementById, so I tried using:

window.onload = function(){ javascript code here }

And also:

$(document).ready(function() {
   javascript code here
});​

Nothing happen. I get no error but the Chart don't appear.

Any help will be highly appreciated.

Upvotes: 2

Views: 2152

Answers (3)

Diego Sarmiento
Diego Sarmiento

Reputation: 581

This is working for me:

function getChart(selection) {
  if (selection.value == "area_chart_2") {
  document.getElementById('area_chart_2').style.display = 'block';
  document.getElementById('area_chart_google').style.display = 'none';
}

else {   
  document.getElementById('area_chart_2').style.display = 'none';
  document.getElementById('area_chart_google').style.display = 'block';
  drawChart(); //call this function when using this Google Chart
 }
}

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

not sure, but try doing :

google.load("visualization", "1", {packages:["corechart"]});
//..your drawChart() function code here
$(document).ready(function() {
  google.setOnLoadCallback(drawChart);
});

I hope you have already included following script in your header:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

Upvotes: 0

jmac
jmac

Reputation: 7128

You need to load the Google API first:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>

You also should have proper <body> and <head> tags in your script.

Otherwise the code is fine. The below works fine for me:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization API Sample
    </title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['corechart']});
    </script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Header');
        data.addColumn('number', '');
        data.addColumn('number', '');
        data.addRows([
          ['Monday',300,43],
          ['Tuesday',250,545],
          ['Wednesday',122,78],
          ['Thursday',348,92],
          ['Friday',23,61],
          ['Saturday',39,93]
        ]);
        var options = {
          title: '',
          hAxis: {title: '',  titleTextStyle: '#efede9'},
          backgroundColor: '#efede9',
          legend: 'none'
        };
        var chart = new google.visualization.AreaChart(document.getElementById('area_chart_google'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body> 
    <div id="area_chart_google" class="area-chart"></div>
  </body>
</html>

Upvotes: 0

Related Questions