0

我正在尝试使用他们的示例和 MySQL 表中的一些数据来填充基本的 Google 面积图。这是我所拥有的:

    <?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>

这是我的回声结果:

    [{"Checkout":"3","BookCount":"19"},{"Checkout":"4","BookCount":"157"},{"Checkout":"5","BookCount":"30"},{"Checkout":"6","BookCount":"45"},{"Checkout":"7","BookCount":"688"},{"Checkout":"8","BookCount":"391"}]

我知道谷歌不喜欢这样,但我无法理解如何格式化它以便它这样做???

4

2 回答 2

0

以下是我使用 SQL -> Array -> Google DataTable JSON String 的方法(我没有参考其他人是如何做到的,所以可能有更好的编码方法)。请注意,$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>
于 2012-10-12T16:13:35.987 回答
0
$numBooks[] = array("Checkout", "BookCount");
while ($row = mysql_fetch_array($result)) {
        $numBooks[] = $row;
};

以所需格式呈现数据

于 2012-08-21T04:50:35.397 回答