Fallback strategy for a mismatch in domain and range cardinalities #915

Closed
yonester opened this Issue Nov 20, 2012 · 3 comments

Comments

2 participants

Consider the following case:

d3.scale.linear().domain([0, 10]).range([0, 1, 2, 3, 4]);

The cardinality of the domain and range arrays don't match, so D3 will choose a bilinear interpolation, with the domain end points being (0, 10) and the range being (0, 1). You can see this in d3 / src / scale / bilinear.js (snippet shown):

var u = uninterpolate(domain[0], domain[1]),
    i = interpolate(range[0], range[1]);

I wonder if a more intuitive fallback strategy would be to instead have the end points for the range be the first and last elements of the array. I understand that this is still a somewhat arbitrary solution to an improper use of the API (e.g., what if the array contains random values?), but I suspect that it will lead to a more "expected" result in a large portion of the cases.

Owner

mbostock commented Jan 23, 2013

In general, I prefer to document behavior with invalid inputs (even if that behavior is undefined) rather than coding defensively against it, since the latter tends to be burdensome and is never enough to defend against all possible invalid inputs. In this case there is no check that the cardinality of the domain and range are mismatched, but there is a check that the smaller of the two is greater than 2; since your domain only has two elements, you’re getting bilinear interpolation.

Fair enough. Then the document is the appropriate target for this. Forgive me as I'm new to this process; do I fork the docs, update, and submit a pull request? Or is this something you take care of as the repo master?

Owner

mbostock commented Feb 14, 2013

You can just click the Edit button the wiki. Thanks for the help!

@yonester yonester closed this Mar 20, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment