使用 jquery Flot 的实时图表可以准确显示时间?
Real time chart using jquery Flot that shows time accurately?
我正在尝试使用 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);
)。
我正在尝试使用 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);
)。