gpt4 book ai didi

jquery - 使用 jquery Flot 的实时图表可以准确显示时间?

转载 作者:行者123 更新时间:2023-12-01 05:27:56 25 4
gpt4 key购买 nike

我正在尝试使用Flot:http://www.flotcharts.org创建通过 ajax 更新的实时图表。我的代码基于以下内容:

var cpu = [], cpuCore = [], disk = [];
var dataset;
var totalPoints = 100;
var updateInterval = 5000;
var now = new Date().getTime();

var options = {
series: {
lines: {
lineWidth: 1.2
},
bars: {
align: "center",
fillColor: { colors: [{ opacity: 1 }, { opacity: 1}] },
barWidth: 500,
lineWidth: 1
}
},
xaxis: {
mode: "time",
tickSize: [60, "second"],
tickFormatter: function (v, axis) {
var date = new Date(v);

if (date.getSeconds() % 20 == 0) {
var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

return hours + ":" + minutes + ":" + seconds;
} else {
return "";
}
},
axisLabel: "Time",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: 'Verdana, Arial',
axisLabelPadding: 10
},
yaxes: [
{
min: 0,
max: 100,
tickSize: 5,
tickFormatter: function (v, axis) {
if (v % 10 == 0) {
return v + "%";
} else {
return "";
}
},
axisLabel: "CPU loading",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: 'Verdana, Arial',
axisLabelPadding: 6
}, {
max: 5120,
position: "right",
axisLabel: "Disk",
axisLabelUseCanvas: true,
axisLabelFontSizePixels: 12,
axisLabelFontFamily: 'Verdana, Arial',
axisLabelPadding: 6
}
],
legend: {
noColumns: 0,
position:"nw"
},
grid: {
backgroundColor: { colors: ["#ffffff", "#EDF5FF"] }
}
};

function initData() {
for (var i = 0; i < totalPoints; i++) {
var temp = [now += updateInterval, 0];

cpu.push(temp);
cpuCore.push(temp);
disk.push(temp);
}
}

function GetData() {
$.ajaxSetup({ cache: false });

$.ajax({
url: "http://www.jqueryflottutorial.com/AjaxUpdateChart.aspx",
dataType: 'json',
success: update,
error: function () {
setTimeout(GetData, updateInterval);
}
});
}

var temp;

function update(_data) {
cpu.shift();
cpuCore.shift();
disk.shift();

now += updateInterval

temp = [now, _data.cpu];
cpu.push(temp);

temp = [now, _data.core];
cpuCore.push(temp);

temp = [now, _data.disk];
disk.push(temp);

dataset = [
{ label: "CPU:" + _data.cpu + "%", data: cpu, lines: { fill: true, lineWidth: 1.2 }, color: "#00FF00" },
{ label: "Disk:" + _data.disk + "KB", data: disk, color: "#0044FF", bars: { show: true }, yaxis: 2 },
{ label: "CPU Core:" + _data.core + "%", data: cpuCore, lines: { lineWidth: 1.2}, color: "#FF0000" }
];

$.plot($("#flot-placeholder1"), dataset, options);
setTimeout(GetData, updateInterval);
}


$(document).ready(function () {
initData();

dataset = [
{ label: "CPU", data: cpu, lines:{fill:true, lineWidth:1.2}, color: "#00FF00" },
{ label: "Disk:", data: disk, color: "#0044FF", bars: { show: true }, yaxis: 2 },
{ label: "CPU Core", data: cpuCore, lines: { lineWidth: 1.2}, color: "#FF0000" }
];

$.plot($("#flot-placeholder1"), dataset, options);
setTimeout(GetData, updateInterval);
});

可以在此处查看工作示例:http://www.jqueryflottutorial.com/tester-11.html

但是为什么时间轴是从下到前呢?

而且我还注意到它不能准确地记录时间,而且它运行的时间越长,它就会变得越不同步。这可以克服吗?

最佳答案

initData() 函数的开头添加 now -=totalPoints * updateInterval;,以便当前时间位于 x 轴的右端左端。

并在 update(_data) 函数中将 now += updateInterval 更改为 now = new Date().getTime(); ,以便新数据点始终具有当前时间。

<小时/>

我还建议将 tickFormatter 函数更改为

    tickFormatter: function (v, axis) {
var date = new Date(v);
if (date.getSeconds() == 0) {
var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
return hours + ":" + minutes;
} else {
return "";
}
},

并在 document.ready() 函数末尾直接调用 GetData(); (而不是 setTimeout(GetData, updateInterval);)。

关于jquery - 使用 jquery Flot 的实时图表可以准确显示时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38847523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com