简体   繁体   English

谷歌饼图动态与MySQL和PHP

[英]google pie chart dynamic with mysql and php

I want to make dynamic generation of google pie chart with provided library of them. 我想使用提供的库来动态生成Google饼图。

I have this code for get data from mysql: 我有以下代码用于从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. 我需要将此数据转换为Java脚本。 Seen x times means how many times it is in database listed: 看到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>

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. 可以使用纯JSON创建DataTable,因此,如果您可以使PHP脚本以所需的格式将结果作为JSON对象返回,则它将起作用。

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

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

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