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

.invert() method for Ordinal Scale #3022

Closed
braunagn opened this issue Nov 13, 2016 · 1 comment
Closed

.invert() method for Ordinal Scale #3022

braunagn opened this issue Nov 13, 2016 · 1 comment

Comments

@braunagn
Copy link

Like the .invert() method for continuous scales (pass a range value, receive a domain value), has the d3 team considered making an equivalent method for Ordinal scale? Several people are interested in this capability; some making work-arounds, such as: http://stackoverflow.com/questions/20758373/inversion-with-ordinal-scale/20766269?noredirect=1#comment68394750_20766269

Thank you,
Nick

@mbostock
Copy link
Member

Historically, there has not been a ordinal.invert for a few reasons:

  1. There’s no requirement that ordinal.range values are unique, so for a given range value y, there could be more than one domain value x that matches.
  2. While ordinal.domain values are keyed by string (i.e., the domain values are internally coerced to strings such that any subsequent input value x will match the domain value with the same string-coerced value), ordinal.range values are not. So you’d need to decide whether range values should also be keyed by string, or whether you’d want to use SameValueZero, strict equality, etc.
  3. Related to 2, at the time ordinal scales were first implemented, ECMAScript collections were not widely available, so unless you string-coerced range values, invert would require a linear scan of the range to find the matching value.

d3/d3-scale#60 implements ordinal.invert, choosing to string-coerce range values for indexing (which seems reasonable). That pull request stalled with a discussion of an alternative implementation, but at some point I hope to close it.

Also see d3/d3-scale#64 for band and point scales.

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