Skip to content

Commit 60576ba

Browse files
committed
More flexible scale.nice. Fixes d3#1088.
1 parent 9e66a85 commit 60576ba

7 files changed

Lines changed: 87 additions & 77 deletions

File tree

d3.js

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6664,12 +6664,20 @@ d3 = function() {
66646664
dx = i0, i0 = i1, i1 = dx;
66656665
dx = x0, x0 = x1, x1 = dx;
66666666
}
6667-
if (nice = nice(x1 - x0)) {
6668-
domain[i0] = nice.floor(x0);
6669-
domain[i1] = nice.ceil(x1);
6670-
}
6667+
domain[i0] = nice.floor(x0);
6668+
domain[i1] = nice.ceil(x1);
66716669
return domain;
66726670
}
6671+
function d3_scale_niceStep(step) {
6672+
return {
6673+
floor: function(x) {
6674+
return Math.floor(x / step) * step;
6675+
},
6676+
ceil: function(x) {
6677+
return Math.ceil(x / step) * step;
6678+
}
6679+
};
6680+
}
66736681
function d3_scale_polylinear(domain, range, uninterpolate, interpolate) {
66746682
var u = [], i = [], j = 0, k = Math.min(domain.length, range.length) - 1;
66756683
if (domain[k] < domain[0]) {
@@ -6731,8 +6739,8 @@ d3 = function() {
67316739
scale.tickFormat = function(m, format) {
67326740
return d3_scale_linearTickFormat(domain, m, format);
67336741
};
6734-
scale.nice = function() {
6735-
d3_scale_nice(domain, d3_scale_linearNice);
6742+
scale.nice = function(m) {
6743+
d3_scale_nice(domain, d3_scale_linearNice(domain, m));
67366744
return rescale();
67376745
};
67386746
scale.copy = function() {
@@ -6743,16 +6751,12 @@ d3 = function() {
67436751
function d3_scale_linearRebind(scale, linear) {
67446752
return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
67456753
}
6746-
function d3_scale_linearNice(dx) {
6747-
dx = Math.pow(10, Math.round(Math.log(dx) / Math.LN10) - 1);
6748-
return dx && {
6749-
floor: function(x) {
6750-
return Math.floor(x / dx) * dx;
6751-
},
6752-
ceil: function(x) {
6753-
return Math.ceil(x / dx) * dx;
6754-
}
6755-
};
6754+
function d3_scale_linearNice(domain, m) {
6755+
return d3_scale_niceStep(m ? d3_scale_linearTickRange(domain, m)[2] : d3_scale_linearNiceStep(domain));
6756+
}
6757+
function d3_scale_linearNiceStep(domain) {
6758+
var extent = d3_scaleExtent(domain), span = extent[1] - extent[0];
6759+
return Math.pow(10, Math.round(Math.log(span) / Math.LN10) - 1);
67566760
}
67576761
function d3_scale_linearTickRange(domain, m) {
67586762
var extent = d3_scaleExtent(domain), span = extent[1] - extent[0], step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)), err = m / span * step;
@@ -6794,7 +6798,23 @@ d3 = function() {
67946798
return scale;
67956799
};
67966800
scale.nice = function() {
6797-
linear.domain(d3_scale_nice(domain, nice).map(log));
6801+
function floor(x) {
6802+
return Math.pow(base, Math.floor(Math.log(x) / Math.log(base)));
6803+
}
6804+
function ceil(x) {
6805+
return Math.pow(base, Math.ceil(Math.log(x) / Math.log(base)));
6806+
}
6807+
linear.domain(d3_scale_nice(domain, log === d3_scale_logp ? {
6808+
floor: floor,
6809+
ceil: ceil
6810+
} : {
6811+
floor: function(x) {
6812+
return -ceil(-x);
6813+
},
6814+
ceil: function(x) {
6815+
return -floor(-x);
6816+
}
6817+
}).map(log));
67986818
return scale;
67996819
};
68006820
scale.ticks = function() {
@@ -6826,25 +6846,6 @@ d3 = function() {
68266846
scale.copy = function() {
68276847
return d3_scale_log(linear.copy(), base, log, pow, domain);
68286848
};
6829-
function nice() {
6830-
return log === d3_scale_logp ? {
6831-
floor: floor,
6832-
ceil: ceil
6833-
} : {
6834-
floor: function(x) {
6835-
return -ceil(-x);
6836-
},
6837-
ceil: function(x) {
6838-
return -floor(-x);
6839-
}
6840-
};
6841-
}
6842-
function floor(x) {
6843-
return Math.pow(base, Math.floor(Math.log(x) / Math.log(base)));
6844-
}
6845-
function ceil(x) {
6846-
return Math.pow(base, Math.ceil(Math.log(x) / Math.log(base)));
6847-
}
68486849
return d3_scale_linearRebind(scale, linear);
68496850
}
68506851
var d3_scale_logFormat = d3.format(".0e");
@@ -6882,8 +6883,8 @@ d3 = function() {
68826883
scale.tickFormat = function(m, format) {
68836884
return d3_scale_linearTickFormat(domain, m, format);
68846885
};
6885-
scale.nice = function() {
6886-
return scale.domain(d3_scale_nice(domain, d3_scale_linearNice));
6886+
scale.nice = function(m) {
6887+
return scale.domain(d3_scale_nice(domain, d3_scale_linearNice(domain, m)));
68876888
};
68886889
scale.exponent = function(x) {
68896890
if (!arguments.length) return exponent;
@@ -8647,9 +8648,7 @@ d3 = function() {
86478648
return scale;
86488649
};
86498650
scale.nice = function(m) {
8650-
return scale.domain(d3_scale_nice(scale.domain(), function() {
8651-
return m;
8652-
}));
8651+
return scale.domain(d3_scale_nice(scale.domain(), m));
86538652
};
86548653
scale.ticks = function(m, k) {
86558654
var extent = d3_scaleExtent(scale.domain());

d3.min.js

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

src/scale/linear.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ function d3_scale_linear(domain, range, interpolate, clamp) {
7070
return d3_scale_linearTickFormat(domain, m, format);
7171
};
7272

73-
scale.nice = function() {
74-
d3_scale_nice(domain, d3_scale_linearNice);
73+
scale.nice = function(m) {
74+
d3_scale_nice(domain, d3_scale_linearNice(domain, m));
7575
return rescale();
7676
};
7777

@@ -86,12 +86,16 @@ function d3_scale_linearRebind(scale, linear) {
8686
return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
8787
}
8888

89-
function d3_scale_linearNice(dx) {
90-
dx = Math.pow(10, Math.round(Math.log(dx) / Math.LN10) - 1);
91-
return dx && {
92-
floor: function(x) { return Math.floor(x / dx) * dx; },
93-
ceil: function(x) { return Math.ceil(x / dx) * dx; }
94-
};
89+
function d3_scale_linearNice(domain, m) {
90+
return d3_scale_niceStep(m
91+
? d3_scale_linearTickRange(domain, m)[2]
92+
: d3_scale_linearNiceStep(domain));
93+
}
94+
95+
function d3_scale_linearNiceStep(domain) {
96+
var extent = d3_scaleExtent(domain),
97+
span = extent[1] - extent[0];
98+
return Math.pow(10, Math.round(Math.log(span) / Math.LN10) - 1);
9599
}
96100

97101
function d3_scale_linearTickRange(domain, m) {

src/scale/log.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,19 @@ function d3_scale_log(linear, base, log, pow, domain) {
3232
};
3333

3434
scale.nice = function() {
35-
linear.domain(d3_scale_nice(domain, nice).map(log));
35+
36+
function floor(x) {
37+
return Math.pow(base, Math.floor(Math.log(x) / Math.log(base)));
38+
}
39+
40+
function ceil(x) {
41+
return Math.pow(base, Math.ceil(Math.log(x) / Math.log(base)));
42+
}
43+
44+
linear.domain(d3_scale_nice(domain, log === d3_scale_logp
45+
? {floor: floor, ceil: ceil}
46+
: {floor: function(x) { return -ceil(-x); }, ceil: function(x) { return -floor(-x); }}).map(log));
47+
3648
return scale;
3749
};
3850

@@ -77,20 +89,6 @@ function d3_scale_log(linear, base, log, pow, domain) {
7789
return d3_scale_log(linear.copy(), base, log, pow, domain);
7890
};
7991

80-
function nice() {
81-
return log === d3_scale_logp
82-
? {floor: floor, ceil: ceil}
83-
: {floor: function(x) { return -ceil(-x); }, ceil: function(x) { return -floor(-x); }};
84-
}
85-
86-
function floor(x) {
87-
return Math.pow(base, Math.floor(Math.log(x) / Math.log(base)));
88-
}
89-
90-
function ceil(x) {
91-
return Math.pow(base, Math.ceil(Math.log(x) / Math.log(base)));
92-
}
93-
9492
return d3_scale_linearRebind(scale, linear);
9593
}
9694

src/scale/nice.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@ function d3_scale_nice(domain, nice) {
1010
dx = x0, x0 = x1, x1 = dx;
1111
}
1212

13-
if (nice = nice(x1 - x0)) {
14-
domain[i0] = nice.floor(x0);
15-
domain[i1] = nice.ceil(x1);
16-
}
17-
13+
domain[i0] = nice.floor(x0);
14+
domain[i1] = nice.ceil(x1);
1815
return domain;
1916
}
17+
18+
function d3_scale_niceStep(step) {
19+
return step ? {
20+
floor: function(x) { return Math.floor(x / step) * step; },
21+
ceil: function(x) { return Math.ceil(x / step) * step; }
22+
} : d3_scale_niceIdentity;
23+
}
24+
25+
var d3_scale_niceIdentity = {
26+
floor: d3_identity,
27+
ceil: d3_identity
28+
};

src/scale/pow.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function d3_scale_pow(linear, exponent, domain) {
3232
return d3_scale_linearTickFormat(domain, m, format);
3333
};
3434

35-
scale.nice = function() {
36-
return scale.domain(d3_scale_nice(domain, d3_scale_linearNice));
35+
scale.nice = function(m) {
36+
return scale.domain(d3_scale_nice(domain, d3_scale_linearNice(domain, m)));
3737
};
3838

3939
scale.exponent = function(x) {

src/time/scale.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function d3_time_scale(linear, methods, format) {
3030
};
3131

3232
scale.nice = function(m) {
33-
return scale.domain(d3_scale_nice(scale.domain(), function() { return m; }));
33+
return scale.domain(d3_scale_nice(scale.domain(), m));
3434
};
3535

3636
scale.ticks = function(m, k) {

0 commit comments

Comments
 (0)