使用Google Visualization动画饼图

时间:2013-06-21 17:47:04

标签: jquery animation charts visualization

我正在试验谷歌图表。我想要一个饼图从0%到75%的动画(见下图)。我试图通过谷歌图表实现这一目标。我正在创建两组数据,一组将以99%开始,另一组以1%开始。我想反转和动画这些。我已经通过动画实现了更改值,但无法弄清楚如何让它们进行动画制作。

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

      google.load('visualization', '1.0', {'packages':['corechart']});

      google.setOnLoadCallback(drawChart);

      function drawChart() {

        var data = new google.visualization.DataTable();
        data.addColumn('string', 'text');
        data.addColumn('number', 'number');

        data.addRows(2);
        data.setValue(0, 0, 'Work');
        data.setValue(0, 1, 1);
        data.setValue(1, 0, 'Eat');
        data.setValue(1, 1, 99);

        var options = {
                        width:500,
                        height:500,
                        animation: {duration: 1000, easing: 'out',}
                      };

        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));

        chart.draw(data, options);

        function aniChart(d,o){
          for (var i=1; i<100; i++) {
            data.setValue(0, 1, i);
          }

          for (var i=99; i>00; i--) {
            data.setValue(1, 1, i);
          }
          setTimeout(function(){
            chart.draw(data, options);
          }, 1000);

        };

        aniChart();

      }
    </script>
  </head>

  <body>
    <div id="chart_div"></div>
  </body>
</html>

chart

3 个答案:

答案 0 :(得分:4)

我认为Google Charts api不支持此功能 - 请参阅Supported Modifications

我认为如果您使用其他图表工具(例如此http://bl.ocks.org/mbostock/1346410

)可能会更容易

答案 1 :(得分:3)

<html>
<head>
    <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart']}]}"></script>         
</head>
<body>
    <div id="piechart" style="width: 900px; height: 500px;"></div>

    <script>
              google.setOnLoadCallback(drawChart);
  function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      ['Work',     11],
      ['Eat',      2],
      ['Commute',  2],
      ['Watch TV', 2],
      ['Sleep',    7]
    ]);

    var options = {
      title: 'My Daily Activities',          
    };

    var chart = new google.visualization.PieChart(document.getElementById('piechart'));
    chart.draw(data, options);

    var counter = 0;

    var handler = setInterval(function(){ 
        counter = counter + 0.1
        options = {
          title: 'My Daily Activities',           
          slices: { 1: {offset: counter},                       
                    3: {offset: counter},                       
                    5: {offset: counter},
          }
        };
        chart.draw(data, options);

        if (counter > 0.3) clearInterval(handler);
    }, 200);        
  } 
    </script>
</body>

答案 2 :(得分:2)

使用整洁的@Muhammad动画循环解决原始问题。

初始值:

  • 工作:0%
  • 吃:100%

循环增加值1个单位并每30毫秒绘制一个饼。

当达到工作= 75%时,循环停止。

&#13;
&#13;
google.load('visualization', '1.0', {'packages':['corechart']});

google.setOnLoadCallback(drawChart);

function drawChart() {

var data = new google.visualization.DataTable();
data.addColumn('string', 'text');
data.addColumn('number', 'number');

data.addRows(2);
data.setValue(0, 0, 'Work');
data.setValue(0, 1, 0.0);
data.setValue(1, 0, 'Eat');
data.setValue(1, 1, 100.0);

var options = {
    width:500,
    height:500
};

var chart = new google.visualization.PieChart(document.getElementById('chart_div'));

chart.draw(data, options);

// initial value
var percent = 0;
// start the animation loop
var handler = setInterval(function(){
    // values increment
    percent += 1;
    // apply new values
    data.setValue(0, 1, percent);
    data.setValue(1, 1, 100 - percent);
    // update the pie
    chart.draw(data, options);
    // check if we have reached the desired value
    if (percent > 74)
        // stop the loop
        clearInterval(handler);
}, 30);

}
&#13;
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="chart_div"></div>
&#13;
&#13;
&#13;