From ccef9f38f26732b68ba04576da77a2b149059f20 Mon Sep 17 00:00:00 2001
From: lyn1dev <78355114+lyn1dev@users.noreply.github.com>
Date: Sun, 22 Nov 2026 23:41:39 +0100
Subject: [PATCH] Format threshold legend tick labels
---
src/legends/ramp.js | 3 ++-
test/legend-test.js | 27 +++++++++++++++++++++++++
test/output/penguinQuantileUnknown.html | 8 ++++----
3 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/src/legends/ramp.js b/src/legends/ramp.js
index c2a8dcbcae..0f48039afb 100644
--- a/src/legends/ramp.js
+++ b/src/legends/ramp.js
@@ -1,6 +1,7 @@
import {quantize, interpolateNumber, piecewise, format, scaleBand, scaleLinear, axisBottom} from "d3";
import {inferFontVariant} from "../axes.js";
import {createContext, create} from "../context.js";
+import {formatDefault} from "../format.js";
import {map, maybeNumberChannel} from "../options.js";
import {interpolatePiecewise} from "../scales/quantitative.js";
import {applyInlineStyles, impliedString, maybeClassName, offset} from "../style.js";
@@ -113,7 +114,7 @@ export function legendRamp(color, options) {
const thresholds = domain;
const thresholdFormat =
- tickFormat === undefined ? (d) => d : typeof tickFormat === "string" ? format(tickFormat) : tickFormat;
+ tickFormat === undefined ? formatDefault : typeof tickFormat === "string" ? format(tickFormat) : tickFormat;
// Construct a linear scale with evenly-spaced ticks for each of the
// thresholds; the domain extends one beyond the threshold extent.
diff --git a/test/legend-test.js b/test/legend-test.js
index ed99cf8695..e79de1b8dc 100644
--- a/test/legend-test.js
+++ b/test/legend-test.js
@@ -22,3 +22,30 @@ it("Plot.legend({}) throws an error", () => {
it("Plot.legend({color: {}}) throws an error", () => {
assert.throws(() => Plot.legend({color: {}}), /unknown legend type/);
});
+
+function tickLabels(legend) {
+ return Array.from(legend.querySelectorAll("text"), (text) => text.textContent);
+}
+
+it(`Plot.legend({color: {type: "quantile"}}) formats the threshold labels`, () => {
+ // Quantile scales are implicitly converted to threshold scales, and the
+ // thresholds inferred from the data are typically long fractions; the labels
+ // are formatted like axis ticks. https://github.com/observablehq/plot/issues/2345
+ const legend = Plot.legend({color: {type: "quantile", domain: [0, 1, 2, 3, 4, 5, 6], n: 7}});
+ assert.deepStrictEqual(tickLabels(legend), ["0.857", "1.714", "2.571", "3.429", "4.286", "5.143"]);
+});
+
+it(`Plot.legend({color: {type: "threshold"}}) formats the threshold labels`, () => {
+ const legend = Plot.legend({color: {type: "threshold", domain: [0.5, 1.25, 2000]}});
+ assert.deepStrictEqual(tickLabels(legend), ["0.5", "1.25", "2,000"]);
+});
+
+it(`Plot.legend({color: {type: "quantile", tickFormat}}) uses the given tick format`, () => {
+ const legend = Plot.legend({color: {type: "quantile", domain: [0, 1, 2, 3, 4, 5, 6], n: 7, tickFormat: ",.2f"}});
+ assert.deepStrictEqual(tickLabels(legend), ["0.86", "1.71", "2.57", "3.43", "4.29", "5.14"]);
+});
+
+it(`Plot.legend({color: {type: "threshold", tickFormat: null}}) suppresses the labels`, () => {
+ const legend = Plot.legend({color: {type: "threshold", domain: [0.5, 1.25, 2], tickFormat: null}});
+ assert.deepStrictEqual(tickLabels(legend), ["", "", ""]);
+});
diff --git a/test/output/penguinQuantileUnknown.html b/test/output/penguinQuantileUnknown.html
index 55cc221da4..1393a05250 100644
--- a/test/output/penguinQuantileUnknown.html
+++ b/test/output/penguinQuantileUnknown.html
@@ -22,19 +22,19 @@
- 3475
+ 3,475
- 3800
+ 3,800
- 4300
+ 4,300
- 4925
+ 4,925
body_mass_g