2

阅读了几篇文章,我有了创建谷歌图表的基本想法,但我仍然不清楚它是如何从从数据库中的表中提取的数据创建的。一些对象的 json 解析已经完成,但不清楚。我写了一些代码。请给我一些前进的方向。

//chartDraw.php

<html>
<head>
<!--Load the AJAX API -->
<script type="text/javascript" src="https://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">
//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);
//Instantiate and draw our chart, passing in some options
var chart=new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data,{width:400,height:240});
}
</script>
</head>
<body>
        <!--Div that will hold the pie chart -->
        <div id="chart_div"></div>
</body>
</html>

//getdata.php中指定的url属性

<?php

mysql_connect('localhost','uname','123456');
mysql_select_db('rcusers');

$sqlquery1="select userid,group_name,req_nodes,actualPE from jobs where userid='zhang' limit 200";

$sqlresult1=mysql_query($sqlquery1);
while($row=mysql_fetch_assoc($sqlresult1)){
        $userDetails[]=$row;

}

?>

接下来是什么以及我应该如何将数据发送到 json 对象以及在哪里?我很困惑..

4

1 回答 1

0

这个简单的例子可能会帮助你

<html>
     <head>
      <script type="text/javascript" src="https://www.google.com/jsapi"></script>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
      <script type="text/javascript">
        $(document).ready( function() {
        $('#test').click(function() {
          $.ajax({
          type: 'POST',
          url: 'fetch_data.php',
          dataType: 'json',
          cache: false,
          success: function(result) {
               var data = google.visualization.arrayToDataTable([
                  ['T', result[0]],
                  ['W', result[1]],
                  ['E', result[2]]

                ]);

                var options = {
                  title: 'My Daily Activities'
                };

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

fecth_data.php

<?php
$array = array(1,2,3,4);
echo json_encode($array);
?>
于 2013-02-23T07:37:36.460 回答