谷歌图表:饼图没有正确计算百分比?

时间:2016-06-29 21:29:10

标签: bash charts google-visualization pie-chart

我使用以下代码作为“Google Chart”。这是饼图。

#! /bin/bash

export DISPLAY=":11.0"

## Define variables 


pname1=$(awk 'NR == 1 {print $1}' /home/gdata)
active1=$(awk 'NR == 1 {print $2}' /home/gdata)
total1=$(awk 'NR == 1 {print $3}' /home/gdata)
percent1=$(awk 'NR == 1 {print $4}' /home/gdata)

TEMP=$(mktemp -t chart.XXXXX)
cat > $TEMP <<EOF

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

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

  // Set a callback to run when the Google Visualization API is loaded.
  google.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', 'Title');
    data.addColumn('number', 'Value');
    data.addRows([
      ['Active', $active1],
      ['Total', $total1]
    ]);

    // Set chart options
    var options = {'title':'$pname1 $percent1',
                   'width':400,
                   'height':300};

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

<body>
<!--Div that will hold the pie chart-->
<table class="columns">
 <tr>
<td><div id="chart_div1" style="border: 1px solid #ccc"></div></td>
</tr>
</table>
 </body> 
</html>
EOF

# open browser
case $(uname) in
Darwin)
  open -a /Applications/Google\ Chrome.app $TEMP
  ;;

Linux|SunOS)
  firefox $TEMP
  ;;
esac

正在发生的事情是我的变量$ active和$ total与渲染图表时的$ percent变量不匹配。例如,在此图表中,“total”表示483,“active”表示85. 85是483的17.6%。在饼图中,85表示为15%。

enter image description here

enter image description here

有没有办法让图表尝试弥补总数为483的百分比,而不是100?

1 个答案:

答案 0 :(得分:1)

数据不起作用。总计是总计,除非它是唯一的值,否则不能有代表总计的饼图。在这种情况下,你只需要一个实心圆圈。您可以做的是将Active与Inactive进行比较。要获取非活动数字,只需从总计中减去活动数。

data.addRows([
  ['Active', $active1],
  ['Inactive', $total1 - $active1]
]);