Skip to content

Commit bf932d0

Browse files
committed
Parse the format specifier passed to d3_scale_linearTickFormat.
Add a unit test case for it.
1 parent f3e1925 commit bf932d0

2 files changed

Lines changed: 25 additions & 21 deletions

File tree

src/scale/linear.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,26 +117,29 @@ function d3_scale_linearTicks(domain, m) {
117117
function d3_scale_linearTickFormat(domain, m, format) {
118118
function decimalPrecision(value) { return -Math.floor(Math.log(value)/Math.LN10 + .01); };
119119
var range = d3_scale_linearTickRange(domain, m);
120-
// Compute "decimal precision" of the tick step size, i.e., the position of its last significant
121-
// digit with respect to the decimal point.
122-
var decimalPrecisionStep = decimalPrecision(range[2]);
123-
var precision;
124-
if (format==="s" || format==="g" || format==="e") {
125-
// For these formats, "precision" specifies the number of significant digits, which equals one
126-
// plus the difference between the decimal precision of the range's maximum absolute value
127-
// (which will equal one of its bounds) and the tick step's decimal precision.
128-
var precisionMaxAbsValue = decimalPrecision(Math.max(Math.abs(range[0]), Math.abs(range[1])));
129-
precision = Math.abs(decimalPrecisionStep - precisionMaxAbsValue)+1;
130-
if (format=="e") {
131-
// The digit before the decimal point counts as one
132-
precision -= 1;
133-
}
134-
} else {
135-
// Formats such as "f" use decimal precision.
136-
precision = decimalPrecisionStep;
137-
}
138-
var formatString = format ? format.replace(d3_format_re, function(a, b, c, d, e, f, g, h, i, j) {
139-
return [ b, c, d, e, f, g, h, i || "." + (precision - (j === "%") * 2), j ].join("");
140-
}) : ",." + precision + "f";
120+
var formatString = format ?
121+
format.replace(d3_format_re,
122+
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;
140+
}
141+
return [ b, c, d, e, f, g, h, i || "." + (precision - (j === "%") * 2), j ].join("");
142+
})
143+
: ",." + decimalPrecision(range[2]) + "f";
141144
return d3.format(formatString);
142145
}

test/scale/linear-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ suite.addBatch({
200200
assert.strictEqual(x.tickFormat(20,"e")(x.ticks(20)[0]), "1.0e-2")
201201
var x = d3.scale.linear().domain([1000, 1001]);
202202
assert.strictEqual(x.tickFormat(3)(x.ticks(3)[1]), "1,000.5");
203+
assert.strictEqual(x.tickFormat(3,",g")(x.ticks(3)[1]), "1,000.5");
203204
assert.strictEqual(x.tickFormat(3,"g")(x.ticks(3)[1]), "1000.5");
204205
assert.strictEqual(x.tickFormat(3,"e")(x.ticks(3)[1]), "1.0005e+3");
205206
assert.strictEqual(x.tickFormat(3,"s")(x.ticks(3)[1]), "1.0005k");

0 commit comments

Comments
 (0)