实时滚动Google LineChart

时间:2018-05-25 08:46:41

标签: javascript google-visualization

this问题之后,我尝试实施滚动的Google LineChart。该图表显示10秒的数据(计算为10 *每次数据更新的周期),并在初始化时将DataTable预设为零,以填充所需的10秒数据。

该图以20Hz(50ms周期)连续运行,从而移除第一个可见数据点并附加新数据点,使得数据点的总数(data.getNumberOfRows())是恒定的。我接近实现这一功能,但正如下面的小提琴所示,我忽略了一些事情。新点被绘制并连接到较早的点,而不是第二个最终点,并且可见数据点的数量随着时间的推移而增长。

google.load('visualization', '1.0', {
'packages': ['corechart']
});

google.setOnLoadCallback(loadChart);

function loadChart() {

    var options = {
        width: 1000,
        height: 400,
        vAxis: {
            minValue: 0,
            maxValue: 100
        },
        curveType: 'none',
        pointSize: 5,
        series: {
            0: {
                color: 'Blue'
            }
        }
    };

    var data = new google.visualization.DataTable();
    data.addColumn('number', 'X');
    data.addColumn('number', 'Data');

    data.addRows([
        [0, 0]
    ]);

    var interval = 50;
    var dataSize = data.getNumberOfRows();
    var plotSize = (10 * ((1/interval) * 1000));

    if (dataSize < plotSize) {
        for (i = 0; i < (plotSize - dataSize); i++ ) {
            data.addRows([[i, 0]]);
        }
    }

    var chart = new google.visualization.LineChart(document.getElementById('google_chart_div'));
    setInterval(drawChart, interval);

    function drawChart() {
        for (i = 1; i < data.getNumberOfRows() - 1; i++) {
            data.setValue(i-1, 1, data.getValue(i, 1));
        }
        data.insertRows(data.getNumberOfRows()-1, [[data.getNumberOfRows()-1, Math.floor(Math.random() * 100) + 1 ]]);
        chart.draw(data, options);
    }
}

Fiddle example code

1 个答案:

答案 0 :(得分:0)

我意识到我应该按照下面的代码重复每次迭代的X值。

google.load('visualization', '1.0', {
    'packages': ['corechart']
});

google.setOnLoadCallback(loadChart);

function loadChart() {

    var options = {
        width: 1000,
        height: 400,
        vAxis: {
            minValue: 0,
            maxValue: 100
        },
        curveType: 'none',
        pointSize: 5,
        series: {
            0: {
                color: 'Blue'
            }
        }
    };

      var data = new google.visualization.DataTable();
      data.addColumn('number', 'X');
      data.addColumn('number', 'Data');

      data.addRows([
          [0, 0]
      ]);

      var interval = 50;
      var dataSize = data.getNumberOfRows();
      var plotSize = (10 * ((1/interval) * 1000));

      if (dataSize < plotSize) {
          for (i = 0; i < (plotSize - dataSize); i++ ) {
              data.addRows([[i, 0]]);
          }
      }

    var chart = new google.visualization.LineChart(document.getElementById('google_chart_div'));
    setInterval(drawChart, interval);

    function drawChart() {
        data.removeRow(0);
        for (i = 0; i < data.getNumberOfRows(); i++) {
            data.setValue(i, 0, i);
        }
        data.insertRows(data.getNumberOfRows(), [[data.getNumberOfRows()-1, Math.floor(Math.random() * 100) + 1 ]]);
        chart.draw(data, options);
    }
}