简体   繁体   English

rest api 使用 javascript 更新实时图表

[英]Live chart updating by rest api using javascript

I'm looking for a chart libraly javascript.我正在寻找一个图表库 javascript。 It can draw line chart and call to rest api every few seconds (5s or 3s) then update the line chart.它可以绘制折线图并每隔几秒(5s或3s)调用rest api然后更新折线图。 I searched its calling live chart updating by json but it draw only data in my json api i want it continue drawing.我通过 json 搜索了它的调用实时图表更新,但它只在我的 json api 中绘制数据,我希望它继续绘制。 Can you help me.你能帮助我吗。 Many many thanks.非常感谢。

If you want to have Charts in your project then you may want to take a look at Chartjs .如果你想在你的项目中使用图表,那么你可能想看看Chartjs It's a great library providing all the charts one may need, such as Line, Bar, Pie, and many others.这是一个很棒的库,提供了人们可能需要的所有图表,例如 Line、Bar、Pie 和许多其他图表。

Creating a chart is easy, eg:创建图表很容易,例如:

var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['M', 'T', 'W', 'T', 'F', 'S', 'S'],
    datasets: [{
      label: 'apples',
      data: [12, 19, 3, 17, 6, 3, 7],
      backgroundColor: "rgba(153,255,51,0.4)"
    }, {
      label: 'oranges',
      data: [2, 29, 5, 5, 2, 3, 10],
      backgroundColor: "rgba(255,153,0,0.4)"
    }]
  }
});

For additional example you may want to see this introduction to Chart.js .对于其他示例,您可能希望查看Chart.js 的介绍

For dynamically updating your Charts check directly the Updating Charts Docu .要动态更新您的图表,请直接检查更新图表文档 An example from the link:链接中的一个示例:

function addData(chart, label, data) {
    chart.data.labels.push(label);
    chart.data.datasets.forEach((dataset) => {
        dataset.data.push(data);
    });
    chart.update();
}

function removeData(chart) {
    chart.data.labels.pop();
    chart.data.datasets.forEach((dataset) => {
        dataset.data.pop();
    });
    chart.update();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM