定期更新 google 图表

Updating google chart periodically

我目前在我的网页上显示了一个静态 google 图表,想知道是否有办法定期更新它?即,每 20 秒加一个点?

我似乎找不到这方面的任何信息。请查看下面我的代码

  function drawAltitudeChart(){

          var graph = [];
          downloadUrl("Map.php", function (data){
              var xml = data.responseXML;
              var markers =     xml.documentElement.getElementsByTagName("marker");
              var dataTable = new google.visualization.DataTable();
              var options = {title:'Altitude (m above sea level)', 
                  curveType:'function', 
                  legend:{position:'bottom'},
                  is3d:true     
              };
              var chart;

              for(var i = 0; i<markers.length; i++){
                  graph[i] = ['', parseInt(markers[i].getAttribute("alt"))];   
              }

              dataTable.addColumn('string', 'id');
              dataTable.addColumn('number', 'Altitude');
              dataTable.addRows(graph);


              chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
              chart.draw(dataTable, options); 

          });
      }

基于您的确切代码的简单方法是:

(function redraw() {
    drawAltitudeChart();
    setTimeout(redraw, 20000);
})();

对于更深入和可定制的方法:(不过,我这样做时只对图表有基本了解 API。)

function AltitudeChart() {
    this.chart = new google.visualization.LineChart(
        document.getElementById('curve_chart')
    );
}

AltitudeChart.prototype = {
    draw: function() {
        var graph = [];
        downloadUrl('Map.php', function(data) {
            var xml       = data.responseXML;
            var markers   = xml.documentElement.getElementsByTagName("marker");
            var dataTable = new google.visualization.DataTable();
            var options   = {
                title: 'Altitude (m above sea level)', 
                curveType: 'function', 
                legend: { position:'bottom' },
                is3d: true     
            };

            for (var i = 0; i < markers.length; i++) {
                graph[i] = ['', parseInt(markers[i].getAttribute("alt"))];   
            }

            dataTable.addColumn('string', 'id');
            dataTable.addColumn('number', 'Altitude');
            dataTable.addRows(graph);

            this.chart.draw(dataTable, options);
        });
    },

    refreshDraw: function() {
        this.drawTimeout = setTimeout(function() {
            this.draw();
            this.refreshDraw(); // recursive call
        }.bind(this), 20000);   // every 20 seconds
    }
};

new AltitudeChart().refreshDraw();

我相信如果对图表有更深入的了解,会有更好的方法来做到这一点 API,但逻辑应该可行。

每 x 秒添加一个新点...

for(var i = 0; i<markers.length; i++) {
    graph[i] = ['', parseInt(markers[i].getAttribute("alt"))];
}

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
dataTable.addColumn('string', 'id');
dataTable.addColumn('number', 'Altitude');

var array = [];
for(var i = 0; i<graph.length; i++) {
    console.log('in loop');
    setTimeout(function(y) {
        array.push(graph[y]);
        dataTable.addRows(a);              
        chart.draw(dataTable, options); 
        array = [];
     }, i*5000, i);
}