Skip to content

Commit 28538c4

Browse files
committed
Default to suitable precision.
The point of this method is to pick the right precision for you!
1 parent fe797df commit 28538c4

4 files changed

Lines changed: 8 additions & 6 deletions

File tree

d3.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7476,7 +7476,8 @@
74767476
match.shift();
74777477
if (match[8] === "s") {
74787478
var prefix = d3.formatPrefix(Math.max(abs(range[0]), abs(range[1])));
7479-
match[8] = match[7] ? "f" : "r";
7479+
if (!match[7]) match[7] = "." + d3_scale_linearPrecision(prefix.scale(range[2]));
7480+
match[8] = "f";
74807481
format = d3.format(match.join(""));
74817482
return function(d) {
74827483
return format(prefix.scale(d)) + prefix.symbol;

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/linear.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ function d3_scale_linearTickFormat(domain, m, format) {
122122
match.shift();
123123
if (match[8] === "s") {
124124
var prefix = d3.formatPrefix(Math.max(abs(range[0]), abs(range[1])));
125-
match[8] = match[7] ? "f" : "r";
125+
if (!match[7]) match[7] = "." + d3_scale_linearPrecision(prefix.scale(range[2]));
126+
match[8] = "f";
126127
format = d3.format(match.join(""));
127128
return function(d) {
128129
return format(prefix.scale(d)) + prefix.symbol;

test/scale/linear-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ suite.addBatch({
223223
},
224224
"applies fixed-scale SI-prefix notation": function(d3) {
225225
var x = d3.scale.linear().domain([0, 1e6]);
226-
assert.deepEqual(x.ticks(10).map(x.tickFormat(10, "s")), ["0M", "0.1M", "0.2M", "0.3M", "0.4M", "0.5M", "0.6M", "0.7M", "0.8M", "0.9M", "1M"]);
227-
assert.deepEqual(x.ticks(10).map(x.tickFormat(10, ".1s")), ["0.0M", "0.1M", "0.2M", "0.3M", "0.4M", "0.5M", "0.6M", "0.7M", "0.8M", "0.9M", "1.0M"]);
228-
assert.deepEqual(x.ticks(10).map(x.tickFormat(10, "+$.1s")), ["+$0.0M", "+$0.1M", "+$0.2M", "+$0.3M", "+$0.4M", "+$0.5M", "+$0.6M", "+$0.7M", "+$0.8M", "+$0.9M", "+$1.0M"]);
226+
assert.deepEqual(x.ticks(10).map(x.tickFormat(10, "s")), ["0.0M", "0.1M", "0.2M", "0.3M", "0.4M", "0.5M", "0.6M", "0.7M", "0.8M", "0.9M", "1.0M"]);
227+
assert.deepEqual(x.ticks(10).map(x.tickFormat(10, ".2s")), ["0.00M", "0.10M", "0.20M", "0.30M", "0.40M", "0.50M", "0.60M", "0.70M", "0.80M", "0.90M", "1.00M"]);
228+
assert.deepEqual(x.ticks(10).map(x.tickFormat(10, "+$s")), ["+$0.0M", "+$0.1M", "+$0.2M", "+$0.3M", "+$0.4M", "+$0.5M", "+$0.6M", "+$0.7M", "+$0.8M", "+$0.9M", "+$1.0M"]);
229229
var x = d3.scale.linear().domain([0, 1e5]);
230230
assert.deepEqual(x.ticks(10).map(x.tickFormat(10, "s")), ["0k", "10k", "20k", "30k", "40k", "50k", "60k", "70k", "80k", "90k", "100k"]);
231231
var x = d3.scale.linear().domain([0, 1e-4]);

0 commit comments

Comments
 (0)