Skip to content

Commit 04e4b57

Browse files
committed
More sensible ticks when not base 10.
1 parent 5ef9dcd commit 04e4b57

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

d3.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,12 +2460,12 @@ d3 = function() {
24602460
scale.ticks = function() {
24612461
var extent = d3_scaleExtent(linear.domain()), ticks = [];
24622462
if (extent.every(isFinite)) {
2463-
var b = Math.log(base), i = Math.floor(extent[0] / b), j = Math.ceil(extent[1] / b), u = pow(extent[0]), v = pow(extent[1]);
2463+
var b = Math.log(base), i = Math.floor(extent[0] / b), j = Math.ceil(extent[1] / b), u = pow(extent[0]), v = pow(extent[1]), n = base % 1 ? 2 : base;
24642464
if (log === d3_scale_logn) {
24652465
ticks.push(-Math.pow(base, -i));
2466-
for (;i++ < j; ) for (var k = 9; k > 0; k--) ticks.push(-Math.pow(base, -i) * k);
2466+
for (;i++ < j; ) for (var k = n - 1; k > 0; k--) ticks.push(-Math.pow(base, -i) * k);
24672467
} else {
2468-
for (;i < j; i++) for (var k = 1; k < 10; k++) ticks.push(Math.pow(base, i) * k);
2468+
for (;i < j; i++) for (var k = 1; k < n; k++) ticks.push(Math.pow(base, i) * k);
24692469
ticks.push(Math.pow(base, i));
24702470
}
24712471
for (i = 0; ticks[i] < u; i++) {}

d3.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scale/log.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ function d3_scale_log(linear, base, log, pow) {
3939
i = Math.floor(extent[0] / b),
4040
j = Math.ceil(extent[1] / b),
4141
u = pow(extent[0]),
42-
v = pow(extent[1]);
42+
v = pow(extent[1]),
43+
n = base % 1 ? 2 : base;
4344
if (log === d3_scale_logn) {
4445
ticks.push(-Math.pow(base, -i));
45-
for (; i++ < j;) for (var k = 9; k > 0; k--) ticks.push(-Math.pow(base, -i) * k);
46+
for (; i++ < j;) for (var k = n - 1; k > 0; k--) ticks.push(-Math.pow(base, -i) * k);
4647
} else {
47-
for (; i < j; i++) for (var k = 1; k < 10; k++) ticks.push(Math.pow(base, i) * k);
48+
for (; i < j; i++) for (var k = 1; k < n; k++) ticks.push(Math.pow(base, i) * k);
4849
ticks.push(Math.pow(base, i));
4950
}
5051
for (i = 0; ticks[i] < u; i++) {} // strip small values

test/scale/log-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,28 @@ suite.addBatch({
216216
}
217217
},
218218

219+
"base two": {
220+
topic: function(log) {
221+
return log().domain([1, 32]).base(2);
222+
},
223+
"generates ticks at powers of two": function(x) {
224+
assert.deepEqual(x.ticks().map(x.tickFormat(10, d3.format("+,d"))), [
225+
"+1", "+2", "+4", "+8", "+16", "+32"
226+
]);
227+
}
228+
},
229+
230+
"base e": {
231+
topic: function(log) {
232+
return log().domain([1, 32]).base(Math.E);
233+
},
234+
"generates ticks at powers of e": function(x) {
235+
assert.deepEqual(x.ticks().map(x.tickFormat(10, d3.format("+.6r"))), [
236+
"+1.00000", "+2.71828", "+7.38906", "+20.0855"
237+
]);
238+
}
239+
},
240+
219241
"nice": {
220242
"can nice the domain, extending it to powers of ten": function(log) {
221243
var x = log().domain([1.1, 10.9]).nice();

0 commit comments

Comments
 (0)