Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Visualization: Animated Line Graph --incremental rather than all at once?

Right now my code looks like this:

function drawChart() {

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

      data.addRows([
        ['', 0],
        ['2008', 123],
        ['2010', 213],
        ['2012', 654]
      ]);

    var options = {
      hAxis: {textStyle:{color: '#FFF'}},  
      vAxis: { baseline:0, baselineColor: '#FFF', gridlineColor: '#FFF',  textStyle:{color: '#FFF'} },
      backgroundColor: 'transparent',
      legend: { position: 'none' },
      colors: ['#FFF'],
      textStyle:{color: '#FFF'},
      pointSize: 10,
      series: {
            0: { pointShape: 'star'}
        },
      animation: {startup: true, duration: 5000, easing: 'linear',}

    };



    var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

chart.draw(data, options);
  }

What I want is for my animation to incrementally reveal each row. How do I go about doing this?

Any help would be greatly appreciated.

like image 930
abc0213 Avatar asked Sep 30 '16 17:09

abc0213


People also ask

How do I animate Google Charts?

Google charts can animate smoothly in one of two ways, either on startup when you first draw the chart, or when you redraw a chart after making a change in data or options. To animate on startup: Set your chart data and options.

What is a line graph animation?

Line graph animations are ideal for comparing two sets of data over a timeline or showing a trend over time. Add unique colors, icons and more for a creative presentation. Need an animated line graph?

What is the default animation for a linear chart?

Default:'linear' animation.startup Determines if the chart will animate on the initial draw. If true, the chart will start at the baseline and animate to its final state. Type:boolean Defaultfalse Events When drawing a chart, a 'ready' event is fired once the chart is ready for external method calls.

How do I animate a transition in a chart?

Call chart.draw(), passing in your data and options. To animate a transition: Start with an already rendered chart. Modify the data table or options. Different chart types support different modifications; see Supported Modificationsto learn what modifications are supported for what chart types.


1 Answers

the chart must be drawn for animation to occur

hold on to the data and only draw one row at a time

see following working snippet...

google.charts.load('current', {
  callback: function () {
    var rawData = [
      [0, 0],
      [1, 2],
      [2, 1],
      [3, 4],
      [4, 2],
      [5, 8],
      [6, 3],
      [7, 16],
      [8, 4],
      [9, 32]
    ];

    var data = new google.visualization.DataTable({
      "cols": [
        {"id":"","label":"X","type":"number"},
        {"id":"","label":"Y","type":"number"}
      ]
    });

    var options = {
      pointSize: 4,
      animation:{
        startup: true,
        duration: 600,
        easing: 'in'
      },
      legend: 'none',
      hAxis: {
        viewWindow: {
          min: 0,
          max: 9
        }
      },
      vAxis: {
        viewWindow: {
          min: 0,
          max: 32
        }
      }
    };

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

    drawChart();
    setInterval(drawChart, 1200);

    var rowIndex = 0;
    function drawChart() {
      if (rowIndex < rawData.length) {
        data.addRow(rawData[rowIndex++]);
        chart.draw(data, options);
      }
    }
  },
  packages:['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>

EDIT

for smoother action, wait for the chart's 'ready' event before drawing again.

see following working snippet...

google.charts.load('current', {
  packages:['corechart']
}).then(function () {
  var rawData = [
    [0, 0],
    [1, 2],
    [2, 1],
    [3, 4],
    [4, 2],
    [5, 8],
    [6, 3],
    [7, 16],
    [8, 4],
    [9, 32]
  ];

  var data = new google.visualization.DataTable({
    "cols": [
      {"id":"","label":"X","type":"number"},
      {"id":"","label":"Y","type":"number"}
    ]
  });

  var options = {
    pointSize: 4,
    animation:{
      startup: true,
      duration: 600,
      easing: 'in'
    },
    legend: 'none',
    hAxis: {
      viewWindow: {
        min: 0,
        max: 9
      }
    },
    vAxis: {
      viewWindow: {
        min: 0,
        max: 32
      }
    }
  };

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

  google.visualization.events.addListener(chart, 'ready', function () {
    drawChart();
  });

  var rowIndex = 0;
  drawChart();
  function drawChart() {
    if (rowIndex < rawData.length) {
      data.addRow(rawData[rowIndex++]);
      chart.draw(data, options);
    }
  }
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
like image 144
WhiteHat Avatar answered Oct 17 '22 19:10

WhiteHat