Created
September 20, 2018 19:57
-
-
Save febret/6be0eca3972512ab2b125bac1b8cac6b to your computer and use it in GitHub Desktop.
Blog - chart domain calc
This file contains 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
export function createDomain(values, symmetrical, includeZero) { | |
const dMax = d3.max(values); | |
const dMin = d3.min(values); | |
if (symmetrical) { | |
const largest = d3.max([Math.abs(dMax), Math.abs(dMin)]); | |
return [-largest, largest]; | |
} | |
if (includeZero) { | |
if (dMin <= 0 && dMax <= 0) { | |
return [dMin, 0]; | |
} else if (dMin >= 0 && dMax >= 0) { | |
return [0, dMax]; | |
} | |
} | |
return [dMin, dMax]; | |
} | |
/** | |
* Extend domain by the given normalized amount | |
*/ | |
export function extendDomain(domain, amt) { | |
let l = ((domain[1] - domain[0]) * amt) || 1; | |
return [domain[0] - l, domain[1] + l]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment