source: node_modules/d3-scale/src/quantile.js@ e4c61dd

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

Prototype 1.1

  • Property mode set to 100644
File size: 1.4 KB
Line 
1import {ascending, bisect, quantileSorted as threshold} from "d3-array";
2import {initRange} from "./init.js";
3
4export default function quantile() {
5 var domain = [],
6 range = [],
7 thresholds = [],
8 unknown;
9
10 function rescale() {
11 var i = 0, n = Math.max(1, range.length);
12 thresholds = new Array(n - 1);
13 while (++i < n) thresholds[i - 1] = threshold(domain, i / n);
14 return scale;
15 }
16
17 function scale(x) {
18 return x == null || isNaN(x = +x) ? unknown : range[bisect(thresholds, x)];
19 }
20
21 scale.invertExtent = function(y) {
22 var i = range.indexOf(y);
23 return i < 0 ? [NaN, NaN] : [
24 i > 0 ? thresholds[i - 1] : domain[0],
25 i < thresholds.length ? thresholds[i] : domain[domain.length - 1]
26 ];
27 };
28
29 scale.domain = function(_) {
30 if (!arguments.length) return domain.slice();
31 domain = [];
32 for (let d of _) if (d != null && !isNaN(d = +d)) domain.push(d);
33 domain.sort(ascending);
34 return rescale();
35 };
36
37 scale.range = function(_) {
38 return arguments.length ? (range = Array.from(_), rescale()) : range.slice();
39 };
40
41 scale.unknown = function(_) {
42 return arguments.length ? (unknown = _, scale) : unknown;
43 };
44
45 scale.quantiles = function() {
46 return thresholds.slice();
47 };
48
49 scale.copy = function() {
50 return quantile()
51 .domain(domain)
52 .range(range)
53 .unknown(unknown);
54 };
55
56 return initRange.apply(scale, arguments);
57}
Note: See TracBrowser for help on using the repository browser.