本文介紹了如何配置 flot 以在 y 軸上的零點繪制缺失的時間序列?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用flot(
I'm using flot (flot on github) to draw a graph with the following time series data:
[
[1357171200000, 1],
[1357344000000, 1],
[1357430400000, 2],
[1357516800000, 2],
[1357689600000, 3],
[1357776000000, 1]
]
As you can see there are some points in the graph wich show the sales for the given day. My json response doesn't contain sales count / data for days where no sale has happened. For example the 04th of January. How can i configure flot to draw the missing days on y-axis at point zero (because there are no sales)? As you can see in the image flot does connect the points so there are no zero points in the graph.
解決方案
Here's a solution that creates a new Array adding in missing days and setting their values to zero:
/* create and return new array padding missing days*/
function newDataArray(data) {
var startDay = data[0][0],
newData = [data[0]];
for (i = 1; i < data.length; i++) {
var diff = dateDiff(data[i - 1][0], data[i][0]);
var startDate = new Date(data[i - 1][0]);
if (diff > 1) {
for (j = 0; j < diff - 1; j++) {
var fillDate = new Date(startDate).setDate(startDate.getDate() + (j + 1));
newData.push([fillDate, 0]);
}
}
newData.push(data[i]);
}
return newData;
}
/* helper function to find date differences*/
function dateDiff(d1, d2) {
return Math.floor((d2 - d1) / (1000 * 60 * 60 * 24));
}
To use:
var data = [
[1357171200000, 1],
[1357344000000, 1],
[1357430400000, 2],
[1357516800000, 2],
[1357689600000, 3],
[1357776000000, 1]
];
var newData=newDataArray(data);
/* pass newData to flot*/
DEMO: http://jsfiddle.net/LK2gD/3/
這篇關于如何配置 flot 以在 y 軸上的零點繪制缺失的時間序列?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!