[英]Getting JSON from MySQL into Google Charts
I'm trying to populate a basic Google Area Chart using their example and some data from a MySQL table. 我正在尝试使用其示例和MySQL表中的一些数据填充基本的Google区域图表。 Here's what I have:
这是我所拥有的:
<?php
include("inc/DBConn.php");
$link = connectToDB();
$query = "SELECT MONTH(checkout) as Checkout, COUNT(item_id) as BookCount FROM C_books WHERE YEAR(checkout) = 2012 GROUP BY MONTH(created) ASC";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$numBooks[] = $row;
};
$chartData = json_encode($numBooks,JSON_NUMERIC_CHECK);
?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable(<?php echo $chartData; ?>,false);
var options = {
title: 'Total Books',
hAxis: {title: 'Month', titleTextStyle: {color: 'red'}}
};
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
This is my result with the echo: 这是我的回声结果:
[{"Checkout":"3","BookCount":"19"},{"Checkout":"4","BookCount":"157"},{"Checkout":"5","BookCount":"30"},{"Checkout":"6","BookCount":"45"},{"Checkout":"7","BookCount":"688"},{"Checkout":"8","BookCount":"391"}]
I know that Google doesn't like this but I'm unable to understand how to format it so that it does??? 我知道Google不喜欢这样,但是我不明白如何设置它的格式,从而做到这一点???
$numBooks[] = array("Checkout", "BookCount");
while ($row = mysql_fetch_array($result)) {
$numBooks[] = $row;
};
present data in needed format 以所需格式显示数据
the following is my method of using SQL -> Array -> Google DataTable JSON String (I have not referenced how others do it, so there could be a better way of coding it). 以下是我使用SQL->数组-> Google DataTable JSON字符串的方法(我没有引用其他人的操作方式,因此可能会有更好的编码方式)。 Note that $task_submissions_arr is an array populated with rows & columns from a database query (but not shown below)
注意$ task_submissions_arr与行和列填充从数据库查询的阵列(但低于未示出)
<?php
// array to be converted to json string and used in google chart's bar chart
$bar_chart_cols_arr = array(
array('id' => 'Name', 'label' => 'Name', 'type' => 'string'),
array('id' => 'Auto Grading Marks', 'label' => 'Auto Grading Marks', 'type' => 'number'),
array('id' => 'Manual Grading Marks', 'label' => 'Manual Grading Marks', 'type' => 'number'));
// array to be converted to json string and used in google chart's bar chart
$bar_chart_rows_arr = array();
for($i=0; $i<count($task_submissions_arr); $i++){
// array nesting is complex owing to to google charts api
array_push($bar_chart_rows_arr, array('c' => array(
array('v' => $task_submissions_arr[$i]['name']),
array('v' => $task_submissions_arr[$i]['auto_grading_marks']),
array('v' => $task_submissions_arr[$i]['manual_grading_marks']))) );
}
?>
<script type="text/javascript">
google.setOnLoadCallback(draw_bar_chart);
function draw_bar_chart() {
var bar_chart_data = new google.visualization.DataTable(
{
cols: <?php echo json_encode($bar_chart_cols_arr); ?>,
rows: <?php echo json_encode($bar_chart_rows_arr); ?>
});
// to be continued...
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.