moki
moki

Reputation: 110

D3 define line based on previous element in data array

With D3 (v4), is there a way to define my line so that a value has to equal the previous value. In other words, if a grouping variable changes, can I not draw this part of the line? I know the following doesn't work, but something like...

var line = d3.line().defined(function(d,i) { return d.year === d[i-1].year; })

Thanks!

Upvotes: 1

Views: 94

Answers (1)

moki
moki

Reputation: 110

I figured it out. You have to reference the actual data variable in the defined function AND you have to provide some logic to skip the first index, like so:

var line = d3.line().defined(function(d,i) {
  if(i>0) {
       return d.year === data[i-1].year;
    } else {
       return d.year === d.year;
    }
}

Upvotes: 1

Related Questions