Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/legends/ramp.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 27 additions & 0 deletions test/legend-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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), ["", "", ""]);
});
8 changes: 4 additions & 4 deletions test/output/penguinQuantileUnknown.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
<g transform="translate(0,28)" fill="none" text-anchor="middle" font-variant="tabular-nums">
<g class="tick" opacity="1" transform="translate(48.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">3475</text>
<text fill="currentColor" y="9" dy="0.71em">3,475</text>
</g>
<g class="tick" opacity="1" transform="translate(96.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">3800</text>
<text fill="currentColor" y="9" dy="0.71em">3,800</text>
</g>
<g class="tick" opacity="1" transform="translate(144.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">4300</text>
<text fill="currentColor" y="9" dy="0.71em">4,300</text>
</g>
<g class="tick" opacity="1" transform="translate(192.5,0)">
<line stroke="currentColor" y2="6" y1="-10"></line>
<text fill="currentColor" y="9" dy="0.71em">4925</text>
<text fill="currentColor" y="9" dy="0.71em">4,925</text>
</g>
</g>
<text x="0" y="12" fill="currentColor" font-weight="bold">body_mass_g</text>
Expand Down