谷歌堆积的柱状图给出了一个错误


Google stacked column chart giving an error

嗨,我正在尝试谷歌堆叠柱状图。但我遇到了以下错误,无法解决。错误为"Uncaught TypeError:无法读取null的属性'type'"

这是我的代码

<html>
 <head>
    <title></title>      
     </head>  
     <?php  $con=mysql_connect("localhost","root", "innernet") or die("Failed to connect with database!!!!");
            mysql_select_db("mobiledb", $con); 

        $user= $_GET['user'];
        //echo $user;
       $response["cols"] = array();    
       // $news = array();
        //$news["id"] = "";
        //$news["label"] = "ID";                                           
        //$news["type"] = "string";                                           
        //array_push($response["cols"], $news);

        array_push($response["cols"], $news);
        $news = array();
        //$news["id"] = "";
        //$news["label"] = "Q9a";                                           
        $news["type"] = "number";                                           
        array_push($response["cols"], $news);       
         $news = array();
        //$news["id"] = "";
        //$news["label"] = "Q9b";                                           
        $news["type"] = "number";                                           
        array_push($response["cols"], $news);
       // $news = array();
      //  $news["id"] = "";
        //$news["label"] = "ts";                                           
      //  $news["type"] = "number";                                           
       // array_push($response["cols"], $news);       

        $result = mysql_query("SELECT `Q9a`, `Q9b` FROM goaltest WHERE id='$user'") or die(mysql_error());
        if (mysql_num_rows($result) > 0)
        {       
            $response["rows"] = array();    $table = array();   $rows = array();  
            while ($row = mysql_fetch_array($result))
            {        
                $temp = array();           
                //$temp[] = array('v' => (string) $row['id']);          
                $temp[] = array('v' => (int) $row['Q9a']);
                $temp[] = array('v' => (int) $row['Q9b']);
                //$temp[] = array('v' => (int) $row['ts']);
                array_push($response["rows"], array('c' => $temp));                                      
            }            
            //echo json_encode($response);             
        }

    ?>
    <!--Load the AJAX API -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
     google.load("visualization", "1.0", {packages:["imagechart"]});
    </script>
    <script type='text/javascript'>
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var response = '<?php echo json_encode($response); ?>';     alert(' hi ' + response);            
        var obj = eval ("(" + response + ")");
        var dataTable = new google.visualization.DataTable(response);

        var options = {cht: 'bvs', chs: '300x125', colors:['#4D89F9','#C6D9FD'],
          chds:'0,160', chxl:'0:|oranges|apples|pears|bananas|kiwis|'};
        var chart = new google.visualization.ImageChart(document.getElementById('bar_div'));
        chart.draw(dataTable, options);
      }
    </script>
  <body>
    <div id='bar_div'></div>
  </body>
</html>

所以请任何人告诉我如何解决这个

<html>
 <head>
    <title></title>      
 </head>  
    <!--Load the AJAX API -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
     google.load("visualization", "1.0", {packages:["imagechart"]});
    </script>
    <script type='text/javascript'>
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var response = '{"cols":[null,{"type":"number"},{"type":"number"}],"rows":[{"c":[{"v":12},{"v":23}]},{"c":[{"v":37},{"v":55}]}]}';     alert(' hi ' + response);            
        var obj = eval ("(" + response + ")");
        var dataTable = new google.visualization.DataTable(response);

        var options = {cht: 'bvs', chs: '300x125', colors:['#4D89F9','#C6D9FD'],
          chds:'0,160', chxl:'0:|oranges|apples|pears|bananas|kiwis|'};
        var chart = new google.visualization.ImageChart(document.getElementById('bar_div'));
        chart.draw(dataTable, options);
      }
    </script>
  <body>
    <div id='bar_div'></div>
  </body>
</html> 

(@asgarden可能已经猜对了。)在您的第一个array_push(…$news)中,$news尚未定义。因此,响应变量中"cols"属性下的第一个数组元素为null。也许你只是想评论一下第一个array_push。

相关文章: