动画不适用于Google图表(可视化)中堆积条形图中的第三条

时间:2018-09-13 08:35:43

标签: javascript charts google-visualization bar-chart stacked-chart

我正在使用Google图表(Google Visualization),它工作正常。但是现在,我在动画方面面临着一个怪异的问题。它是堆积的条形图。前两列(横条)的动画效果完美无缺,但第三列(横条)同时出现,最后一根横条(第三列)的动画无效。

我尝试使用总共2个小节,现在第二个小节的动画不起作用(一次出现)。显然问题出在最后一个小节。从Google图表开始看,堆积条形图中是否存在缺陷?

这是我的代码:

            var data = google.visualization.arrayToDataTable([
            ['Status', awating, not_interested, interested, { role: 'annotation' }],
            ['SANDRA COOMBS', 2, 4, 2, ''],
            ['VINCENT ODA', 2, 2, 2, ''],
        ]);
        arTotal = niTotal = iTotal = 0;
        for (var i = 0; i < data.getNumberOfRows(); i++) {
            if (data.getValue(i, 1) != null) {
                arTotal += data.getValue(i, 1);
            }
            if (data.getValue(i, 2) != null) {
                niTotal += data.getValue(i, 2);
            }
            if (data.getValue(i, 3) != null) {
                iTotal += data.getValue(i, 3);
            }
        }
        var view = new google.visualization.DataView(data);
        view.setColumns([0, 1,
            {
                calc: "stringify",
                sourceColumn: 1,
                type: "string",
                role: "annotation"
            },
            2, {
                calc: "stringify",
                sourceColumn: 2,
                type: "string",
                role: "annotation"
            },
            3, {
                calc: "stringify",
                sourceColumn: 3,
                type: "string",
                role: "annotation"
            }]);
        var options = {
            legend: {
                position: 'none'
            },
            chartArea: { width: width, height: height, right: right },
            isStacked: true,
            orientation: orientation.orientation,
            colors: ['#008FBE', '#BE1E2D', '#00BD90'],
            fontSize: '12',
            fontName: 'OpenSans-Regular',
            hAxis: {
                viewWindowMode: 'maximized',
            },
            vAxis: {
                viewWindowMode: 'maximized',
            },
            animation: {
                startup: true,
                duration: 1500,
                easing: 'out',
            },
        };
        var chart = new google.visualization.ColumnChart(document.getElementById("currentStatusChart"));
        google.visualization.events.addListener(chart, 'ready', readyHandler);
        chart.draw(view, options);

1 个答案:

答案 0 :(得分:1)

startup上使用动画时遇到了各种错误,
特别是在使用DataView绘制图表

一种解决方法是在绘制图表之前将DataView转换为DataTable
您可以使用方法-> toDataTable()

view.toDataTable()

在这种情况下确实有帮助,
参见以下工作片段...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
      ['Status', 'awating', 'not_interested', 'interested', { role: 'annotation' }],
      ['SANDRA COOMBS', 2, 4, 2, ''],
      ['VINCENT ODA', 2, 2, 2, ''],
  ]);
  arTotal = niTotal = iTotal = 0;
  for (var i = 0; i < data.getNumberOfRows(); i++) {
      if (data.getValue(i, 1) != null) {
          arTotal += data.getValue(i, 1);
      }
      if (data.getValue(i, 2) != null) {
          niTotal += data.getValue(i, 2);
      }
      if (data.getValue(i, 3) != null) {
          iTotal += data.getValue(i, 3);
      }
  }
  var view = new google.visualization.DataView(data);
  view.setColumns([0, 1,
      {
          calc: "stringify",
          sourceColumn: 1,
          type: "string",
          role: "annotation"
      },
      2, {
          calc: "stringify",
          sourceColumn: 2,
          type: "string",
          role: "annotation"
      },
      3, {
          calc: "stringify",
          sourceColumn: 3,
          type: "string",
          role: "annotation"
      }]);
  var options = {
      legend: {
          position: 'none'
      },
      //chartArea: { width: width, height: height, right: right },
      isStacked: true,
      //orientation: orientation.orientation,
      colors: ['#008FBE', '#BE1E2D', '#00BD90'],
      fontSize: '12',
      fontName: 'OpenSans-Regular',
      hAxis: {
          viewWindowMode: 'maximized',
      },
      vAxis: {
          viewWindowMode: 'maximized',
      },
      animation: {
          startup: true,
          duration: 1500,
          easing: 'out',
      },
  };
  var chart = new google.visualization.ColumnChart(document.getElementById("currentStatusChart"));
  chart.draw(view.toDataTable(), options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="currentStatusChart"></div>