Scales with null/undefined/false

874 views
Skip to first unread message

Chris Rich

unread,
Dec 6, 2012, 5:59:59 PM12/6/12
to d3...@googlegroups.com
Hey all,
  So, I think there should be a pretty succinct way to wrap a function around a scale to handle null/undefined values.  Currently, i believe scales will return treat these values as 0.  But, I would like a custom color in my case to be output.  Additionally, I would like this function to retain all of the methods of the scale it is modifying.

For instance

var scale = d3.scale.linear().domain([0,10]).range(["red","blue"])

to give a custom color to the null/undefined values I could do something like:

function newScale(d){
var nullColor = "black"
return (d === undefined || d === null)?nullColor:scale(d)}

but then trying to access the scales methods would work

newScale.domain()

I could inherit the methods using d3.rebind, but then I would need a list of methods.  I would like this to be general for any type of scale (linear,log,whatever).

Any ideas?  This seems doable, but i can't work it out.

Thanks in advance.

Chris






Kai Chang

unread,
Dec 9, 2012, 11:40:06 PM12/9/12
to d3...@googlegroups.com
The SVG line shape has a .defined() method which provides support for
missing values:

https://github.com/mbostock/d3/wiki/SVG-Shapes#wiki-line_defined

Example here:

http://bl.ocks.org/3891711

Source:

https://github.com/mbostock/d3/blob/master/src/svg/line.js#L22

Support for undefined/null values to parallel coordinates would be
nice (by rendering partial polylines), so I've been thinking about
this too. A lot of the visual clutter is due to lines going to 0
because the value is null. For sparse datasets, it makes a big
difference and seems more correct.

Chris Rich

unread,
Dec 10, 2012, 12:29:01 AM12/10/12
to d3...@googlegroups.com, kai.s...@gmail.com
Hehe, apparently we are thinking along the same lines.  I just saw line.defined last week and implemented .defined in parallel coordinates on Friday.  Basically, if a dimension is null, the lineto command is made into a moveto command.  However, then I found this article that was recommend using an offscale point for null values (pdf):


The problem with skipping over an axis (for null values) is that unless the dimension with defined values are adjacent to each other, you don't get a polyline segment.  Offsetting the null value to an off axis point would still allow you to view the line.

I'm new to git.  Is there a way to fork the parallel coordinates main branch, so i can make a block's example with my changes?  I think it would be easier to understand the above with a visual example.

Chris

Kai Chang

unread,
Dec 10, 2012, 2:27:09 AM12/10/12
to d3...@googlegroups.com
Here's one to work from, based off Mike's Cost of Living example.

http://bl.ocks.org/4248862

Chris Rich

unread,
Dec 11, 2012, 12:06:37 AM12/11/12
to d3...@googlegroups.com, kai.s...@gmail.com
I forked the gist to show two ways of dealing with null values.


Not really sure which way is a better way to handle things.  They both have their pros and cons.  Thoughts?
Reply all
Reply to author
Forward
0 new messages