我在获取图形输出时遇到问题。图形显示为空。我想构建一个图形,显示以年/月为单位注册的用户数。
我从数据库获得的值是:
Example: id=1 , registado = 2014-05-04
id=2 , registado = 2014-07-09
代码分为2个文件,一个是从数据库中获取字段,另一个是调用图形的函数,以及用于将数据放入图形的查询。
查询的输出:
1 May 20148 Jul 201411 Aug 2014
相应的代码:
getpiechartdata.php
<?php
$dbuser="root";
$dbname="dissertacao";
$dbpass="";
$dbserver="localhost";
$sql_query = "SELECT id,registado,MONTHNAME(registado), YEAR(registado), SUM(id) FROM utilizadores GROUP BY YEAR(registado), MONTH(registado)";
$con = mysql_connect($dbserver,$dbuser,$dbpass);
if (!$con){ die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $con);
$result = mysql_query($sql_query);
$total_rows = mysql_num_rows($result);
$row_num = 0;
while($row = mysql_fetch_array($result)){
$dados .= "['".$row{'id'}."',"(' M Y', strtotime($row['registado']),";
}
mysql_close($con);
?>
index.php
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1.1", {packages:["bar"]});
function drawItems(num) {
var jsonPieChartData = $.ajax({
url: "getpiechartdata.php",
data: "q="+num,
dataType:"json",
async: false
}).responseText;
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Numero de utilizadores', 'Data'],
<?php echo $dados ?>
]);
var options = {
title: 'Numero de utilizadores registados',
curveType: 'function',
legend: { position: 'bottom' }
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>