Skip to content

Commit 5928263

Browse files
authored
fix descending domains on diverging scales (#1188)
1 parent 38cbe9a commit 5928263

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/scales/diverging.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
descending,
23
interpolateNumber,
34
interpolateRgb,
45
piecewise,
@@ -37,6 +38,7 @@ function ScaleD(
3738
) {
3839
pivot = +pivot;
3940
let [min, max] = domain;
41+
if (descending(min, max) < 0) ([min, max] = [max, min]), (reverse = !reverse);
4042
min = Math.min(min, pivot);
4143
max = Math.max(max, pivot);
4244

test/scales/scales-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,37 @@ it("plot(…).scale(name).unknown reflects the given unknown option for a diverg
361361
});
362362
});
363363

364+
it("plot(…).scale(name) handles a diverging scale with a descending domain", async () => {
365+
const plot = Plot.plot({
366+
color: {type: "diverging", domain: [100, -10]}
367+
});
368+
const {interpolate, ...color} = plot.scale("color");
369+
scaleEqual(color, {
370+
type: "diverging",
371+
symmetric: false,
372+
domain: [-100, 100],
373+
pivot: 0,
374+
clamp: false
375+
});
376+
for (const t of d3.ticks(0, 1, 100)) {
377+
assert.strictEqual(interpolate(t), d3.interpolateRdBu(1 - t));
378+
}
379+
});
380+
381+
it("plot(…).scale(name) handles a reversed diverging scale with a descending domain", async () => {
382+
const plot = Plot.plot({
383+
color: {type: "diverging", domain: [100, -10], reverse: true}
384+
});
385+
scaleEqual(plot.scale("color"), {
386+
type: "diverging",
387+
symmetric: false,
388+
domain: [-100, 100],
389+
pivot: 0,
390+
clamp: false,
391+
interpolate: d3.interpolateRdBu
392+
});
393+
});
394+
364395
it("plot(…).scale(name) promotes the given zero option to the domain", async () => {
365396
const penguins = await d3.csv("data/penguins.csv", d3.autoType);
366397
const plot = Plot.dotX(penguins, {x: "body_mass_g"}).plot({x: {zero: true}});

0 commit comments

Comments
 (0)