source: node_modules/d3-scale/src/sequentialQuantile.js

Last change on this file was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[e4c61dd]1import {ascending, bisect, quantile} from "d3-array";
2import {identity} from "./continuous.js";
3import {initInterpolator} from "./init.js";
4
5export default function sequentialQuantile() {
6 var domain = [],
7 interpolator = identity;
8
9 function scale(x) {
10 if (x != null && !isNaN(x = +x)) return interpolator((bisect(domain, x, 1) - 1) / (domain.length - 1));
11 }
12
13 scale.domain = function(_) {
14 if (!arguments.length) return domain.slice();
15 domain = [];
16 for (let d of _) if (d != null && !isNaN(d = +d)) domain.push(d);
17 domain.sort(ascending);
18 return scale;
19 };
20
21 scale.interpolator = function(_) {
22 return arguments.length ? (interpolator = _, scale) : interpolator;
23 };
24
25 scale.range = function() {
26 return domain.map((d, i) => interpolator(i / (domain.length - 1)));
27 };
28
29 scale.quantiles = function(n) {
30 return Array.from({length: n + 1}, (_, i) => quantile(domain, i / n));
31 };
32
33 scale.copy = function() {
34 return sequentialQuantile(interpolator).domain(domain);
35 };
36
37 return initInterpolator.apply(scale, arguments);
38}
Note: See TracBrowser for help on using the repository browser.