简体   繁体   中英

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.

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

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

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