Skip to content

Commit

Permalink
Fix #187 - add sequentialQuantile.quantiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Sep 2, 2019
1 parent e99772a commit bcc9711
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,10 @@ A [sequential scale](#sequential-scales) with a symmetric logarithmic transform,

A [sequential scale](#sequential-scales) using a *p*-quantile transform, analogous to a [quantile scale](#quantile-scales).

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

Returns an array of *n* + 1 quantiles. For example, if *n* = 4, returns an array of five numbers: the minimum value, the first quartile, the median, the third quartile, and the maximum.

### 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.
Expand Down
6 changes: 5 additions & 1 deletion src/sequentialQuantile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ascending, bisect} from "d3-array";
import {ascending, bisect, quantile} from "d3-array";
import {identity} from "./continuous.js";
import {initInterpolator} from "./init.js";

Expand Down Expand Up @@ -26,6 +26,10 @@ export default function sequentialQuantile() {
return domain.map((d, i) => interpolator(i / (domain.length - 1)));
};

scale.quantiles = function(n) {
return Array.from({length: n + 1}, (_, i) => quantile(domain, i / n));
};

scale.copy = function() {
return sequentialQuantile(interpolator).domain(domain);
};
Expand Down
6 changes: 6 additions & 0 deletions test/sequentialQuantile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ tape("sequentialQuantile().range() returns the computed range", function(test) {
test.deepEqual(s.range(), [0 / 4, 1 / 4, 2 / 4, 3 / 4, 4 / 4]);
test.end();
});

tape("sequentialQuantile().quantiles(n) computes n + 1 quantiles", function(test) {
var s = scale.scaleSequentialQuantile().domain(Array.from({length: 2000}, (_, i) => 2 * i / 1999));
test.deepEqual(s.quantiles(4), [0, 0.5, 1, 1.5, 2]);
test.end();
});

0 comments on commit bcc9711

Please sign in to comment.