-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathquantize.js
56 lines (46 loc) · 1.31 KB
/
quantize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {bisect} from "d3-array";
import {linearish} from "./linear.js";
import {initRange} from "./init.js";
export default function quantize() {
var x0 = 0,
x1 = 1,
n = 1,
domain = [0.5],
range = [0, 1],
unknown;
function scale(x) {
return x != null && x <= x ? range[bisect(domain, x, 0, n)] : unknown;
}
function rescale() {
var i = -1;
domain = new Array(n);
while (++i < n) domain[i] = ((i + 1) * x1 - (i - n) * x0) / (n + 1);
return scale;
}
scale.domain = function(_) {
return arguments.length ? ([x0, x1] = _, x0 = +x0, x1 = +x1, rescale()) : [x0, x1];
};
scale.range = function(_) {
return arguments.length ? (n = (range = Array.from(_)).length - 1, rescale()) : range.slice();
};
scale.invertExtent = function(y) {
var i = range.indexOf(y);
return i < 0 ? [NaN, NaN]
: i < 1 ? [x0, domain[0]]
: i >= n ? [domain[n - 1], x1]
: [domain[i - 1], domain[i]];
};
scale.unknown = function(_) {
return arguments.length ? (unknown = _, scale) : scale;
};
scale.thresholds = function() {
return domain.slice();
};
scale.copy = function() {
return quantize()
.domain([x0, x1])
.range(range)
.unknown(unknown);
};
return initRange.apply(linearish(scale), arguments);
}