Skip to content

Commit 8fd6d25

Browse files
authored
implicit trim for log ticks (#254)
1 parent 3281b77 commit 8fd6d25

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/log.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ticks} from "d3-array";
2-
import {format} from "d3-format";
2+
import {format, formatSpecifier} from "d3-format";
33
import nice from "./nice.js";
44
import {copy, transformer} from "./continuous.js";
55
import {initRange} from "./init.js";
@@ -111,10 +111,13 @@ export function loggish(transform) {
111111
};
112112

113113
scale.tickFormat = function(count, specifier) {
114+
if (count == null) count = 10;
114115
if (specifier == null) specifier = base === 10 ? ".0e" : ",";
115-
if (typeof specifier !== "function") specifier = format(specifier);
116+
if (typeof specifier !== "function") {
117+
if (!(base % 1) && (specifier = formatSpecifier(specifier)).precision == null) specifier.trim = true;
118+
specifier = format(specifier);
119+
}
116120
if (count === Infinity) return specifier;
117-
if (count == null) count = 10;
118121
var k = Math.max(1, base * count / scale.ticks().length); // TODO fast estimate?
119122
return function(d) {
120123
var i = d / pows(Math.round(logs(d)));

test/log-test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,43 @@ it("log.tickFormat(count, format) returns a filtered format", () => {
401401

402402
it("log.tickFormat(count, specifier) returns a filtered format", () => {
403403
const x = scaleLog().domain([1000.1, 1]);
404-
assert.deepStrictEqual(x.ticks().map(x.tickFormat(10, ".1s")), [
404+
assert.deepStrictEqual(x.ticks().map(x.tickFormat(10, "s")), [
405405
"1k",
406406
"", "", "", "", "", "", "300", "200", "100",
407407
"", "", "", "", "", "", "30", "20", "10",
408408
"", "", "", "", "", "", "3", "2", "1"
409409
]);
410410
});
411411

412+
it("log.tickFormat(count, specifier) trims trailing zeroes by default", () => {
413+
const x = scaleLog().domain([100.1, 0.02]);
414+
assert.deepStrictEqual(x.ticks().map(x.tickFormat(10, "f")), [
415+
"100",
416+
"", "", "", "", "", "", "", "20", "10",
417+
"", "", "", "", "", "", "", "2", "1",
418+
"", "", "", "", "", "", "", "0.2", "0.1",
419+
"", "", "", "", "", "", "", "0.02"
420+
]);
421+
});
422+
423+
it("log.tickFormat(count, specifier) with base two trims trailing zeroes by default", () => {
424+
const x = scaleLog().base(2).domain([100.1, 0.02]);
425+
assert.deepStrictEqual(x.ticks().map(x.tickFormat(10, "f")), [
426+
"64", "32", "16", "8", "4", "2", "1", "0.5", "0.25", "0.125", "0.0625", "0.03125"
427+
]);
428+
});
429+
430+
it("log.tickFormat(count, specifier) preserves trailing zeroes if needed", () => {
431+
const x = scaleLog().domain([100.1, 0.02]);
432+
assert.deepStrictEqual(x.ticks().map(x.tickFormat(10, ".1f")), [
433+
"100.0",
434+
"", "", "", "", "", "", "", "20.0", "10.0",
435+
"", "", "", "", "", "", "", "2.0", "1.0",
436+
"", "", "", "", "", "", "", "0.2", "0.1",
437+
"", "", "", "", "", "", "", "0.0"
438+
]);
439+
});
440+
412441
it("log.ticks() returns the empty array when the domain is degenerate", () => {
413442
const x = scaleLog();
414443
assert.deepStrictEqual(x.domain([0, 1]).ticks(), []);

0 commit comments

Comments
 (0)