Google Pie Chart PHP MySQL

How to print a Google Pie Chart with PHP & MySQL Data?

Default Google Pie Chart Code

Thats the standard code from the official google page:

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work',     11],
          ['Eat',      2],
          ['Commute',  2],
          ['Watch TV', 2],
          ['Sleep',    7]
        ]);

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

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

        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="piechart" style="width: 900px; height: 500px;"></div>
  </body>
</html>

PHP & MySQL Code

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

<?php

$branchen = "[['Branche', 'Anzahl an Branchen'],";

$sql3 = "SELECT branche, count(branche) as count FROM userstocks u LEFT JOIN stocks s on u.stockid = s.id WHERE depotid = '$depotid' GROUP BY branche";
$result3 = $mysqli->query($sql3);
while($row3 = $result3->fetch_object()) {
	$branchen .= "['".$row3->branche."', ".$row3->count."], ";
}

$branchen = substr($branchen, 0, -2);
$branchen .= "]";

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

  function drawChart() {

    var data = google.visualization.arrayToDataTable($branchen);

    var options = {
      title: 'Branchen'
    };

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

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

echo "<div id='piechart' style='width: 900px; height: 500px;'></div>";

?>

Result

Google Pie Chart with PHP & MySQL
Google Pie Chart with PHP & MySQL

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Nach oben scrollen