简体   繁体   English

PHP mysql json Google图表

[英]PHP mysql json Google charts

I am working on a project where I am making use of the Google Charting API and I want to populate the chart using json with php mysql. 我正在开发一个使用Google Charting API的项目,并且希望使用json和php mysql来填充图表。

<?php 

$sql = $db->query("SELECT  COUNT(depots_id) AS count FROM inputs WHERE etat_input ='Valider'");


$results = array();
while ($var = $sql->fetch(PDO::FETCH_ASSOC)) {
        $results[] = $var;

}                   

$pie_chart_data = array();
foreach ($results as $result) {
    $pie_chart_data[] = array((int)$result['count']);
}
$pie_chart_data = json_encode($pie_chart_data);
?>

And the code javascript to building the chart : 和代码JavaScript来构建图表:

<script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {

        var data = google.visualization.DataTable();
        data.addColumn('number','depots_id');
        data.addRows({$pie_chart_data});

        var options = {
          title: 'My Daily Activities'
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

        chart.draw(data, options);
      }
    </script>




 <div class="col-md-6 col-sm-6 col-xs-12">
              <div id="piechart" style="width: 900px; height: 500px;"></div>


            </div>

but it shows me no results. 但这没有结果。

A similar question was asked here , and may help you identify the issue. 在这里提出类似的问题,可能会帮助您确定问题。

If this still doesn't help, could you confirm that your PHP correctly outputs the JSON as expected (and even post an example of the output)? 如果仍然不能解决问题,您是否可以确认PHP是否按预期正确输出了JSON(甚至发布了输出示例)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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