简体   繁体   English

无法从MySQL数据库创建Google饼图

[英]Unable to create a google pie chart from mysql database

Reading through several posts, I have got the basic idea of creating google charts but I am still not clear with how it is created from data extracted from tables in DB. 通过阅读几篇文章,我已经了解了创建Google图表的基本思想,但是我仍然不清楚如何从数据库表中提取的数据创建它。 Some json parsing of objects is done, but not clear with it. 完成了一些对象的json解析,但不清楚。 I have written some code. 我已经写了一些代码。 Please provide me with some direction ahead. 请向我提供一些指导。

//chartDraw.php //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 specified in the url property //在url属性中指定的getdata.php

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

}

?>

What next and How am I supposed to send the data to json objects and where? 接下来是什么?我应该如何将数据发送到json对象?在哪里? I am confused.. 我很困惑..

This simple example may be help you 这个简单的例子可能对您有帮助

<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 fecth_data.php

<?php
$array = array(1,2,3,4);
echo json_encode($array);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM