This repository has been archived by the owner on Apr 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3.hist2d.js
119 lines (101 loc) · 2.66 KB
/
d3.hist2d.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
(function() {
d3.hist2d = function() {
var b = 0; // bins
var d; // domain
var s; // size [width, height]
var xi = 0; // array index for x
var yi = 1; // array index for y
var x; // row scale
var y; // column scale
var t = 12; // interval in ms to compute data between animation frames
function hist2d(data, callback) {
x = _quantize(d[0]);
y = _quantize(d[1]);
var hist = new Array(b * b);
i = hist.length;
while (i--) {
hist[i] = [];
}
var id = 0;
var hx = 0;
var hy = 0;
var now = 0;
var then = 0;
var i = data.length;
var iterate = function() {
if (i > 0) {
now = then = Date.now();
while (then - now < t && i-- > 0) {
hx = x(data[i][xi]);
hy = y(data[i][yi]);
id = hx * b + hy;
hist[id][hist[id].length] = data[i];
if (!hist[id].k) {
hist[id].x = hx;
hist[id].y = hy;
hist[id].k = true;
}
then = Date.now();
}
requestAnimationFrame(iterate);
} else {
hist = hist.filter(function(d) {
return d.k;
});
requestAnimationFrame(terminate);
}
};
requestAnimationFrame(iterate);
var terminate = function() {
callback(hist);
};
return hist2d;
}
// hist2d.bins(number) => b
hist2d.bins = function(_) {
if (!arguments.length) { return b; }
b = _;
return hist2d;
};
// hist2d.indices([x, y]) => [x, y]
hist2d.indices = function(_) {
if (!arguments.length) { return [xi, yi]; }
xi = _[0];
yi = _[1];
return hist2d;
};
// hist2d.interval(number) => t
hist2d.interval = function(_) {
if (!arguments.length) { return t; }
t = _;
return hist2d;
};
// hist2d.domain([[x],[y]]) => d
hist2d.domain = function(_) {
if (!arguments.length) { return d; }
d = _;
return hist2d;
};
// hist2d.size([width, height]) => s
hist2d.size = function(_) {
if (!arguments.length) { return s; }
s = [_[0] / b, _[1] / b];
return hist2d;
};
/**
* Quantize Credits:
* Mike Bostock (@mbostock)
* https://github.com/d3/d3-scale/blob/master/src/quantize.js
*/
var _quantize = function(domain) {
var x0 = +domain[0];
var x1 = +domain[domain.length - 1];
var kx = b / (x1 - x0);
var i = b - 1;
return function(x) {
return Math.max(0, Math.min(i, Math.floor(kx * (x - x0))));
};
};
return hist2d;
};
})();