Skip to content

Commit 3eb988f

Browse files
committed
Refactor d3_scale_linearTickFormat to only compute precision when necessary.
Add unit test cases for % format.
1 parent bf932d0 commit 3eb988f

2 files changed

Lines changed: 15 additions & 18 deletions

File tree

src/scale/linear.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,20 @@ function d3_scale_linearTickFormat(domain, m, format) {
120120
var formatString = format ?
121121
format.replace(d3_format_re,
122122
function(a, b, c, d, e, f, g, h, i, j) {
123-
// Compute "decimal precision" of the tick step size, i.e., the position of its last
124-
// significant digit with respect to the decimal point.
125-
var decimalPrecisionStep = decimalPrecision(range[2]);
126-
var precision;
127-
if (j==="s" || j==="g" || j==="e") {
128-
// For these formats, "precision" specifies the number of significant digits, which equals
129-
// one plus the difference between the decimal precision of the range's maximum absolute
130-
// value (which will equal one of its bounds) and the tick step's decimal precision.
131-
var precisionMaxAbsValue = decimalPrecision(Math.max(Math.abs(range[0]), Math.abs(range[1])));
132-
precision = Math.abs(decimalPrecisionStep - precisionMaxAbsValue)+1;
133-
if (j==="e") {
134-
// The digit before the decimal point counts as one.
135-
precision -= 1;
136-
}
137-
} else {
138-
// Formats such as "f" use decimal precision.
139-
precision = decimalPrecisionStep;
123+
function computePrecision() {
124+
// Compute the "decimal precision" of the tick step size, i.e., the position of its last
125+
// significant digit with respect to the decimal point.
126+
var decimalPrecisionStep = decimalPrecision(range[2]);
127+
return (j==="s" || j==="g" || j==="e") ?
128+
// For formats "s" and "g", "precision" specifies the number of significant digits, which
129+
// equals one plus the difference between the decimal precision of the range's maximum
130+
// absolute value (which equals one of its bounds) and the tick step's decimal precision.
131+
// Format "e" is similar except that the digit before the decimal point counts as one.
132+
Math.abs(decimalPrecisionStep - decimalPrecision(Math.max(Math.abs(range[0]), Math.abs(range[1])))) + (j!=="e")*1 :
133+
// Formats such as "f" and "%" depend only on step precision.
134+
decimalPrecisionStep - (j==="%")*2;
140135
}
141-
return [ b, c, d, e, f, g, h, i || "." + (precision - (j === "%") * 2), j ].join("");
136+
return [ b, c, d, e, f, g, h, i || "." + computePrecision(), j ].join("");
142137
})
143138
: ",." + decimalPrecision(range[2]) + "f";
144139
return d3.format(formatString);

test/scale/linear-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ suite.addBatch({
198198
assert.strictEqual(x.tickFormat(20,"g")(x.ticks(20)[0]), "0.010")
199199
assert.strictEqual(x.tickFormat(10,"e")(x.ticks(10)[0]), "1e-2")
200200
assert.strictEqual(x.tickFormat(20,"e")(x.ticks(20)[0]), "1.0e-2")
201+
assert.strictEqual(x.tickFormat(10,"%")(x.ticks(10)[0]), "1%")
202+
assert.strictEqual(x.tickFormat(20,"%")(x.ticks(10)[0]), "1.0%")
201203
var x = d3.scale.linear().domain([1000, 1001]);
202204
assert.strictEqual(x.tickFormat(3)(x.ticks(3)[1]), "1,000.5");
203205
assert.strictEqual(x.tickFormat(3,",g")(x.ticks(3)[1]), "1,000.5");

0 commit comments

Comments
 (0)