Skip to content

Format threshold legend tick labels - #2485

Open
lyn1dev wants to merge 1 commit into
observablehq:mainfrom
lyn1dev:threshold-legend-tick-format
Open

lyn1dev wants to merge 1 commit into
observablehq:mainfrom
lyn1dev:threshold-legend-tick-format

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 22, 2026

Copy link
Copy Markdown

Threshold-based ramp legends label their ticks with the raw threshold values, so a quantile scale (which is internally converted to a threshold scale) shows the raw quantiles:

Plot.barX([0, 1, 2, 3, 4, 5, 6], {fill: (d) => d}).plot({color: {type: "quantile", legend: true, n: 7}})

labels the ticks 0.8571428571428571, 1.7142857142857142, …, while the corresponding axis ticks and tip values are formatted with formatDefault. The same happens with an explicit threshold scale, where a threshold of 2000 is labelled 2000 instead of 2,000.

This changes the default tickFormat of threshold legends (which also serve quantile and quantize scales) from the identity to formatDefault, the formatter already used for axis ticks. An explicit tickFormat — a d3-format string, a function, or null to hide the labels — still takes precedence.

Tests

test/legend-test.js gains four cases: quantile labels are formatted, threshold labels are formatted, an explicit tickFormat string is respected, and tickFormat: null still hides the labels.

Without the change, the two formatting tests fail:

AssertionError: expected [ '0.8571428571428571', …(5) ] to deeply equal [ '0.857', '1.714', '2.571', …(3) ]
AssertionError: expected [ '0.5', '1.25', '2000' ] to deeply equal [ '0.5', '1.25', '2,000' ]

With it, npx vitest run test/legend-test.js passes (8/8). The committed snapshot test/output/penguinQuantileUnknown.html is updated accordingly (3475 becomes 3,475, and so on), which is the only snapshot that changes.

The full suite (npx vitest run) was run before and after the change; the failures in both runs are the same pre-existing, environment-specific ones (test/docs-test.js network anchors, test/plots/time-axis.ts local time zone, and canvas raster snapshots), and they also fail on an unmodified checkout. npx eslint and npx tsc pass.

Fixes #2345.

AI was used to write this code.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tick values in quantile legend don't get rounded

1 participant