按Google饼图中的列值/类别对行进行分组

时间:2016-03-12 01:12:23

标签: php mysql charts google-visualization

我的表格如下所示:

sub_note | item_amount | sub_cat       |
---------------------------------------|
Spotify  |  $5.99      | entertainment |
Vodafone |  $35        | phone         |
Netflix  |  $5.99      | entertainment |

我想显示一个谷歌饼图,告诉我每个类别花费了多少,即娱乐,手机等。

我使用了以下改编的Google快速入门示例,但我所看到的只是一张空白图表,其中没有数据'。

<script type="text/javascript">

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

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

  // Callback that creates and populates a data table,
  // instantiates the pie chart, passes in the data and
  // draws it.
  function drawChart() {

    // Create the data table.
     var data = new google.visualization.DataTable();
    data.addColumn('string', 'Category');
    data.addColumn('number', 'Total');
    data.addRows([
     <?php 
     $query = "SELECT SUM(sub_amount) AS sub_cat, subs_total FROM subscriptions WHERE sub_user_id ='".$user_id."' GROUP BY sub_cat ORDER BY sub_cat";
      $exec = mysqli_query($connect,$query);
      while($row = mysqli_fetch_array($exec)){
      echo "['".$row['sub_cat']."',".$row['subs_total']."],";
      }
 ?>]);

    // Set chart options
    var options = {'title':'Spending by Category',
                   'width':600,
                   'height':400};

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>

很清楚,查询代码存在问题以及如何对其进行分组,但我试图弄明白这一点。

任何指针都会很棒!

更新:感谢@WhiteHat进行SQL更正,这里是正常运行的代码:

<script type="text/javascript">

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

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

  // Callback that creates and populates a data table,
  // instantiates the pie chart, passes in the data and
  // draws it.
  function drawChart() {

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Category');
    data.addColumn('number', 'Total');
    data.addRows([
     <?php 
     $query = "SELECT sub_cat, SUM(sub_amount) as subs_total FROM subscriptions WHERE sub_user_id ='".$user_id."' GROUP BY sub_cat ORDER BY sub_cat";
      $exec = mysqli_query($connect,$query);
      while($row = mysqli_fetch_array($exec)){
      echo "['".$row['sub_cat']."',".$row['subs_total']."],";
      }
   ?>]);

    // Set chart options
    var options = {'title':'How Much Pizza I Ate Last Night',
                   'width':600,
                   'height':400};

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>

2 个答案:

答案 0 :(得分:1)

试试这个sql ...

$query = "SELECT sub_cat, SUM(sub_amount) as subs_total FROM subscriptions WHERE sub_user_id ='".$user_id."' GROUP BY sub_cat ORDER BY sub_cat";

该类别应该是第一列和小计秒。

答案 1 :(得分:0)

请测试你的sql代码是否返回了什么? 如果你在SQL中给我正确的数据集,我会帮助你。 尝试在您的JavaScript之外执行echovardump,以了解您的SQL是否良好。