来自MySQL JSON PHP的谷歌条形图

时间:2014-01-09 16:59:31

标签: php json google-visualization

我想使用Google Charts创建一个简单的条形图。 数据来自MySQL。使用PHP 到目前为止我所拥有的:  1. Index.php(没问题)和GetData.php(问题)

<script type="text/javascript">
    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart']});
    // Set a callback to run when the Google Visualization API is loaded.
    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.
      var data = new google.visualization.DataTable(jsonData);
   var chart = new google.visualization.barChart(document.getElementById('chart_div'));
      chart.draw(data, {width: 400, height: 240});
    }
    </script>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>

...访问getdata.php

 <?php
 include('../conn.php');
 ?>
<?php
$result = mysql_query
("select (case when n = 1 then 'Quality' else 'Speed' end) as         Assessment,
   (case when n = 1 then avg_quality else avg_speed end) as Value
from (select AVG(r.quality) as avg_quality, AVG(r.speed) as avg_speed
  from reading_assestment r join
       people p
       on r.person_id =p.person_id
  where person_id = 3
 ) t cross join
 (select 1 as n union all select 2) n");

$rows = array();
while($r = mysql_fetch_assoc($result))
{
$rows[] = $r;
}
print json_encode($rows);

选择查询输出是否正确:

    Assestment Value
    Quality      77
    Speed        65

在此之后我不知道如何创建正确的json输出来绘制Barchart

0 个答案:

没有答案