小编ein*_*ker的帖子

D3:在多条线的折线图中跳过空值

我有一个动态数组来显示多条线的折线图。例子:

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}]]
Run Code Online (Sandbox Code Playgroud)

我脚本的这一部分将绘制线条:

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); }));
Run Code Online (Sandbox Code Playgroud)

(我使用的脚本基于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}]]
Run Code Online (Sandbox Code Playgroud)

如何让第二行忽略第一个对象,并从 2007 年开始?

根据答案 1,这就是我现在所拥有的,仍然显示整行:

data …
Run Code Online (Sandbox Code Playgroud)

javascript d3.js

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

d3.js ×1

javascript ×1