简体   繁体   中英

google pie chart dynamic with mysql and php

I want to make dynamic generation of google pie chart with provided library of them.

I have this code for get data from mysql:

$query = "SELECT name FROM mybase WHERE name = '$name' ";

        $mysqli = new mysqli();
        $mysqli->connect('localhost', 'blabla', 'blabla', 'blabla');
        $result = $mysqli->query($query);
        while ($row = $result->fetch_assoc()) {
            //here I cant figure out how to make data in format for pie chart
        }

I need to get this data to Java script. Seen x times means how many times it is in database listed:

<html>
  <head>
    <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([
          ['Name', 'Seen x times'],
          ['somenamefrommysql',     11],
          ['somenamefrommysql',      2],
          ['somenamefrommysql',  2],
          ['somenamefrommysql', 2],
          ['somenamefrommysql',    7]
        ]);

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

        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

A DataTable can be created from pure JSON, so if you can get your PHP script to return the results as a JSON object in the format that it requires then it will work.

See https://developers.google.com/chart/interactive/docs/reference#DataTable

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