我正在尝试为我的数据库表创建一个饼图。
temp -> with columns(id, sent, pcount, ncount)
pcount
并且ncount
是整数。我想为这两个值创建一个饼图。
我正在尝试加载此文件。
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart()
{
var jsonData = $.ajax({
url: "graphData.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 options = {'title':'Ticket Sales',
'width':500,
'height':400};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data,options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
graphData.php 内容如下。
<?php
$con = mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
$sql = "SELECT pcount,count(*) AS count from temp";
if ($result=mysqli_query($con,$sql))
{
$rownum=mysqli_num_rows($result);
printf("Result set has %d rows.\n",$rownum);
mysqli_free_result($result);
}
//start the json data in the format Google Chart js/API expects to receieve it
$data = array('cols' => array(array('label' => 'pcount', 'type' => 'int'),
array('label' => 'mcount', 'type' => 'int')),
'rows' => array());
while($row = mysqli_fetch_row($result)) {
$data['rows'][] = array('c' => array(array('v' => $row[0]), array('v' => $row[1])));
}
echo json_encode($data);
?>
我从网上获取了这段代码,并根据我的需要进行了修改。当我加载我的第一个 PHP 页面时,它什么也没显示。我究竟做错了什么?