Skip to content

Commit 27ec3b8

Browse files
authored
formatNumber; filter log tickFormat function (#2078)
1 parent 7d48d4a commit 27ec3b8

File tree

10 files changed

+197
-6
lines changed

10 files changed

+197
-6
lines changed

docs/features/formats.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import * as d3 from "d3";
99

1010
These helper functions are provided for convenience as a **tickFormat** option for the [axis mark](../marks/axis.md), as the **text** option for a [text mark](../marks/text.md), or other use. See also [d3-format](https://d3js.org/d3-format), [d3-time-format](https://d3js.org/d3-time-format), and JavaScript’s built-in [date formatting](https://observablehq.com/@mbostock/date-formatting) and [number formatting](https://observablehq.com/@mbostock/number-formatting).
1111

12+
## formatNumber(*locale*) {#formatNumber}
13+
14+
```js
15+
Plot.formatNumber("en-US")(Math.PI) // "3.142"
16+
```
17+
18+
Returns a function that formats a given number according to the specified *locale*. The *locale* is a [BCP 47 language tag](https://tools.ietf.org/html/bcp47) and defaults to U.S. English.
19+
1220
## formatIsoDate(*date*) {#formatIsoDate}
1321

1422
```js

src/format.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/**
2+
* Returns a function that formats a given number according to the specified
3+
* *locale*.
4+
*
5+
* [1]: https://tools.ietf.org/html/bcp47
6+
*
7+
* @param locale - a [BCP 47 language tag][1]; defaults to U.S. English.
8+
*/
9+
export function formatNumber(locale?: string): (i: number) => string;
10+
111
/**
212
* Returns a function that formats a given month number (from 0 = January to 11
313
* = December) according to the specified *locale* and *format*.

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ export {select, selectFirst, selectLast, selectMaxX, selectMaxY, selectMinX, sel
5353
export {stackX, stackX1, stackX2, stackY, stackY1, stackY2} from "./transforms/stack.js";
5454
export {treeNode, treeLink} from "./transforms/tree.js";
5555
export {pointer, pointerX, pointerY} from "./interactions/pointer.js";
56-
export {formatIsoDate, formatWeekday, formatMonth} from "./format.js";
56+
export {formatIsoDate, formatNumber, formatWeekday, formatMonth} from "./format.js";
5757
export {scale} from "./scales.js";
5858
export {legend} from "./legends.js";

src/marks/axis.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ function inferTextChannel(scale, data, ticks, tickFormat, anchor) {
652652
// possible, or the default ISO format (2014-01-26). TODO We need a better way
653653
// to infer whether the ordinal scale is UTC or local time.
654654
export function inferTickFormat(scale, data, ticks, tickFormat, anchor) {
655-
return typeof tickFormat === "function"
655+
return typeof tickFormat === "function" && !(scale.type === "log" && scale.tickFormat)
656656
? tickFormat
657657
: tickFormat === undefined && data && isTemporal(data)
658658
? inferTimeFormat(scale.type, data, anchor) ?? formatDefault

test/marks/format-test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ it("formatMonth(locale, format) does the right thing", () => {
77
assert.strictEqual(Plot.formatMonth("en", "narrow")(0), "J");
88
});
99

10-
// GitHub Actions does not support locales.
11-
it.skip("formatMonth('fr', format) does the right thing", () => {
10+
it("formatMonth('fr', format) does the right thing", () => {
1211
assert.strictEqual(Plot.formatMonth("fr", "long")(11), "décembre");
1312
assert.strictEqual(Plot.formatMonth("fr", "short")(11), "déc.");
1413
assert.strictEqual(Plot.formatMonth("fr", "narrow")(11), "D");
@@ -27,7 +26,7 @@ it("formatMonth(locale) has the expected default", () => {
2726
assert.strictEqual(Plot.formatMonth("en", undefined)(0), "Jan");
2827
});
2928

30-
it.skip("formatMonth('fr') has the expected default", () => {
29+
it("formatMonth('fr') has the expected default", () => {
3130
assert.strictEqual(Plot.formatMonth("fr")(11), "déc.");
3231
assert.strictEqual(Plot.formatMonth("fr", undefined)(11), "déc.");
3332
});

test/output/logTickFormatFunction.svg

Lines changed: 82 additions & 0 deletions
Loading
Lines changed: 82 additions & 0 deletions
Loading

test/plot.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ for (const [name, plot] of Object.entries(plots)) {
1919
reindexMarker(root);
2020
reindexClip(root);
2121
let expected;
22-
let actual = beautify.html(root.outerHTML, {
22+
let actual = beautify.html(root.outerHTML.replaceAll(" ", "\xa0"), {
2323
indent_size: 2,
2424
inline: ["title", "tspan", "span", "svg", "a", "i"],
2525
indent_inner_html: false

test/plots/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ export * from "./linear-regression-cars.js";
152152
export * from "./linear-regression-mtcars.js";
153153
export * from "./linear-regression-penguins.js";
154154
export * from "./log-degenerate.js";
155+
export * from "./log-tick-format.js";
155156
export * from "./long-labels.js";
156157
export * from "./markers.js";
157158
export * from "./markov-chain.js";

test/plots/log-tick-format.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import * as Plot from "@observablehq/plot";
2+
3+
export async function logTickFormatFunction() {
4+
return Plot.plot({x: {type: "log", domain: [1, 4200], tickFormat: Plot.formatNumber()}});
5+
}
6+
7+
export async function logTickFormatFunctionSv() {
8+
return Plot.plot({x: {type: "log", domain: [1, 4200], tickFormat: Plot.formatNumber("sv-SE")}});
9+
}

0 commit comments

Comments
 (0)