Skip to content

Commit dc792de

Browse files
committed
Support for nice polylinear log scales.
1 parent 943e2d5 commit dc792de

5 files changed

Lines changed: 23 additions & 15 deletions

File tree

d3.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,14 +2280,15 @@ d3.scale.log = function() {
22802280
scale.clamp = d3.rebind(scale, linear.clamp);
22812281

22822282
scale.nice = function() {
2283-
var d = linear.domain().map(pow),
2284-
start = d[0],
2285-
end = d[1],
2283+
var domain = linear.domain().map(pow),
2284+
last = domain.length - 1,
2285+
start = domain[0],
2286+
end = domain[last],
22862287
reverse = end < start,
22872288
min = reverse ? end : start,
2288-
max = reverse ? start : end,
2289-
domain = [log.floor(min), log.ceil(max)];
2290-
if (reverse) domain.reverse();
2289+
max = reverse ? start : end;
2290+
domain[reverse ? last : 0] = log.floor(min);
2291+
domain[reverse ? 0 : last] = log.ceil(max);
22912292
return scale.domain(domain);
22922293
};
22932294

d3.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scale/log.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ d3.scale.log = function() {
2525
scale.clamp = d3.rebind(scale, linear.clamp);
2626

2727
scale.nice = function() {
28-
var d = linear.domain().map(pow),
29-
start = d[0],
30-
end = d[1],
28+
var domain = linear.domain().map(pow),
29+
last = domain.length - 1,
30+
start = domain[0],
31+
end = domain[last],
3132
reverse = end < start,
3233
min = reverse ? end : start,
33-
max = reverse ? start : end,
34-
domain = [log.floor(min), log.ceil(max)];
35-
if (reverse) domain.reverse();
34+
max = reverse ? start : end;
35+
domain[reverse ? last : 0] = log.floor(min);
36+
domain[reverse ? 0 : last] = log.ceil(max);
3637
return scale.domain(domain);
3738
};
3839

tests/test-scale-log.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ console.log(" 10 -> ", x(10));
6969
console.log("");
7070

7171
var x = d3.scale.log();
72-
[[1.1, 10.9], [10.9, 1.1], [.7, 11.001], [123.1, 6.7], [0, .49]].forEach(function(d) {
72+
[
73+
[1.1, 10.9], [10.9, 1.1], [.7, 11.001], [123.1, 6.7], [0, .49],
74+
[.12, 1, 2.5, 3, 10.9]
75+
].forEach(function(d) {
7376
console.log("domain([" + d.map(f) + " ]).nice():");
7477
console.log("", x.domain(d).nice().domain().map(f).join(","));
7578
console.log("");

tests/test-scale-log.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ domain([ 123.100, 6.700 ]).nice():
6464
domain([ 0.000, 0.490 ]).nice():
6565
0.000, 1.000
6666

67+
domain([ 0.120, 1.000, 2.500, 3.000, 10.900 ]).nice():
68+
0.100, 1.000, 2.500, 3.000, 100.000
69+
6770
ticks:
6871
[.1, 10] -> 1, 2, 3, 4, 5, 6, 7, 8, 9, 1e+1
6972
[.1, 100] -> 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1e+1, 2e+1, 3e+1, 4e+1, 5e+1, 6e+1, 7e+1, 8e+1, 9e+1, 1e+2

0 commit comments

Comments
 (0)