1

我想用提供的库动态生成谷歌饼图。

我有这个从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
        }

我需要将此数据获取到 Java 脚本。看到 x 次表示它在数据库中列出的次数:

<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>
4

1 回答 1

0

可以从纯 JSON 创建 DataTable,因此如果您可以让 PHP 脚本以所需格式将结果作为 JSON 对象返回,那么它将起作用。

请参阅https://developers.google.com/chart/interactive/docs/reference#DataTable

于 2012-10-19T04:07:50.677 回答