这是一个很好的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 :(得分: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])
, {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])
, {labels: ["Date", "series1"], valueRange:r});
<script src="http://dygraphs.com/dygraph-combined.js"></script>
<div id="demodiv"></div>
但这会产生另一个问题:值标签也会从Infinity
转换为1e7
。将鼠标悬停在图表上时,您会看到值为1e7
而不是无限。