We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d6904a4 commit ddfa33eCopy full SHA for ddfa33e
src/tickFormat.js
@@ -1,10 +1,10 @@
1
import {tickStep} from "d3-array";
2
import {format, formatPrefix, formatSpecifier, precisionFixed, precisionPrefix, precisionRound} from "d3-format";
3
4
-export default function tickFormat(start, stop, count, specifier) {
+export default function tickFormat(start, stop, count, specifierIn) {
5
var step = tickStep(start, stop, count),
6
precision;
7
- specifier = formatSpecifier(specifier == null ? ",f" : specifier);
+ const specifier = formatSpecifier(specifierIn == null ? ",f" : specifierIn);
8
switch (specifier.type) {
9
case "s": {
10
var value = Math.max(Math.abs(start), Math.abs(stop));
@@ -21,7 +21,14 @@ export default function tickFormat(start, stop, count, specifier) {
21
}
22
case "f":
23
case "%": {
24
- if (specifier.precision == null && !isNaN(precision = precisionFixed(step))) specifier.precision = precision - (specifier.type === "%") * 2;
+ if (specifier.precision == null) {
25
+ if (step === 0) {
26
+ if (specifierIn == null) specifier.trim = true;
27
+ }
28
+ else if (!isNaN(precision = precisionFixed(step))) {
29
+ specifier.precision = precision - (specifier.type === "%") * 2;
30
31
32
break;
33
34
test/tickFormat-test.js
@@ -40,4 +40,20 @@ it("tickFormat(start, stop, count) uses the default precision when the domain is
40
const f = tickFormat(0, NaN, 10);
41
assert.strictEqual(f + "", " >-,f");
42
assert.strictEqual(f(0.12), "0.120000");
43
+
44
+ const f2 = tickFormat(0.12, NaN, 10);
45
+ assert.strictEqual(f2 + "", " >-,f");
46
+ assert.strictEqual(f2(0.12), "0.120000");
47
+});
48
49
+it("tickFormat(start, stop, count) uses the default precision with trimming when the domain is collapsed", () => {
50
+ const f = tickFormat(5.5, 5.5, 10);
51
+ assert.strictEqual(f + "", " >-,~f");
52
+ assert.strictEqual(f(0), "0");
53
+ assert.strictEqual(f(5.5), "5.5");
54
+ assert.strictEqual(f(Math.PI), "3.141593");
55
+ assert.strictEqual(f(-Math.PI), "−3.141593");
56
+ assert.strictEqual(f(Math.PI * 1e7), "31,415,926.535898");
57
+ assert.strictEqual(f(-Math.PI * 1e7), "−31,415,926.535898");
58
});
59
0 commit comments