2016-07-21 72 views
0

這裏處理無窮大是一個不錯的dygraph在Dygraph

var data = [1,2,3,3,2].map((d,i) => [i, d]); 
 

 
new Dygraph(document.getElementById("demodiv"), data, {labels: ["Date", "series1"]});
<script src="http://dygraphs.com/dygraph-combined.js"></script> 
 

 
<div id="demodiv"></div>

但這裏是什麼,如果一個樣品是無窮像4/0

var infty = 4/0 
 
var data = [1,2,infty,3,2].map((d,i) => [i, d]); 
 

 
new Dygraph(document.getElementById("demodiv"), data, {labels: ["Date", "series1"]});
<script src="http://dygraphs.com/dygraph-combined.js"></script> 
 

 
<div id="demodiv"></div>

所有圖形都消失了。假定的解決方案是什麼?

回答

0

我已經解決了這個問題,在一定程度上,通過預先計算最大值

var data = [1,2,4/0,3,10,2,1,0/0,2,1] 
 
var filtered = data.filter(a => !(a == Infinity || isNaN(a))) 
 
var r = ['min', 'max'].map(name => Math[name]).map(f => f(...filtered)) 
 
new Dygraph(document.getElementById("demodiv"), data.map((d,i) => [i, d]) 
 
\t , {labels: ["Date", "series1"], valueRange:r});
<script src="http://dygraphs.com/dygraph-combined.js"></script> 
 

 
<div id="demodiv"></div>

正如你看到的,我們必須在x = 2被簡單地忽略了無窮大。不過,我希望圖表顯示它會垂直向上超出「屋頂」以顯示無窮大。我不得不這樣做手工

var data = [1,2,4/0,3,10,2,1,0/0,2,1] 
 
var filtered = data.filter(a => !(a == Infinity || isNaN(a))) 
 
var r = ['min', 'max'].map(name => Math[name]).map(f => f(...filtered)) 
 

 
// map infinities to large numbers. These nums are too large for the displayed range and did not participate in visible range definition 
 
//var m = new Map(); m.set(Infinity, Number.MAX_VALUE); m.set(-Infinity, Number.MIN_VALUE) // min/max are too large for the Dygraph 
 
var m = new Map(); m.set(Infinity, 1e7); m.set(-Infinity, -1e7) 
 
data = data.map(a => m.get(a) || a) 
 

 
new Dygraph(document.getElementById("demodiv"), data.map((d,i) => [i, d]) 
 
\t , {labels: ["Date", "series1"], valueRange:r});
<script src="http://dygraphs.com/dygraph-combined.js"></script> 
 

 
<div id="demodiv"></div>

然而,這使得另一個問題:值標籤也被轉換從Infinity1e7。將鼠標懸停在圖上時,您會看到該值爲1e7而非Infinity。