Skip to content

Commit

Permalink
implicit trim for log ticks (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Sep 19, 2021
1 parent 3281b77 commit 8fd6d25
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/log.js
@@ -1,5 +1,5 @@
import {ticks} from "d3-array";
import {format} from "d3-format";
import {format, formatSpecifier} from "d3-format";
import nice from "./nice.js";
import {copy, transformer} from "./continuous.js";
import {initRange} from "./init.js";
Expand Down Expand Up @@ -111,10 +111,13 @@ export function loggish(transform) {
};

scale.tickFormat = function(count, specifier) {
if (count == null) count = 10;
if (specifier == null) specifier = base === 10 ? ".0e" : ",";
if (typeof specifier !== "function") specifier = format(specifier);
if (typeof specifier !== "function") {
if (!(base % 1) && (specifier = formatSpecifier(specifier)).precision == null) specifier.trim = true;
specifier = format(specifier);
}
if (count === Infinity) return specifier;
if (count == null) count = 10;
var k = Math.max(1, base * count / scale.ticks().length); // TODO fast estimate?
return function(d) {
var i = d / pows(Math.round(logs(d)));
Expand Down
31 changes: 30 additions & 1 deletion test/log-test.js
Expand Up @@ -401,14 +401,43 @@ it("log.tickFormat(count, format) returns a filtered format", () => {

it("log.tickFormat(count, specifier) returns a filtered format", () => {
const x = scaleLog().domain([1000.1, 1]);
assert.deepStrictEqual(x.ticks().map(x.tickFormat(10, ".1s")), [
assert.deepStrictEqual(x.ticks().map(x.tickFormat(10, "s")), [
"1k",
"", "", "", "", "", "", "300", "200", "100",
"", "", "", "", "", "", "30", "20", "10",
"", "", "", "", "", "", "3", "2", "1"
]);
});

it("log.tickFormat(count, specifier) trims trailing zeroes by default", () => {
const x = scaleLog().domain([100.1, 0.02]);
assert.deepStrictEqual(x.ticks().map(x.tickFormat(10, "f")), [
"100",
"", "", "", "", "", "", "", "20", "10",
"", "", "", "", "", "", "", "2", "1",
"", "", "", "", "", "", "", "0.2", "0.1",
"", "", "", "", "", "", "", "0.02"
]);
});

it("log.tickFormat(count, specifier) with base two trims trailing zeroes by default", () => {
const x = scaleLog().base(2).domain([100.1, 0.02]);
assert.deepStrictEqual(x.ticks().map(x.tickFormat(10, "f")), [
"64", "32", "16", "8", "4", "2", "1", "0.5", "0.25", "0.125", "0.0625", "0.03125"
]);
});

it("log.tickFormat(count, specifier) preserves trailing zeroes if needed", () => {
const x = scaleLog().domain([100.1, 0.02]);
assert.deepStrictEqual(x.ticks().map(x.tickFormat(10, ".1f")), [
"100.0",
"", "", "", "", "", "", "", "20.0", "10.0",
"", "", "", "", "", "", "", "2.0", "1.0",
"", "", "", "", "", "", "", "0.2", "0.1",
"", "", "", "", "", "", "", "0.0"
]);
});

it("log.ticks() returns the empty array when the domain is degenerate", () => {
const x = scaleLog();
assert.deepStrictEqual(x.domain([0, 1]).ticks(), []);
Expand Down

0 comments on commit 8fd6d25

Please sign in to comment.