-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectbin.js
86 lines (73 loc) · 2.13 KB
/
rectbin.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(function() {
d3.rectbin = function() {
var dx = 0.1,
dy = 0.1,
x = rectbinX,
y = rectbinY;
function rectbin(points, xExtent = null, yExtent = null) {
var binsById = {};
// console.log('Extent?', xExtent, yExtent)
if (!xExtent) xExtent = d3.extent(points, function(d, i){ return x.call(rectbin, d, i) ;});
if (!yExtent) yExtent = d3.extent(points, function(d, i){ return y.call(rectbin, d, i) ;});
// debugger
// console.log(`d3.range(${yExtent[0]}, ${yExtent[1]}, ${dy}): ${d3.range(yExtent[0], yExtent[1], dy)}`)
d3.range(yExtent[0], yExtent[1] + dy, dx).forEach(function(Y){
d3.range(xExtent[0], xExtent[1] + dx, dx).forEach(function(X){
// console.log('XY', X, Y)
var py = Y / dy;
var pj = trunc(py);
var px = X / dx;
var pi = trunc(px);
// debugger
var id = pi + '-' + pj;
var bin = binsById[id] = [];
bin.i = pi;
bin.j = pj;
bin.x = pi * dx;
bin.y = pj * dy;
});
});
points.forEach(function(point, i) {
var py = y.call(rectbin, point, i) / dy;
var pj = trunc(py);
var px = x.call(rectbin, point, i) / dx;
var pi = trunc(px);
var id = pi + '-' + pj;
var bin = binsById[id];
try {
bin.push(point);
} catch(e) {
console.log(e)
// debugger
}
});
return d3.values(binsById);
}
rectbin.x = function(_) {
if (!arguments.length) return x;
x = _;
return rectbin;
};
rectbin.y = function(_) {
if (!arguments.length) return y;
y = _;
return rectbin;
};
rectbin.dx = function(_) {
if (!arguments.length) return dx;
dx = _;
return rectbin;
};
rectbin.dy = function(_) {
if (!arguments.length) return dy;
dy = _;
return rectbin;
};
return rectbin;
};
var rectbinX = function(d) { return d[0]; },
rectbinY = function(d) { return d[1]; };
})();
function trunc(x) {
return x < 0 ? Math.ceil(x) : Math.floor(x);
}