【发布时间】:2019-04-08 09:36:52
【问题描述】:
我正在使用带有来自 sql DB 的数据的谷歌图表(在 php 网页上)。我遇到的问题是它没有正确显示字段名称和值,它只是显示第一个字段“费用”的值。它应该显示两个字段“费用”和“收入”以及数据库中的值。任何想法我做错了什么?
我的代码如下:
<?php
$dbhandle = new mysqli('localhost','root','','useraccounts');
echo $dbhandle->connect_error;
$query = "SELECT * FROM ctincome";
$res = $dbhandle->query($query);
?>
<html>
<head>
<script type="text/javascript"
src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load("current", {packages:["corechart"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['expense','income'],
<?php
while($row=$res->fetch_assoc())
{
echo "['".$row['expense']."','".$row['income']."'],";
}
?>
]);
var options = {
title: 'Expenses to Income',
pieHole: 0.4,
};
var chart = new
google.visualization.PieChart(document.getElementById
('donutchart'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="donutchart" style="width: 900px; height: 500px;"></div>
</body>
</html>
【问题讨论】:
-
看看这个链接,它可能对你有帮助。 stackoverflow.com/questions/2970936/…
标签: php sql charts google-visualization