Search code examples
javascriptchartsdygraphs

Dygraph - how can we update chart by sending 1 set of data point, rather than the entire data series


I would like to update the live data by sending 1 set of data point rather than sending whole data series array and old value should get auto deleted.


Solution

  • This can be done starting from sample of the Gallery Dynamic Update

    Calling updateOptions after updating the data series.

    Here a snipnet showing how to append a new data and delete the oldest one :

    var data = [];
    var t = new Date();
    for (var i = 10; i >= 0; i--) {
        var x = new Date(t.getTime() - i * 1000);
        data.push([x, Math.random()]);
    }
    
    var g = new Dygraph(document.getElementById("graph"), data, {height:200});
    
    window.setInterval(function() {
            var x = new Date(); 
            var y = Math.random();
            data.shift();
            data.push([x, y]);
            g.updateOptions( { 'file': data } );
          }, 1000);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dygraph/1.1.1/dygraph-combined.js"></script>
    <div id="graph" />