Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scaleQuantize doesn't work as expected with reversed domain #123

Closed
patricksurry opened this issue Nov 4, 2017 · 1 comment
Closed

scaleQuantize doesn't work as expected with reversed domain #123

patricksurry opened this issue Nov 4, 2017 · 1 comment

Comments

@patricksurry
Copy link

patricksurry commented Nov 4, 2017

Using https://d3js.org/d3.v4.min.js:

fwd = d3.scaleQuantize().domain([0,1]).range([0,1,2])
bwd = d3.scaleQuantize().domain([1,0]).range([0,1,2])
pts = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
pts.map(fwd)
=> [0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2]
pts.map(bwd)
=> [0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2]

Other d3 scales with continuous domain seem to happily support the first endpoint greater than the second (super handy for swapping axes around etc), but scaleQuantize didn't do what I expected.

@mbostock
Copy link
Member

mbostock commented Nov 8, 2017

Correct—the code currently assumes that the domain is in ascending order, and the behavior is undefined if it’s not. (It uses d3.bisect internally.)

There are other scales that require ascending domains, namely d3.scaleThreshold. (d3.scaleQuantile internally computes a sorted array of quantile thresholds from an unsorted domain, so it’s likewise not possible to have a “descending” quantile scale.)

That said it seems reasonable here to support a descending quantize scale. However, I’m not sure what quantize.invertExtent should return in this case. Should it return [x0, x1] where x0 < x1 for a descending domain, or x0 > x1? My intuition is the latter, so that the order of the array returned by invertExtent is consistent with the domain. But I’m not entirely sure.

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

No branches or pull requests

2 participants