Skip to content

Commit

Permalink
Fix #188 - {sequential,diverging}.range.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Sep 2, 2019
1 parent bcc9711 commit 60858c6
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 14 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ Equivalent to [scaleTime](#scaleTime), but the returned time scale operates in [

### Sequential Scales

Sequential scales, like [diverging scales](#diverging-scales), are similar to [continuous scales](#continuous-scales) in that they map a continuous, numeric input domain to a continuous output range. However, unlike continuous scales, the output range of a sequential scale is fixed by its interpolator and not configurable. These scales do not expose [invert](#continuous_invert), [range](#continuous_range), [rangeRound](#continuous_rangeRound) and [interpolate](#continuous_interpolate) methods.
Sequential scales, like [diverging scales](#diverging-scales), are similar to [continuous scales](#continuous-scales) in that they map a continuous, numeric input domain to a continuous output range. However, unlike continuous scales, the input domain and output range of a sequential scale always has exactly two elements, and the output range is typically specified as an interpolator rather than an array of values. These scales do not expose [invert](#continuous_invert) and [interpolate](#continuous_interpolate) methods.

<a name="scaleSequential" href="#scaleSequential">#</a> d3.<b>scaleSequential</b>([[<i>domain</i>, ]<i>interpolator</i>]) · [Source](https://github.com/d3/d3-scale/blob/master/src/sequential.js), [Examples](https://observablehq.com/@d3/sequential-scales)

Expand Down Expand Up @@ -573,9 +573,13 @@ See [*continuous*.clamp](#continuous_clamp).

If *interpolator* is specified, sets the scale’s interpolator to the specified function. If *interpolator* is not specified, returns the scale’s current interpolator.

<a name="sequential_range" href="#sequential_range">#</a> <i>sequential</i>.<b>range</b>() · [Source](https://github.com/d3/d3-scale/blob/master/src/sequential.js), [Examples](https://observablehq.com/@d3/sequential-scales)
<a name="sequential_range" href="#sequential_range">#</a> <i>sequential</i>.<b>range</b>([<i>range</i>]) · [Source](https://github.com/d3/d3-scale/blob/master/src/sequential.js), [Examples](https://observablehq.com/@d3/sequential-scales)

See [*continuous*.range](#continuous_range). Note that a sequential scale’s range is read-only, as it is determined by its [interpolator](#sequential_interpolator).
See [*continuous*.range](#continuous_range).

<a name="sequential_rangeRound" href="#sequential_rangeRound">#</a> <i>sequential</i>.<b>rangeRound</b>([<i>range</i>]) · [Source](https://github.com/d3/d3-scale/blob/master/src/sequential.js), [Examples](https://observablehq.com/@d3/sequential-scales)

See [*continuous*.rangeRound](#continuous_rangeRound).

<a name="sequential_copy" href="#sequential_copy">#</a> <i>sequential</i>.<b>copy</b>() · [Source](https://github.com/d3/d3-scale/blob/master/src/sequential.js), [Examples](https://observablehq.com/@d3/sequential-scales)

Expand Down Expand Up @@ -607,7 +611,7 @@ Returns an array of *n* + 1 quantiles. For example, if *n* = 4, returns an array

### Diverging Scales

Diverging scales, like [sequential scales](#sequential-scales), are similar to [continuous scales](#continuous-scales) in that they map a continuous, numeric input domain to a continuous output range. However, unlike continuous scales, the output range of a diverging scale is fixed by its interpolator and not configurable. These scales do not expose [invert](#continuous_invert), [range](#continuous_range), [rangeRound](#continuous_rangeRound) and [interpolate](#continuous_interpolate) methods.
Diverging scales, like [sequential scales](#sequential-scales), are similar to [continuous scales](#continuous-scales) in that they map a continuous, numeric input domain to a continuous output range. However, unlike continuous scales, the input domain and output range of a diverging scale always has exactly three elements, and the output range is typically specified as an interpolator rather than an array of values. These scales do not expose [invert](#continuous_invert) and [interpolate](#continuous_interpolate) methods.

<a name="scaleDiverging" href="#scaleDiverging">#</a> d3.<b>scaleDiverging</b>([[<i>domain</i>, ]<i>interpolator</i>]) · [Source](https://github.com/d3/d3-scale/blob/master/src/diverging.js), [Examples](https://observablehq.com/@d3/diverging-scales)

Expand All @@ -633,9 +637,13 @@ See [*continuous*.clamp](#continuous_clamp).

If *interpolator* is specified, sets the scale’s interpolator to the specified function. If *interpolator* is not specified, returns the scale’s current interpolator.

<a name="diverging_range" href="#diverging_range">#</a> <i>diverging</i>.<b>range</b>() · [Source](https://github.com/d3/d3-scale/blob/master/src/diverging.js), [Examples](https://observablehq.com/@d3/diverging-scales)
<a name="diverging_range" href="#diverging_range">#</a> <i>diverging</i>.<b>range</b>([<i>range</i>]) · [Source](https://github.com/d3/d3-scale/blob/master/src/diverging.js), [Examples](https://observablehq.com/@d3/diverging-scales)

See [*continuous*.range](#continuous_range).

<a name="diverging_rangeRound" href="#diverging_rangeRound">#</a> <i>diverging</i>.<b>rangeRound</b>([<i>range</i>]) · [Source](https://github.com/d3/d3-scale/blob/master/src/diverging.js), [Examples](https://observablehq.com/@d3/diverging-scales)

See [*continuous*.range](#continuous_range). Note that a diverging scale’s range is read-only, as it is determined by its [interpolator](#diverging_interpolator).
See [*continuous*.range](#continuous_rangeRound).

<a name="diverging_copy" href="#diverging_copy">#</a> <i>diverging</i>.<b>copy</b>() · [Source](https://github.com/d3/d3-scale/blob/master/src/diverging.js), [Examples](https://observablehq.com/@d3/diverging-scales)

Expand Down
14 changes: 11 additions & 3 deletions src/diverging.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {interpolate, interpolateRound, piecewise} from "d3-interpolate";
import {identity} from "./continuous.js";
import {initInterpolator} from "./init.js";
import {linearish} from "./linear.js";
Expand Down Expand Up @@ -37,9 +38,16 @@ function transformer() {
return arguments.length ? (interpolator = _, scale) : interpolator;
};

scale.range = function() {
return [interpolator(0), interpolator(0.5), interpolator(1)];
};
function range(interpolate) {
return function(_) {
var r0, r1, r2;
return arguments.length ? ([r0, r1, r2] = _, interpolator = piecewise(interpolate, [r0, r1, r2]), scale) : [interpolator(0), interpolator(0.5), interpolator(1)];
};
}

scale.range = range(interpolate);

scale.rangeRound = range(interpolateRound);

scale.unknown = function(_) {
return arguments.length ? (unknown = _, scale) : unknown;
Expand Down
13 changes: 11 additions & 2 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ export function initRange(domain, range) {
export function initInterpolator(domain, interpolator) {
switch (arguments.length) {
case 0: break;
case 1: this.interpolator(domain); break;
default: this.interpolator(interpolator).domain(domain); break;
case 1: {
if (typeof domain === "function") this.interpolator(domain);
else this.range(domain);
break;
}
default: {
this.domain(domain);
if (typeof interpolator === "function") this.interpolator(interpolator);
else this.range(interpolator);
break;
}
}
return this;
}
14 changes: 11 additions & 3 deletions src/sequential.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {interpolate, interpolateRound} from "d3-interpolate";
import {identity} from "./continuous.js";
import {initInterpolator} from "./init.js";
import {linearish} from "./linear.js";
Expand Down Expand Up @@ -32,9 +33,16 @@ function transformer() {
return arguments.length ? (interpolator = _, scale) : interpolator;
};

scale.range = function() {
return [interpolator(0), interpolator(1)];
};
function range(interpolate) {
return function(_) {
var r0, r1;
return arguments.length ? ([r0, r1] = _, interpolator = interpolate(r0, r1), scale) : [interpolator(0), interpolator(1)];
};
}

scale.range = range(interpolate);

scale.rangeRound = range(interpolateRound);

scale.unknown = function(_) {
return arguments.length ? (unknown = _, scale) : unknown;
Expand Down
21 changes: 21 additions & 0 deletions test/diverging-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,24 @@ tape("diverging.interpolator(interpolator) sets the interpolator", function(test
test.equal(s( 0.5), 1.0);
test.end();
});

tape("diverging.range(range) sets the interpolator", function(test) {
var s = scale.scaleDiverging().range([1, 3, 10]);
test.equal(s.interpolator()(0.5), 3);
test.deepEqual(s.range(), [1, 3, 10]);
test.end();
});

tape("diverging.range(range) ignores additional values", function(test) {
var s = scale.scaleDiverging().range([1, 3, 10, 20]);
test.equal(s.interpolator()(0.5), 3);
test.deepEqual(s.range(), [1, 3, 10]);
test.end();
});

tape("scaleDiverging(range) sets the interpolator", function(test) {
var s = scale.scaleDiverging([1, 3, 10]);
test.equal(s.interpolator()(0.5), 3);
test.deepEqual(s.range(), [1, 3, 10]);
test.end();
});
21 changes: 21 additions & 0 deletions test/sequential-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,24 @@ tape("sequential.range() returns the computed range", function(test) {
test.deepEqual(s.range(), [1, 3]);
test.end();
});

tape("sequential.range(range) sets the interpolator", function(test) {
var s = scale.scaleSequential().range([1, 3]);
test.equal(s.interpolator()(0.5), 2);
test.deepEqual(s.range(), [1, 3]);
test.end();
});

tape("sequential.range(range) ignores additional values", function(test) {
var s = scale.scaleSequential().range([1, 3, 10]);
test.equal(s.interpolator()(0.5), 2);
test.deepEqual(s.range(), [1, 3]);
test.end();
});

tape("scaleSequential(range) sets the interpolator", function(test) {
var s = scale.scaleSequential([1, 3]);
test.equal(s.interpolator()(0.5), 2);
test.deepEqual(s.range(), [1, 3]);
test.end();
});

0 comments on commit 60858c6

Please sign in to comment.