Javascript 使用ajax更新Chartjs推送数据
这是我为使用chart.js库呈现图表而创建的代码:Javascript 使用ajax更新Chartjs推送数据,javascript,ajax,chart.js,reload,Javascript,Ajax,Chart.js,Reload,这是我为使用chart.js库呈现图表而创建的代码: $(document).ready(function(){ $.ajax({ url : "data.php", type : "JSON", success : function(data){ var lung = data.length; console.log(data);
$(document).ready(function(){
$.ajax({
url : "data.php",
type : "JSON",
success : function(data){
var lung = data.length;
console.log(data);
var timestamp_utc = [];
var temperature = [];
for(var i in data) {
timestamp_utc.push(data[i].timestamp_utc);
temperature.push(data[i].temperature);
}
var config_temp = {
labels: timestamp_utc.slice(lung-10, lung),
datasets: [
{
label: "temperature",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(0, 169, 252, 0.75)",
borderColor: "rgba(0, 169, 252, 1)",
pointRadius: "5",
pointColor: "rgba(0, 169, 252, 1)",
pointBorderColor: "#fff",
pointHoverBackgroundColor: "rgba(0, 169, 252, 1)",
pointHoverBorderColor: "rgba(0, 169, 252, 1)",
data: temperature.slice(lung-10, lung)
}]
};
var ctx_temp = $("#mycanvas");
var LineGraph_temp = new Chart(ctx_temp, {
type: 'line',
data: config_temp
});
},
error : function(data) {
}
});
});
我想每15分钟更新一次图表。
如何执行此操作?您可以使用
setInterval
方法执行代码,用更新的数据呈现图表
var pollInterval = 15000; //change this value as needed
function renderChart() {
// put your existing code to render the chart here
}
$(function () {
window.setInterval(renderChart, pollInterval);
});
setInterval(function(){//将ajax调用放入其中},900000);你的解决方案可行,但这不是我想要的。。。我希望你上传的数据发生在这种风格:是的,但如何?我考虑加入一个ajax响应数据变量。对吗?