-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrdv.js
134 lines (109 loc) · 3.05 KB
/
rdv.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
(function(window) {
var rdv = {};
function Feature(opts) {
_.extend(this, opts);
_.bindAll(this, 'on', 'off');
}
function noop() {}
var WIDTH = rdv.WIDTH = {};
var TWOD = rdv.TWOD = {};
rdv.HEIGHT = {};
Feature.prototype = {
range: [0, Infinity],
vis: null,
on: noop,
off: noop,
applies: function(ppp) {
var range = this.range;
return range[0] <= ppp && ppp < range[1];
},
apply: function(ppp) {
var feature = this;
var applies = feature.applies(ppp);
return applies ? feature.on : feature.off;
}
};
rdv.Feature = Feature;
rdv.Vis = function(dimensionality, minPPP) {
dimensionality = dimensionality || TWOD;
minPPP = minPPP || 0;
var selection;
var data = [];
var features = [];
var margin = margin || { top: 0, bottom: 0, right: 0, left: 0 };
var w;
var h;
var dispatch = d3.dispatch('data', 'resize');
function vis(sel) {
selection = sel;
}
function getPPP() {
var ew = w - margin.right - margin.left;
var eh = h - margin.top - margin.bottom;
var pixels = dimensionality === TWOD ? ew * eh :
dimensionality === WIDTH ? ew : eh;
return data.length / pixels;
}
vis.ppp = getPPP;
vis.render = function() {
var ppp = getPPP();
features.forEach(function(feature) {
selection.call(feature.apply(ppp));
});
return vis;
};
vis.resize = function() {
var rect = selection.node().getBoundingClientRect();
w = rect.width;
h = rect.height;
dispatch.resize(w, h);
return vis;
};
vis.data = function(d) {
if (d) {
data = d;
dispatch.data(data);
return vis;
}
return data;
};
vis.features = function(d) {
if (d) {
features = d;
// add vis ref to features
features.forEach(function(feature) {
feature.vis = vis;
});
return vis;
}
return features;
};
vis.w = function(d) {
if (d !== undefined) {
w = d;
return vis;
}
return w;
};
vis.h = function(d) {
if (d !== undefined) {
h = d;
return vis;
}
return h;
};
vis.margin = function(d) {
if (d !== undefined) {
margin = d;
return vis;
}
return margin;
};
vis.on = function() {
dispatch.on.apply(dispatch, arguments);
return vis;
};
return vis;
};
window.rdv = rdv;
}(this));