Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add space/gap in a line in D3

I have a multiline chart similar to this, where each line represent a particular year and the x-axis represents days(e.g. Jan 1, Jan 2, ... Dec 31) in that particular year.

some lines are for leap years and some are for non-leap years. For a non-leap year line, I want the date Feb 29 to not display anything, i.e. a gap or a blank space in the line. Right now, my data set has null in the Feb 29 object. So D3 considers that point as 0 and stretches it all the way to bottom(x-axis).

Is there any way to achieve a space in line? Or is there a workaround for this problem? Any help is appreciated. Thank you.

like image 348
Akshay Khot Avatar asked Oct 25 '25 08:10

Akshay Khot


1 Answers

You can use line.defined: https://github.com/d3/d3/wiki/SVG-Shapes#line_defined

According to the API:

The defined accessor can be used to define where the line is defined and undefined, which is typically useful in conjunction with missing data.

If your line should be undefined where the value is null or NaN, do this:

line.defined(function(d) { return !isNaN(d.x); });
like image 75
Gerardo Furtado Avatar answered Oct 27 '25 20:10

Gerardo Furtado