为Google饼图创建数据

时间:2013-03-14 12:00:17

标签: php javascript google-api

我想这可能更像是一个数学问题,但无论如何我都会尝试!我正在使用Google饼图(https://developers.google.com/chart/interactive/docs/gallery/piechart)来显示我的数据。

如何创建一个完整的饼图,目前它只是创建了不会填满整个馅饼的小切片!

这些数据是从Wordpress收集的 - 它只计算有多少公司是学术,商业或临床。这些变量如下:

<p>Academic<?php echo $academic;?></p>
<p>Business<?php echo $business;?></p>
<p>Clinical<?php echo $clinical;?></p>

对于我输出数字1,2和1的测试数据。只需将变量放入代码中就可以创建小切片,我该如何填充馅饼?

谷歌示例中的饼图总计达24个,那么这个等式是什么? P.s我在数学上很垃圾!


      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawVisualization);
function drawVisualization() {
  // Create and populate the data table.
        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Academic', '<?php echo $academic; ?>'],
          ['Business', '<?php echo $business; ?>'],
          ['Clinical', '<?php echo $clinical; ?>']
        ]);

        var options = {
          backgroundColor: 'none',
          chartArea: {width:"221",height:"221"},
          width:'221',
          height:'221',
          legend: {position: 'none'},
          tooltip: {trigger: 'none'},
          enableInteractivity: false,
          slices: {0: {color: '#a95892'}, 1:{color: '#d7663a'}, 2:{color: '#316086'}}
        };

  // Create and draw the visualization.
  new google.visualization.PieChart(document.getElementById('chart_div')).
      draw(data, options);
}

1 个答案:

答案 0 :(得分:2)

这应该有效(至少在Code Playground中对我有用):

function drawVisualization() {
var data = google.visualization.arrayToDataTable([
    ['Type', 'Amount'],
    ['Academic', <?php echo $academic;?>],
    ['Business', <?php echo $business;?>],
    ['Clinical', <?php echo $clinical;?>]]);
new google.visualization.PieChart(document.getElementById('visualization')).draw(data, {title:"how many?"});
}

http://h3n.info/pie.png