谷歌图表 - 阶梯图的动画

时间:2017-04-26 15:26:51

标签: javascript animation charts google-visualization

我有2个数据集动画的问题Google图表中的步进图表。当它只是一个LineChart时它一切都很好,但我的客户希望有SteppedArea图表。当我将类型从LineChart更改为SteppedAreaChart时,第一个数据集的动画是OK,但第二个数据集有些错误,我无法弄清楚原因。你能看看这支代码笔吗?非常感谢你

function drawStepChart() {
    var data1 = new google.visualization.DataTable();
    data1.addColumn('number', 'Year');
    data1.addColumn('number', 'One');
    data1.addColumn('number', 'Two');

    var options = {
        animation: {duration: 50},
        areaOpacity: 0
    };

    var stepchart = new google.visualization.SteppedAreaChart(document.getElementById('step'));

    var index = 0;
    var index2 = 0;
    var animate1 = function() {
        if (index < values[1].length) {
            data1.addRows([values[1][index]]);
            stepchart.draw(data1, options);
            index++;
        } else {
            if(index2 < values[0].length) {
                data1.addRows([values[0][index2]]);
                stepchart.draw(data1, options);
                index2++;
            }                
        }
    }
    google.visualization.events.addListener(stepchart, 'animationfinish', animate1);
    stepchart.draw(data1, options);
    animate1();
}       

Codepen

编辑:他们说,在谷歌图表文档中,阶梯图的动画不支持添加行。但我不确定这是问题,因为它在第一个数据集中可以正常工作吗?

1 个答案:

答案 0 :(得分:0)

看起来它无法处理第一个系列的null值,
并设置interpolateNulls: true没有帮助

作为修复,请尝试使用setValue,而不是addRows
在动画的第二部分
填写第一组行的缺失值

似乎修复了该行,请参阅以下工作代码段...

&#13;
&#13;
google.charts.load("current", {
  callback: function () {
    drawLineChart();
    drawStepChart();
  },
  packages: ["corechart", "table"]
});

// two sets of values
var values = [
  [
    [1, 1.22, null],
    [2, 1.22, null],
    [3, 1.22, null],
    [4, 1.22, null],
    [5, 1.22, null],
    [6, 1.55, null],
    [7, 1.55, null],
    [8, 1.55, null],
    [9, 1.90, null],
    [10, 1.90, null]
  ],
  [
    [1, null, 2.11],
    [2, null, 2.11],
    [3, null, 2.11],
    [4, null, 0.80],
    [5, null, 0.80],
    [6, null, 0.80],
    [7, null, 0.80],
    [8, null, 1.00],
    [9, null, 2.10],
    [10, null, 2.10]
  ]
];

function drawLineChart() {
  var data1 = new google.visualization.DataTable();
  data1.addColumn("number", "Year");
  data1.addColumn("number", "One");
  data1.addColumn("number", "Two");

  var options = {
    animation: { duration: 50 }
  };

  var linechart = new google.visualization.LineChart(
    document.getElementById("line")
  );

  var index = 0;
  var index2 = 0;
  var animate1 = function() {
    if (index < values[1].length) {
      data1.addRows([values[1][index]]);
      linechart.draw(data1, options);
      index++;
    } else {
      if (index2 < values[0].length) {
        data1.addRows([values[0][index2]]);
        linechart.draw(data1, options);
        index2++;
      }
    }
  };
  google.visualization.events.addListener(
    linechart,
    "animationfinish",
    animate1
  );
  linechart.draw(data1, options);
  animate1();
}

function drawStepChart() {
  var data1 = new google.visualization.DataTable();
  data1.addColumn("number", "Year");
  data1.addColumn("number", "One");
  data1.addColumn("number", "Two");

  var options = {
    animation: { duration: 50 },
    areaOpacity: 0
  };

  var stepchart = new google.visualization.SteppedAreaChart(
    document.getElementById("step")
  );

  var index = 0;
  var index2 = 0;
  var animate1 = function() {
    if (index < values[1].length) {
      data1.addRows([values[1][index]]);
      stepchart.draw(data1, options);
      index++;
    } else {
      if (index2 < values[0].length) {
        data1.setValue(index2, 1, values[0][index2][1]);
        stepchart.draw(data1, options);
        index2++;
      }
    }
  };
  google.visualization.events.addListener(
    stepchart,
    "animationfinish",
    animate1
  );
  stepchart.draw(data1, options);
  animate1();
}
&#13;
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="line"></div>
<div id="step"></div>
&#13;
&#13;
&#13;