使用带有PHP数据的Google图表
我正在尝试使用Google图表生成一个饼图,我想使用的数据在MySQL数据库中,因此我创建了一个脚本来获取数据并将其解析为JSON,理想情况下,JSON将填充该图 我已经创建了这个脚本,并检查它是否接收到数据,它是否是JSON格式的,但我得到的只是“Table has no columns”,有人知道我哪里出错了吗 显示图形的页面使用带有PHP数据的Google图表,php,json,pdo,google-visualization,Php,Json,Pdo,Google Visualization,我正在尝试使用Google图表生成一个饼图,我想使用的数据在MySQL数据库中,因此我创建了一个脚本来获取数据并将其解析为JSON,理想情况下,JSON将填充该图 我已经创建了这个脚本,并检查它是否接收到数据,它是否是JSON格式的,但我得到的只是“Table has no columns”,有人知道我哪里出错了吗 显示图形的页面 <!--Load the AJAX API--> <script type="text/javascript&qu
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load the Visualization API and the corechart package.
google.charts.load('current', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
var jsonData = $.ajax({
url: "scripts/charts/popularColours.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>
您可以看到,这是返回的数据
[{"veh_colour":null,"col":206},{"veh_colour":"Black Pearl","col":9},{"veh_colour":"dynamic red","col":8},{"veh_colour":"Battersea Blue","col":6},{"veh_colour":"Arctic White","col":6},{"veh_colour":"Laser Blue","col":5}]
您是否尝试对响应进行正确编码?现在,响应作为默认文本响应发送,但您可以让PHP告诉您的浏览器/客户端响应实际上是JSON: 在echo之前添加此项应做到:
标题(“内容类型:application/json”);
然而,jQuery可能已经猜到了这一点。也许问题出在JS方面。您是否尝试过这样的方式,而不是直接呼叫.responseText
$.ajax({
//你的参数
})
.完成(功能(数据){
//数据应该是JSON响应
});
为了从json创建google数据表,它必须采用特定的格式。否则,您可以提供一个简单的数组并使用它来创建数据表。注意:$上的async:false
。ajax
已被弃用,请改用done
回调。检查。。。
$dsn = "";
$options = [
PDO::ATTR_EMULATE_PREPARES => false, // turn off emulation mode for "real" prepared statements
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //turn on errors in the form of exceptions
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //make the default fetch be an associative array
];
try {
$pdo = new PDO($dsn, "dbu300121", "3DD63tJXYpP%", $options);
} catch (Exception $e) {
error_log($e->getMessage());
exit('There has been an unexpected error, please try again last if the issue continues then contact your IT Support team.'); //something a user can understand
}
$statement = $pdo->prepare("SELECT veh_colour, COUNT(*) as col FROM cst_vehicle GROUP BY veh_colour ORDER BY 2 DESC LIMIT 6");
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo $json;
[{"veh_colour":null,"col":206},{"veh_colour":"Black Pearl","col":9},{"veh_colour":"dynamic red","col":8},{"veh_colour":"Battersea Blue","col":6},{"veh_colour":"Arctic White","col":6},{"veh_colour":"Laser Blue","col":5}]