【问题标题】:Google Pie Charts and PHP谷歌饼图和 PHP
【发布时间】:2012-07-14 16:53:02
【问题描述】:

我想在我的网站上有一个 Google 饼图。饼图将填充数据库中的数据。我在https://developers.google.com/chart/interactive/docs/php_example 上查看了一些示例,但在涉及 JSON 格式时我迷失了。

这里有一些例子:

 <html>
   <head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">

// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});

// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);

function drawChart() {
  var jsonData = $.ajax({
      url: "getData.php",
      dataType:"json",
      async: false
      }).responseText;

  // Create our data table out of JSON data loaded from server.
  var data = new google.visualization.DataTable(jsonData);

  // Instantiate and draw our chart, passing in some options.
  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, {width: 400, height: 240});
}

</script>
</head>

<body>
  <!--Div that will hold the pie chart-->
  <div id="chart_div"></div>
</body>
</html>

这是我迷路的sn-p(getData.php):

 <?php 

// This is just an example of reading server side data and sending it to the client.
// It reads a json formatted text file and outputs it.

 $string = file_get_contents("sampleData.json");
 echo $string;

// Instead you can query your database and parse into JSON etc etc

 ?>

我将数据存储在数据库中,而不是 JSON 格式。如何使用 MySQL 数据库查询处理 JSON 格式?如果您有一些示例或演示,我将不胜感激。

【问题讨论】:

    标签: php javascript mysql json google-visualization


    【解决方案1】:

    只是 echo php 结果 var data = new google.visualization.DataTable([&lt;?php echo $result ;?&gt;]);

    你将按照以下方式从数据库中获取数据 以下格式

                 ['Task', 'Hours per Day'],
                          ['Work',     11],
                          ['Eat',      2],
                          ['Commute',  2],
                          ['Watch TV', 2],
                          ['Sleep',    7]
    

    我认为它对你有用,我在热图中使用相同的逻辑

    【讨论】:

      【解决方案2】:

      首先要做的是查看有关json_encode() 方法的PHP 手册。它提供了如何使用 PHP 生成 JSON 的示例。

      这是来自another similar SO question的一个简短示例:

      // Get your data from DB
      $sth = mysql_query("SELECT ...");
      // Create an array
      $rows = array();
      // Loop over the DB result and add it to your array
      while($r = mysql_fetch_assoc($sth)) {
          $rows[] = $r;
      }
      // Use json_encode() to turn the array into JSON
      print json_encode($rows);
      

      如果您需要重命名数据库列,以便您的 JSON 数据在属性上获得与数据库中使用的名称不同的名称,您可以在 SQL 中使用 AS

      SELECT blog_title as title ...
      

      【讨论】:

      • 感谢您的帮助。我必须在 json 中设置标签还是在 api 的选项中设置标签?
      • @extra90 我相信您 JSON 数据中的属性名称将用作图表上的标签,如果您是这个意思?
      • 我正在寻找谷歌图表的文档,json 必须是正确的 json 格式。 json_encode 是不够的。你有什么经验吗?
      • @extra90 没有这方面的经验,但是如果您描述一下您的 JSON 的外观,我相信我们可以使用json_encode() 进行设置,应该就足够了.
      猜你喜欢
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多