我有一个动态数组来显示一条线图和几条线。例如:
var data =
[[{x:2005, y:100}, {x:2007, y:96.5}, {x:2009, y:100.3}, {x:2011, y:102.3}],
[{x:2005, y:100}, {x:2007, y:105}, {x:2009, y:102}, {x:2011, y:104}]]
我的脚本的这一部分将会绘制线条:
graph.selectAll("path.line")
.data(data)
.enter().append("path")
.attr("class", "line")
.style("stroke", function(d, i) { return d3.rgb(z(i)); })
.style("stroke-width", 2)
.attr("d", d3.svg.line()
.y(function(d) { return y(d.y); })
.x(function(d,i) { return x(i); }));
我是一名有用的助手,可以为您翻译文本。
(我使用的脚本基于 http://cgit.drupalcode.org/d3/tree/libraries/d3.linegraph/linegraph.js)
我的问题是:数据数组是动态的,我事先不知道它里面有什么。有时2005年的y值将为空:
var data =
[[{x:2005, y:100}, {x:2007, y:96.5}, {x:2009, y:100.3}, {x:2011, y:102.3}],
[{x:2005, y:null}, {x:2007, y:105}, {x:2009, y:102}, {x:2011, y:104}]]
我该如何让第二行忽略第一个对象,从2007年开始呢?
根据答案1的建议,现在我的代码是这样的,但仍然显示整行:
data =
[[{x:2005, y:100}, {x:2007, y:96.5}, {x:2009, y:100.3}, {x:2011, y:102.3}],
[{x:2005, y:null}, {x:2007, y:105}, {x:2009, y:102}, {x:2011, y:104}]];
var validatedInput = function(inptArray) {
return inptArray.filter(function(obj) {
return obj.y != null;
});
};
graph.selectAll("path.line")
.data(data, validatedInput)
.enter().append("path")
.attr("class", "line")
.style("stroke", function(d, i) { return d3.rgb(z(i)); })
.style("stroke-width", 2)
.attr("d", d3.svg.line()
.y(function(d) { return y(d.y); })
.x(function(d,i) { return x(i); }));
defined()
这行代码是如何工作的,并且让你省略空值吗? - user3768495