JSON PHP Google Visualization

时间:2014-08-22 11:54:36

标签: php ajax json google-visualization

尝试以正确的格式获取我的json输出谷歌可视化折线图,但我显然做错了,因为它返回表没有列。正如文档中所解释的那样,我使用Ajax来调用php页面。

访问getdata.php

<?php

    class MyDB extends SQLite3
       {
          function __construct()
          {
             $this->open('water.db');
          }
       }

    $db = new MyDB();
       if(!$db){
          echo $db->lastErrorMsg();
       } else {
          //echo "Opened database successfully\n";
       }

       $sql =<<<EOF
          SELECT * from wT; 
    EOF;

      $data = array();

      $data['cols'][] = array('label' => 'Temperature', 'type' => 'number');
      $data['cols'][] = array('label' => 'Time', 'type' => 'string');

      $rows = array();
      $ret = $db->query($sql);


      while($row = $ret->fetchArray(SQLITE3_ASSOC) ){

        $temp = array();

        $temp[] = array('v' => (float) $row['fishTemp']);
        $temp[] = array('v' => (string) $row['time']);
        $rows = array('c' => $temp);


        $data['rows'][] = $rows;

      }

      $jsonTable = json_encode($data, true);

      var_dump($jsonTable);
      $db->close();

    ?>

base.html文件

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);


      function drawChart() {
        var jsonData = $.ajax({
          url: "getData.php",
          dataType:"json",
          async: false
          }).responseText;

        // Create our data table out of JSON data loaded from server.
        console.log(jsonData);

        //var obj = window.JSON.stringify(jsonData);
        var data = new google.visualization.DataTable(jsonData);

        var options = {
          title: 'Title'
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

控制台的输出看起来像这样......我错过了什么?!

  

&#34; {&#34; COLS&#34;:[{&#34;标签&#34;:&#34;温度&#34;&#34;类型&#34;:&#34;字符串&#34;},{&#34;标签&#34;:&#34;时间&#34;&#34;类型&#34;:&#34;日期&#34;}],&#34;行& #34;:[{&#34; c&#34;:[{&#34; v&#34;:18.25},{&#34; v&#34;:&#34; 2014-08-19 16: 23:23&#34;}]},{&#34; C&#34;:[{&#34; v&#34;:18.25},{&#34; v&#34;:&#34; 2014- 08-19 16:23:31&#34;}]},{&#34; c&#34;:[{&#34; v&#34;:18.25},{&#34; v&#34;:& #34; 2014-08-19 16:23:39&#34;}]},{&#34; c&#34;:[{&#34; v&#34;:18.25},{&#34; v& #34;:&#34; 2014-08-19 16:23:47&#34;}]},{&#34; c&#34;:[{&#34; v&#34;:18.25},{ &#34; v&#34;:&#34; 2014-08-19 16:23:55&#34;}]},{&#34; c&#34;:[{&#34; v&#34; :18.25},{&#34; v&#34;:&#34; 2014-08-19 16:24:06&#34;}]},{&#34; c&#34;:[{&#34 ; v&#34;:18.25},{&#34; v&#34;:&#34; 2014-08-19 16:24:14&#34;}]}

1 个答案:

答案 0 :(得分:0)

使用json加载dataTable会将日期作为字符串接受,格式如下:日期(年,月,日[,小时,分钟,秒[,毫秒]]),其中一天之后的所有内容都是可选的,而月份是从零开始的。

所以对你的第一次来说,它应该是:

{"v":"Date(2014,07,19,16,23,23)"}

如果您想直接使用毫秒时间:

{"v":"Date(1411154603000)}