Skip to content

Commit

Permalink
fix tickFormat on a collapsed domain (start == end)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Jun 17, 2024
1 parent d6904a4 commit ddfa33e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/tickFormat.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {tickStep} from "d3-array";
import {format, formatPrefix, formatSpecifier, precisionFixed, precisionPrefix, precisionRound} from "d3-format";

export default function tickFormat(start, stop, count, specifier) {
export default function tickFormat(start, stop, count, specifierIn) {
var step = tickStep(start, stop, count),
precision;
specifier = formatSpecifier(specifier == null ? ",f" : specifier);
const specifier = formatSpecifier(specifierIn == null ? ",f" : specifierIn);
switch (specifier.type) {
case "s": {
var value = Math.max(Math.abs(start), Math.abs(stop));
Expand All @@ -21,7 +21,14 @@ export default function tickFormat(start, stop, count, specifier) {
}
case "f":
case "%": {
if (specifier.precision == null && !isNaN(precision = precisionFixed(step))) specifier.precision = precision - (specifier.type === "%") * 2;
if (specifier.precision == null) {
if (step === 0) {
if (specifierIn == null) specifier.trim = true;
}
else if (!isNaN(precision = precisionFixed(step))) {
specifier.precision = precision - (specifier.type === "%") * 2;
}
}
break;
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/tickFormat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,20 @@ it("tickFormat(start, stop, count) uses the default precision when the domain is
const f = tickFormat(0, NaN, 10);
assert.strictEqual(f + "", " >-,f");
assert.strictEqual(f(0.12), "0.120000");

const f2 = tickFormat(0.12, NaN, 10);
assert.strictEqual(f2 + "", " >-,f");
assert.strictEqual(f2(0.12), "0.120000");
});

it("tickFormat(start, stop, count) uses the default precision with trimming when the domain is collapsed", () => {
const f = tickFormat(5.5, 5.5, 10);
assert.strictEqual(f + "", " >-,~f");
assert.strictEqual(f(0), "0");
assert.strictEqual(f(5.5), "5.5");
assert.strictEqual(f(Math.PI), "3.141593");
assert.strictEqual(f(-Math.PI), "−3.141593");
assert.strictEqual(f(Math.PI * 1e7), "31,415,926.535898");
assert.strictEqual(f(-Math.PI * 1e7), "−31,415,926.535898");
});

0 comments on commit ddfa33e

Please sign in to comment.