Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix descending domains on diverging scales #1188

Merged
merged 1 commit into from
Dec 16, 2022
Merged
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
2 changes: 2 additions & 0 deletions src/scales/diverging.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
descending,
interpolateNumber,
interpolateRgb,
piecewise,
Expand Down Expand Up @@ -37,6 +38,7 @@ function ScaleD(
) {
pivot = +pivot;
let [min, max] = domain;
if (descending(min, max) < 0) ([min, max] = [max, min]), (reverse = !reverse);
min = Math.min(min, pivot);
max = Math.max(max, pivot);

Expand Down
31 changes: 31 additions & 0 deletions test/scales/scales-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,37 @@ it("plot(…).scale(name).unknown reflects the given unknown option for a diverg
});
});

it("plot(…).scale(name) handles a diverging scale with a descending domain", async () => {
const plot = Plot.plot({
color: {type: "diverging", domain: [100, -10]}
});
const {interpolate, ...color} = plot.scale("color");
scaleEqual(color, {
type: "diverging",
symmetric: false,
domain: [-100, 100],
pivot: 0,
clamp: false
});
for (const t of d3.ticks(0, 1, 100)) {
assert.strictEqual(interpolate(t), d3.interpolateRdBu(1 - t));
}
});

it("plot(…).scale(name) handles a reversed diverging scale with a descending domain", async () => {
const plot = Plot.plot({
color: {type: "diverging", domain: [100, -10], reverse: true}
});
scaleEqual(plot.scale("color"), {
type: "diverging",
symmetric: false,
domain: [-100, 100],
pivot: 0,
clamp: false,
interpolate: d3.interpolateRdBu
});
});

it("plot(…).scale(name) promotes the given zero option to the domain", async () => {
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
const plot = Plot.dotX(penguins, {x: "body_mass_g"}).plot({x: {zero: true}});
Expand Down