Skip to content

Commit ac6bff0

Browse files
mbostockFil
andauthored
expose range for continuous color scales (#560)
Co-authored-by: Philippe Rivière <[email protected]>
1 parent d9f28a3 commit ac6bff0

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/scales/quantitative.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {constant} from "../mark.js";
2828
import {order} from "../scales.js";
2929

3030
export const flip = i => t => i(1 - t);
31+
const unit = [0, 1];
3132

3233
const interpolators = new Map([
3334
// numbers
@@ -52,7 +53,7 @@ export function ScaleQ(key, scale, channels, {
5253
zero,
5354
domain = (registry.get(key) === radius || registry.get(key) === opacity ? inferZeroDomain : inferDomain)(channels),
5455
round,
55-
range = registry.get(key) === radius ? inferRadialRange(channels, domain) : registry.get(key) === opacity ? [0, 1] : undefined,
56+
range = registry.get(key) === radius ? inferRadialRange(channels, domain) : registry.get(key) === opacity ? unit : undefined,
5657
type,
5758
scheme = type === "cyclical" ? "rainbow" : "turbo",
5859
interpolate = registry.get(key) === color ? (range !== undefined ? interpolateRgb : quantitativeScheme(scheme)) : round ? interpolateRound : interpolateNumber,
@@ -77,12 +78,11 @@ export function ScaleQ(key, scale, channels, {
7778
interpolate = flip(interpolate);
7879
reverse = false;
7980
}
80-
if (domain.length > 2) {
81-
if (range === undefined) range = Float64Array.from(domain, (_, i) => i / (domain.length - 1));
82-
scale.interpolate(interpolatePiecewise(interpolate));
83-
} else {
84-
scale.interpolate(constant(interpolate));
81+
if (range === undefined) {
82+
range = Float64Array.from(domain, (_, i) => i / (domain.length - 1));
83+
if (range.length === 2) range = unit; // optimize common case of [0, 1]
8584
}
85+
scale.interpolate((range === unit ? constant : interpolatePiecewise)(interpolate));
8686
} else {
8787
scale.interpolate(interpolate);
8888
}

test/scales/scales-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ it("plot(…).scale('color') can return a linear scale", () => {
296296
assert.deepStrictEqual(plot.scale("color"), {
297297
type: "linear",
298298
domain: [1, 5],
299+
range: [0, 1],
299300
interpolate: d3.interpolateTurbo,
300301
clamp: false
301302
});
@@ -318,6 +319,7 @@ it("plot(…).scale('color') can return a utc scale", async () => {
318319
assert.deepStrictEqual(plot.scale("color"), {
319320
type: "utc",
320321
domain: [new Date("2013-05-13"), new Date("2018-05-11")],
322+
range: [0, 1],
321323
interpolate: d3.interpolateTurbo,
322324
clamp: false
323325
});
@@ -696,6 +698,7 @@ it("plot(…).scale('color') promotes a cyclical scale to a linear scale", () =>
696698
assert.deepStrictEqual(plot.scale("color"), {
697699
type: "linear",
698700
domain: [1, 5],
701+
range: [0, 1],
699702
interpolate: d3.interpolateRainbow,
700703
clamp: false
701704
});
@@ -706,6 +709,7 @@ it("plot(…).scale('color') ignores nonsensical options for cyclical scale", ()
706709
assert.deepStrictEqual(plot.scale("color"), {
707710
type: "linear",
708711
domain: [1, 5],
712+
range: [0, 1],
709713
interpolate: d3.interpolateRainbow,
710714
clamp: false
711715
});
@@ -716,6 +720,7 @@ it("plot(…).scale('color') promotes a cyclical scale to a linear scale, even w
716720
assert.deepStrictEqual(plot.scale("color"), {
717721
type: "linear",
718722
domain: [1, 5],
723+
range: [0, 1],
719724
interpolate: d3.interpolateBlues,
720725
clamp: false
721726
});
@@ -726,6 +731,7 @@ it("plot(…).scale('color') promotes a cyclical scale to a linear scale and ign
726731
assert.deepStrictEqual(plot.scale("color"), {
727732
type: "linear",
728733
domain: [1, 5],
734+
range: [0, 1],
729735
interpolate: d3.interpolateWarm,
730736
clamp: false
731737
});
@@ -736,6 +742,7 @@ it("plot(…).scale('color') promotes a sequential scale to a linear scale, even
736742
assert.deepStrictEqual(plot.scale("color"), {
737743
type: "linear",
738744
domain: [1, 5],
745+
range: [0, 1],
739746
interpolate: d3.interpolateBlues,
740747
clamp: false
741748
});
@@ -746,6 +753,7 @@ it("plot(…).scale('color') promotes a sequential scale to a linear scale and i
746753
assert.deepStrictEqual(plot.scale("color"), {
747754
type: "linear",
748755
domain: [1, 5],
756+
range: [0, 1],
749757
interpolate: d3.interpolateWarm,
750758
clamp: false
751759
});
@@ -756,6 +764,7 @@ it("plot(…).scale('color') promotes a sequential scale to a linear scale", ()
756764
assert.deepStrictEqual(plot.scale("color"), {
757765
type: "linear",
758766
domain: [1, 5],
767+
range: [0, 1],
759768
interpolate: d3.interpolateTurbo,
760769
clamp: false
761770
});
@@ -767,6 +776,7 @@ it("plot(…).scale('color') promotes a reversed sequential scale to a linear sc
767776
assert.deepStrictEqual(color, {
768777
type: "linear",
769778
domain: [1, 5],
779+
range: [0, 1],
770780
clamp: false
771781
});
772782
for (const t of d3.ticks(1, 5, 100)) {

0 commit comments

Comments
 (0)