我正在使用谷歌图表API并使用php mysql填充条形图。 我的前端是:
<script type="text/javascript">
// Load the Visualization API and the line package.
google.charts.load('current', {'packages':['bar']});
// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
$.ajax({
type: 'POST',
url: 'getData.php',
success: function (data1) {
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable();
data.addColumn('number', 'State');
data.addColumn('number', 'Status');
data.addColumn('number', 'City');
var jsonData = $.parseJSON(data1);
for (var i = 0; i < jsonData.length; i++) {
data.addRow([jsonData[i].state, parseInt(jsonData[i].status), parseInt(jsonData[i].city)]);
}
var options = {
chart: {
title: 'Users',
subtitle: 'Users registered'
},
width: 900,
height: 500,
axes: {
x: {
0: {side: 'top'}
}
}
};
var chart = new google.charts.Bar(document.getElementById('bar_chart'));
chart.draw(data, options);
}
});
}
</script>
<div id="bar_chart"></div>
我的getData.php文件有:
$sql = "SELECT `state`,`status`,`city` FROM `user`";
$sth = mysqli_query($con,$sql) or die(mysqli_error($con));
$row=mysqli_fetch_assoc($sth);
print_r(json_encode($row,true));
我在成功函数中从数据库中获取值,但无法在图表中打印