Skip to content

Commit 507264a

Browse files
committed
formatNumber; filter log tickFormat function
1 parent 7d48d4a commit 507264a

File tree

7 files changed

+108
-2
lines changed

7 files changed

+108
-2
lines changed

docs/features/formats.md

+8
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) // "2020-01-01"
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

+10
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

+1-1
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

+1-1
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/output/logTickFormatFunction.svg

+82
Loading

test/plots/index.ts

+1
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

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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+
}

0 commit comments

Comments
 (0)