javascript - Update Chart JS data dynamically and add new data, remove old data to create timeline "like" char

I'm building a realtime Chart JS chart which is meant to show data and update every few seconds, I

I'm building a realtime Chart JS chart which is meant to show data and update every few seconds, I attach my chart JS code below, which randomises data on page load, I have a labels array which I need to change to whatever the current date/time is, and after every X number of items it removes the previous so that the graph is essentially a timeline showing real time stats, I'm first trying to figure out how to get my chart to update data every few seconds and aren't sure how to do this:

$(document).ready(function(){

  // Random data on page load
  var datasetValue = [];
  var count = 70;
  for (var j = 0; j < count; j++) {
    datasetValue[j] = {
      data: [
        Math.round(Math.random() * 100),
        Math.round(Math.random() * 100)-10,
        Math.round(Math.random() * 100),
        Math.round(Math.random() * 100)-10,
        Math.round(Math.random() * 100),
        Math.round(Math.random() * 100)-10,
        Math.round(Math.random() * 100)
      ]
    }
  }
  var mydata = {
    datasets: datasetValue
  }

  // Our chart
  var ctx = document.getElementById('status-chart').getContext('2d');
  var chart = new Chart(ctx, {
    type: 'line',
    data: {
      labels: ["January", "February", "March", "April", "May", "June", "July"],
      datasets: [{
        label: "Max",
        fill: false,
        borderColor: '#f44336',
        data: mydata.datasets[1].data,
      }, {
        label: "Free",
        fill: false,
        borderColor: '#4CAF50',
        data: mydata.datasets[3].data,
      }, {
        label: "Total",
        fill: false,
        borderColor: '#607D8B',
        data: mydata.datasets[2].data,
      }, {
        label: "Used",
        fill: false,
        borderColor: '#2196F3',
        data: mydata.datasets[4].data,
      }]
    },
    options: {
      legend: {
        display: true,
        labels: {
          fontColor: '#000'
        }
      }
    }
  });
});

I'm building a realtime Chart JS chart which is meant to show data and update every few seconds, I attach my chart JS code below, which randomises data on page load, I have a labels array which I need to change to whatever the current date/time is, and after every X number of items it removes the previous so that the graph is essentially a timeline showing real time stats, I'm first trying to figure out how to get my chart to update data every few seconds and aren't sure how to do this:

$(document).ready(function(){

  // Random data on page load
  var datasetValue = [];
  var count = 70;
  for (var j = 0; j < count; j++) {
    datasetValue[j] = {
      data: [
        Math.round(Math.random() * 100),
        Math.round(Math.random() * 100)-10,
        Math.round(Math.random() * 100),
        Math.round(Math.random() * 100)-10,
        Math.round(Math.random() * 100),
        Math.round(Math.random() * 100)-10,
        Math.round(Math.random() * 100)
      ]
    }
  }
  var mydata = {
    datasets: datasetValue
  }

  // Our chart
  var ctx = document.getElementById('status-chart').getContext('2d');
  var chart = new Chart(ctx, {
    type: 'line',
    data: {
      labels: ["January", "February", "March", "April", "May", "June", "July"],
      datasets: [{
        label: "Max",
        fill: false,
        borderColor: '#f44336',
        data: mydata.datasets[1].data,
      }, {
        label: "Free",
        fill: false,
        borderColor: '#4CAF50',
        data: mydata.datasets[3].data,
      }, {
        label: "Total",
        fill: false,
        borderColor: '#607D8B',
        data: mydata.datasets[2].data,
      }, {
        label: "Used",
        fill: false,
        borderColor: '#2196F3',
        data: mydata.datasets[4].data,
      }]
    },
    options: {
      legend: {
        display: true,
        labels: {
          fontColor: '#000'
        }
      }
    }
  });
});
Share Improve this question asked Mar 7, 2019 at 12:48 Ryan HRyan H 2,9956 gold badges56 silver badges157 bronze badges 3
  • Wrap the code to draw chart in a function and call the function periodically using setInterval – Anurag Srivastava Commented Mar 7, 2019 at 12:52
  • you want to update your data with new values and remove your old ddata? – Negi Rox Commented Mar 7, 2019 at 12:59
  • Yes, that's what I'd like to achieve @NegiRox – Ryan H Commented Mar 7, 2019 at 13:16
Add a ment  | 

1 Answer 1

Reset to default 2

you can use chart.update() method with assigning a new dataset

<canvas id="myChart" width="400" height="250"></canvas>
<input type="button" value="New Data" onclick="RandomData()">


var canvas = document.getElementById('myChart');
var data = {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [
        {
            label: "My dataset",
            fill: false,
            lineTension: 0.1,
            backgroundColor: "rgba(75,192,192,0.4)",
            borderColor: "rgba(75,192,192,1)",
            borderCapStyle: 'butt',
            borderDash: [],
            borderDashOffset: 0.0,
            borderJoinStyle: 'miter',
            pointBorderColor: "rgba(75,192,192,1)",
            pointBackgroundColor: "#fff",
            pointBorderWidth: 1,
            pointHoverRadius: 5,
            pointHoverBackgroundColor: "rgba(75,192,192,1)",
            pointHoverBorderColor: "rgba(220,220,220,1)",
            pointHoverBorderWidth: 2,
            pointRadius: 5,
            pointHitRadius: 10,
            data: [65, 59, 80, 0, 56, 55, 40],
        }
    ]
};

function RandomData(){
  var newData=[];
  for(var i=0;i<7;i++){
     newData.push(Math.round(Math.random() * 100))
  }
    myLineChart.data.datasets[0].data =newData;
  myLineChart.update();
}
var option = {
    showLines: true
};
var myLineChart = Chart.Line(canvas,{
    data:data,
  options:option
});

please see a demo here

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745358016a4624230.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信