musigh
musigh

Reputation: 175

Animation is not working for third bar in stacked bar chart in Google Charts (Visualization)

I am using Google Charts (Google Visualization), it was working fine . But now, I am facing a weird problem in animation. It is a stacked bar chart. First two columns (bars) is animating flawlessly but third column (bar) is coming at once, animation is not working for the last bar (third column).

I have tried with total 2 bars and now 2nd bar's animation is not working (came at once). It is clear that problem is in last bar. Is it a flaw in stacked bar chart from Google Chart's end?

Here is my code:

            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);

Upvotes: 1

Views: 193

Answers (1)

WhiteHat
WhiteHat

Reputation: 61222

i've encountered various bugs when using animation on startup,
specifically when a DataView is used to draw the chart

a workaround is to convert the DataView into a DataTable before drawing the chart,
you can use method --> toDataTable()

view.toDataTable()

which does seem to help in this situation,
see following working snippet...

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>

Upvotes: 1

Related Questions