Skip to content

Commit

Permalink
simpler exact ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Sep 19, 2021
1 parent 9f745d0 commit f0180a8
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@ function transformExpn(x) {
return -Math.exp(-x);
}

function pow10(x, k) {
return isFinite(x) ? +(k + "e" + x) : x < 0 ? 0 : x;
}

function exp(x, k) {
return Math.exp(x) * k;
function pow10(x) {
return isFinite(x) ? +("1e" + x) : x < 0 ? 0 : x;
}

function powp(base) {
return base === 10 ? pow10
: base === Math.E ? exp
: (x, k) => Math.pow(base, x) * k;
: base === Math.E ? Math.exp
: x => Math.pow(base, x);
}

function logp(base) {
return base === Math.E ? Math.log
: base === 10 && Math.log10
|| base === 2 && Math.log2
|| (base = Math.log(base), (x) => Math.log(x) / base);
|| (base = Math.log(base), x => Math.log(x) / base);
}

function reflect(f) {
Expand Down Expand Up @@ -90,24 +86,23 @@ export function loggish(transform) {
i = Math.floor(i), j = Math.ceil(j);
if (u > 0) for (; i <= j; ++i) {
for (k = 1; k < base; ++k) {
t = pows(i, k);
t = i < 0 ? k / pows(-i) : k * pows(i);
if (t < u) continue;
if (t > v) break;
z.push(t);
}
} else for (; i <= j; ++i) {
for (k = base - 1; k >= 1; --k) {
t = pows(i, k);
t = i > 0 ? k / pows(-i) : k * pows(i);
if (t < u) continue;
if (t > v) break;
z.push(t);
}
}
if (z.length * 2 < n) z = ticks(u, v, n);
} else {
z = ticks(i, j, Math.min(j - i, n)).map(i => pows(i, 1));
z = ticks(i, j, Math.min(j - i, n)).map(pows);
}

return r ? z.reverse() : z;
};

Expand All @@ -121,16 +116,16 @@ export function loggish(transform) {
if (count === Infinity) return specifier;
const k = Math.max(1, base * count / scale.ticks().length); // TODO fast estimate?
return d => {
let i = d / pows(Math.round(logs(d)), 1);
let i = d / pows(Math.round(logs(d)));
if (i * base < base - 0.5) i *= base;
return i <= k ? specifier(d) : "";
};
};

scale.nice = () => {
return domain(nice(domain(), {
floor: x => pows(Math.floor(logs(x)), 1),
ceil: x => pows(Math.ceil(logs(x)), 1)
floor: x => pows(Math.floor(logs(x))),
ceil: x => pows(Math.ceil(logs(x)))
}));
};

Expand Down

0 comments on commit f0180a8

Please sign in to comment.