使用Json和PHP的Google Chart

时间:2015-04-09 04:54:29

标签: php json google-visualization

My code is this
$sth = mysql_query("SELECT * FROM client_details");
    $rows = array();
    $flag = true;
    $table = array();
    $table['cols'] = array(
    array('label' => 'Year', 'type' => 'string'),
    array('label' => 'Days', 'type' => 'number'),
    );   
    $rows = array();
    while($r = mysql_fetch_assoc($sth)) {
    $temp = array();
      $Date = $r['dob'];  
      $days = $r['days'];    
      $addeddate = date('m/d/Y', strtotime($Date . + $days . 'days'));      
      $temp[] = array('v' => (string) $addeddate);
    // Values of each slice
    $temp[] = array('v' => (int) $r['days']); 
    $rows[] = array('c' => $temp);
   }
   $table['rows'] = $rows;
   $jsonTable = json_encode($table);
   echo $jsonTable;

我想在php中用json代码绘制一个谷歌图。我写了代码。但是它没有显示图形。但是它打印了json代码。问题是什么?请帮助我..

1 个答案:

答案 0 :(得分:0)

你的json很好。错误必须在客户端代码中。

请参阅此示例,该示例使用您的json。

google.load("visualization", "1", {
    packages: ["corechart"]
});
google.setOnLoadCallback(drawChart);

function drawChart() {
    var jsonData = {"cols":[{"label":"Year","type":"string"}, {"label":"Days","type":"number"}], "rows":[{"c":[{"v":"03\/18\/2015"},{"v":1}]}, {"c":[{"v":"04\/28\/2015"},{"v":42}]}, {"c":[{"v":"05\/26\/2015"},{"v":70}]}, {"c":[{"v":"06\/23\/2015"},{"v":98}]}, {"c":[{"v":"09\/19\/2015"},{"v":186}]}, {"c":[{"v":"12\/21\/2015"},{"v":279}]}, {"c":[{"v":"03\/23\/2016"},{"v":372}]}, {"c":[{"v":"06\/24\/2016"},{"v":465}]}, {"c":[{"v":"03\/14\/2027"},{"v":558}]}]} ;

    var data = new google.visualization.DataTable(jsonData);

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, {
    });
}
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="chart_div" style="width: 900px; height: 300px;"></div>