Skip to content

Commit

Permalink
Fallback to linear ticks for small domains.
Browse files Browse the repository at this point in the history
Fixes #44.
Fixes #49.
Fixes #129.
  • Loading branch information
mbostock committed Sep 2, 2019
1 parent 3d3ac08 commit 4300ef7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/log.js
Expand Up @@ -86,22 +86,23 @@ export function loggish(transform) {
z = [];

if (!(base % 1) && j - i < n) {
i = Math.round(i) - 1, j = Math.round(j) + 1;
if (u > 0) for (; i < j; ++i) {
i = Math.floor(i), j = Math.ceil(j);
if (u > 0) for (; i <= j; ++i) {
for (k = 1, p = pows(i); k < base; ++k) {
t = p * k;
if (t < u) continue;
if (t > v) break;
z.push(t);
}
} else for (; i < j; ++i) {
} else for (; i <= j; ++i) {
for (k = base - 1, p = pows(i); k >= 1; --k) {
t = p * k;
if (t < u) continue;
if (t > v) break;
z.push(t);
}
}
if (!z.length) z = ticks(u, v, n);
} else {
z = ticks(i, j, Math.min(j - i, n)).map(pows);
}
Expand Down
8 changes: 8 additions & 0 deletions test/log-test.js
Expand Up @@ -299,6 +299,14 @@ tape("log.ticks() generates the expected power-of-ten ticks for small domains",
test.end();
});

tape("log.ticks() generates linear ticks when the domain extent is small", function(test) {
var s = scale.scaleLog();
test.deepEqual(s.domain([41, 42]).ticks(), [41, 41.1, 41.2, 41.3, 41.4, 41.5, 41.6, 41.7, 41.8, 41.9, 42]);
test.deepEqual(s.domain([42, 41]).ticks(), [42, 41.9, 41.8, 41.7, 41.6, 41.5, 41.4, 41.3, 41.2, 41.1, 41]);
test.deepEqual(s.domain([1600, 1400]).ticks(), [1600, 1580, 1560, 1540, 1520, 1500, 1480, 1460, 1440, 1420, 1400]);
test.end();
});

tape("log.base(base).ticks() generates the expected power-of-base ticks", function(test) {
var s = scale.scaleLog().base(Math.E);
test.deepEqual(s.domain([0.1, 100]).ticks().map(round), [0.135335283237, 0.367879441171, 1, 2.718281828459, 7.389056098931, 20.085536923188, 54.598150033144]);
Expand Down

0 comments on commit 4300ef7

Please sign in to comment.