INNER CODE UNIT · JavaScript
niceStep
oncologylab/fp-tools · src/fp_tools/resources/static_browser/app.js:75
function niceStep(range, count = 5) {
const raw = Math.max(range / count, 1e-12),
power = 10 ** Math.floor(Math.log10(raw)),
scaled = raw / power;
return (scaled <= 1 ? 1 : scaled <= 2 ? 2 : scaled <= 5 ? 5 : 10) * power;
}
function niceLimit(value) {
const number = Math.max(Math.abs(finite(value)), 1e-9),
power = 10 ** Math.floor(Math.log10(number)),
scaled = number / power;
return (scaled <= 1 ? 1 : scaled <= 2 ? 2 : scaled <= 5 ? 5 : 10) * power;
}
function niceTicks(min, max, count = 5) {
if (!Number.isFinite(min) || !Number.isFinite(max) || min === max)
return [min || 0];
const step = niceStep(max - min, count - 1),
start = Math.ceil(min / step) * step,
end = Math.floor(max / step) * step,