谷歌堆积柱形图给出错误

时间:2013-08-07 21:53:31

标签: php google-visualization

您好我正在尝试谷歌堆积柱形图。但我得到以下错误,我无法解决它。错误是“Uncaught TypeError:无法读取属性'type'的null”

这是我的代码

<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> 

1 个答案:

答案 0 :(得分:3)

(@ asgallant可能已经猜对了。)在你的第一个array_push中(... $ news)$ news尚未定义。因此,响应变量中“cols”属性下的第一个数组元素为null。也许你只想注释掉第一个array_push。