Skip to content

Commit fe797df

Browse files
committed
More standard precision interpretation.
Rather than overload the meaning of precision to bias the selection of the SI prefix, always use the standard SI prefix, and use the precision in the same sense as with fixed digits: the number of digits after the decimal point.
1 parent 621558c commit fe797df

4 files changed

Lines changed: 11 additions & 10 deletions

File tree

d3.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7475,8 +7475,8 @@
74757475
var match = d3_format_re.exec(format);
74767476
match.shift();
74777477
if (match[8] === "s") {
7478-
var prefix = d3.formatPrefix(Math.max(abs(range[0]), abs(range[1])) * (match[7] ? Math.pow(10, -match[7].substring(1)) : 1));
7479-
match[7] = "." + d3_scale_linearPrecision(prefix.scale(range[2])), match[8] = "f";
7478+
var prefix = d3.formatPrefix(Math.max(abs(range[0]), abs(range[1])));
7479+
match[8] = match[7] ? "f" : "r";
74807480
format = d3.format(match.join(""));
74817481
return function(d) {
74827482
return format(prefix.scale(d)) + prefix.symbol;
@@ -7501,7 +7501,7 @@
75017501
}
75027502
function d3_scale_linearFormatPrecision(type, range) {
75037503
var p = d3_scale_linearPrecision(range[2]);
7504-
return type in d3_scale_linearFormatSignificant ? Math.abs(p - d3_scale_linearPrecision(Math.max(Math.abs(range[0]), Math.abs(range[1])))) + +(type !== "e") : p - (type === "%") * 2;
7504+
return type in d3_scale_linearFormatSignificant ? Math.abs(p - d3_scale_linearPrecision(Math.max(abs(range[0]), abs(range[1])))) + +(type !== "e") : p - (type === "%") * 2;
75057505
}
75067506
d3.scale.log = function() {
75077507
return d3_scale_log(d3.scale.linear().domain([ 0, 1 ]), 10, true, [ 1, 10 ]);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ function d3_scale_linearTickFormat(domain, m, format) {
121121
var match = d3_format_re.exec(format);
122122
match.shift();
123123
if (match[8] === "s") {
124-
var prefix = d3.formatPrefix(Math.max(abs(range[0]), abs(range[1])) * (match[7] ? Math.pow(10, -match[7].substring(1)) : 1));
125-
match[7] = "." + d3_scale_linearPrecision(prefix.scale(range[2])), match[8] = "f";
124+
var prefix = d3.formatPrefix(Math.max(abs(range[0]), abs(range[1])));
125+
match[8] = match[7] ? "f" : "r";
126126
format = d3.format(match.join(""));
127127
return function(d) {
128128
return format(prefix.scale(d)) + prefix.symbol;
@@ -152,6 +152,6 @@ function d3_scale_linearPrecision(value) {
152152
function d3_scale_linearFormatPrecision(type, range) {
153153
var p = d3_scale_linearPrecision(range[2]);
154154
return type in d3_scale_linearFormatSignificant
155-
? Math.abs(p - d3_scale_linearPrecision(Math.max(Math.abs(range[0]), Math.abs(range[1])))) + +(type !== "e")
155+
? Math.abs(p - d3_scale_linearPrecision(Math.max(abs(range[0]), abs(range[1])))) + +(type !== "e")
156156
: p - (type === "%") * 2;
157157
}

test/scale/linear-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +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")), ["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, "+$s")), ["+$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")), ["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"]);
228229
var x = d3.scale.linear().domain([0, 1e5]);
229230
assert.deepEqual(x.ticks(10).map(x.tickFormat(10, "s")), ["0k", "10k", "20k", "30k", "40k", "50k", "60k", "70k", "80k", "90k", "100k"]);
230231
var x = d3.scale.linear().domain([0, 1e-4]);

0 commit comments

Comments
 (0)