Chartjs-Fixed time frame in Chart.JS

0👍

There is no need to set TimeMin and TimeMax.
There should be a function to be called for example every 30 seconds and remove the first element of labels and data that are shown on the chart. See the updated addData() function, below.

function addData(sensorData){
    d = new Date();
    if (chart.data.datasets.length > 0) {
      chart.data.labels.push('(' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + ') ' + d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate());
      //The setTimeout function is called every 30 seconds and runs the "Action" function.
      setTimeout(Action, 30000);
      chart.data.datasets.forEach(function(dataset) {
        dataset.data.push(sensorData);
      });
      //The Action function removes the first element of displayed labels and pushed data.
      function Action() {
        chart.data.labels.shift();
        chart.data.datasets.forEach(function(dataset) {
          dataset.data.shift();
        });
      }
    window.myLine.update();
    }
  };

Leave a comment