Created
January 4, 2021 19:06
-
-
Save LuisSevillano/d53a1dc529eef518780c6df99613e2fd to your computer and use it in GitHub Desktop.
Invert d3 scaleBand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/d3/d3-scale/blob/11777dac7d4b0b3e229d658aee3257ea67bd5ffa/src/band.js#L32 | |
x.invert = function(_) { | |
const scale = this; | |
const domain = scale.domain; | |
const paddingOuter = scale.paddingOuter(); | |
const paddingInner = scale.paddingInner(); | |
const step = scale.step(); | |
const range = scale.range(); | |
var domainIndex, | |
n = domain().length, | |
reverse = range[1] < range[0], | |
start = range[reverse - 0], | |
stop = range[1 - reverse]; | |
if (_ < start + paddingOuter * step) domainIndex = 0; | |
else if (_ > stop - paddingOuter * step) domainIndex = n - 1; | |
else domainIndex = Math.floor((_ - start - paddingOuter * step) / step); | |
return domain()[domainIndex]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment