I have a dynamic array to show a line graph with several lines. Example:
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}]]
This part of my script will draw the lines:
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); }));
(The script I'm using is based on http://cgit.drupalcode.org/d3/tree/libraries/d3.linegraph/linegraph.js)
My problem: the data array is dynamic, I don't know beforehand what's in it. Sometimes the y value for 2005 will be null:
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}]]
How can I make the second line ignore the first object, and start at 2007?
Based on answer 1 this is what I have now, still showing the whole line:
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); }));