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

前端 未结 1 469
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-21 06:28

Right now my code looks like this:

function drawChart() {

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


        
相关标签:
1条回答
  • 2020-12-21 07:09

    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>

    0 讨论(0)
提交回复
热议问题