使用沿着x軸的毫秒繪製線條圖時,我通常會發現數據點位置不一致。最常見的是,初始數據點位於y軸的左側(如下圖所示),有時最後的數據點將超出視圖框的右側/超過x軸的終點。gRaphael線形圖繪製超出軸的值
有誰知道: 1)爲什麼發生這種情況, 2)如何防止它,& /或 3)如何檢查它(我可以用變換來移動線/分,如果我知道它的時候發生了多少)。
我的代碼:
var r = Raphael("holder"),
txtattr = { font: "12px sans-serif" };
var r2 = Raphael("holder2"),
txtattr2 = { font: "12px sans-serif" };
var x = [], y = [], y2 = [], y3 = [];
for (var i = 0; i < 1e6; i++) {
x[i] = i * 10;
y[i] = (y[i - 1] || 0) + (Math.random() * 7) - 3;
}
var demoX = [[1, 2, 3, 4, 5, 6, 7],[3.5, 4.5, 5.5, 6.5, 7, 8]];
var demoY = [[12, 32, 23, 15, 17, 27, 22], [10, 20, 30, 25, 15, 28]];
var xVals = [1288885800000, 1289929440000, 1290094500000, 1290439560000, 1300721700000, 1359499228000, 1359499308000, 1359499372000];
var yVals = [80, 76, 70, 74, 74, 78, 77, 72];
var xVals2 = [1288885800000, 1289929440000];
var yVals2 = [80, 76];
var lines = r.linechart(10, 10, 300, 220, xVals, yVals, { nostroke: false, axis: "0 0 1 1", symbol: "circle", smooth: true })
.hoverColumn(function() {
this.tags = r.set();
for (var i = 0, ii = this.y.length; i < ii; i++) {
this.tags.push(r.tag(this.x, this.y[i], this.values[i], 160, 10).insertBefore(this).attr([{ fill: "#fff" }, { fill: this.symbols[i].attr("fill") }]));
}
}, function() {
this.tags && this.tags.remove();
});
lines.symbols.attr({ r: 3 });
var lines2 = r2.linechart(10, 10, 300, 220, xVals2, yVals2, { nostroke: false, axis: "0 0 1 1", symbol: "circle", smooth: true })
.hoverColumn(function() {
this.tags = r2.set();
for (var i = 0, ii = this.y.length; i < ii; i++) {
this.tags.push(r.tag(this.x, this.y[i], this.values[i], 160, 10).insertBefore(this).attr([{ fill: "#fff" }, { fill: this.symbols[i].attr("fill") }]));
}
}, function() {
this.tags && this.tags.remove();
});
lines2.symbols.attr({ r: 3 });
我必須使用gRaphael與x軸必須是在毫秒(這是後來用W /定製日期字符串)
主要的例子小提琴:http://jsfiddle.net/kcar/aNJxf/
二次例如小提琴(頁面上第四示例頻繁顯示兩個軸錯誤): http://jsfiddle.net/kcar/saBnT/
根本原因是snapEnds函數(第718行g.raphael.js),它在四捨五入的情況下(儘管在某些情況下罰款)會在其他情況下對日期加上或減去年份。
在這一點之後,還沒有一路走過,但是由於數據點在每次四捨五入變得瘋狂時都放錯了位置,而不是在沒有時,我會繼續前進並假設這導致了問題計算圖表列,在發送到snapEnds之前,這些值僅用於確認其不僅僅是接收錯誤計算的數據。從g.raphael.js
snapEnds: function(from, to, steps) {
var f = from,
t = to;
if (f == t) {
return {from: f, to: t, power: 0};
}
function round(a) {
return Math.abs(a - .5) < .25 ? ~~(a) + .5 : Math.round(a);
}
var d = (t - f)/steps,
r = ~~(d),
R = r,
i = 0;
if (r) {
while (R) {
i--;
R = ~~(d * Math.pow(10, i))/Math.pow(10, i);
}
i ++;
} else {
if(d == 0 || !isFinite(d)) {
i = 1;
} else {
while (!r) {
i = i || 1;
r = ~~(d * Math.pow(10, i))/Math.pow(10, i);
i++;
}
}
i && i--;
}
t = round(to * Math.pow(10, i))/Math.pow(10, i);
if (t < to) {
t = round((to + .5) * Math.pow(10, i))/Math.pow(10, i);
}
f = round((from - (i > 0 ? 0 : .5)) * Math.pow(10, i))/Math.pow(10, i);
return { from: f, to: t, power: i };
},
如果我將毫秒數轉換爲小時數,但這些錯誤大部分都會被消除(它們仍然會出現的差不多),但在我的情況下,這仍然不是很理想,但可以作爲快速解決其他人遇到同樣問題的方法。 – kcar
只需要指出來自Epoch的毫秒數/小時 – kcar
在調查這一點時,似乎有些時候它的線路比例關閉了(有時線路太長,所以第一個和最後一個點超出了它們應該在的位置,有時線太短),有時候只是定位。對於我的生活,我不知道爲什麼相同的數據/代碼會導致完全不同的錯誤。 – kcar