简体   繁体   中英

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

$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). Note that $task_submissions_arr is an array populated with rows & columns from a database query (but not shown below)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM