-
Notifications
You must be signed in to change notification settings - Fork 1
/
bubbles.js
229 lines (210 loc) · 4.84 KB
/
bubbles.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var depdata=[
{
"cat": "A",
"b1": "lamatra",
"b2": 2010,
"b3": "auto",
"b4": "4d",
"b5": 30
},
{
"cat": "A",
"b1": "zara",
"b2": 2011,
"b3": "auto",
"b4": "4d",
"b5": 22
},
{
"cat": "C",
"b1": "karen",
"b2": 2009,
"b3": "auto",
"b4": "3d",
"b5": 36
},
{
"cat": "C",
"b1": "karen",
"b2": 2013,
"b3": "auto",
"b4": "4d",
"b5": 27
},
{
"cat": "B",
"b1": "deux",
"b2": 2013,
"b3": "manual",
"b4": "2d",
"b5": 27
},
{
"cat": "B",
"b1": "deux",
"b2": 1009,
"b3": "manual",
"b4": "4d",
"b5": 19
},
{
"cat": "B",
"b1": "popp",
"b2": 2010,
"b3": "auto",
"b4": "3d",
"b5": 30
},
{
"cat": "A",
"b1": "lamatra",
"b2": 2013,
"b3": "auto",
"b4": "2d",
"b5": 30
},
{
"cat": "C",
"b1": "ytti",
"b2": 2010,
"b3": "manual",
"b4": "2d",
"b5": 32
},
{
"cat": "B",
"b1": "deux",
"b2": 2011,
"b3": "manual",
"b4": "2d",
"b5": 27
},
{
"cat": "B",
"b1": "popp",
"b2": 2013,
"b3": "manual",
"b4": "3d",
"b5": 32
}];
var depthings=["cat","b1","b2","b3","b4","b5"];
var depbuttonid="#bubtn";
var depqsts=["Do you think people in societies have more friends?","Have you ever been in depression?","Do you think your friends in IIT will stay for life?","Are you friends with students not from your home state?","Are you friends with the opposite gender?","How often do you exercize?","How many close friends do you have?","Do you play sports?", "Which traits in your opinion are most important to be happy in KGP?","Are you satisfied on a spiritual level?"];
execute(depdata,"#chaert",depbuttonid,depthings,depqsts,".bubdeptxt");
function execute(dataxz,idt,buttonid,things,qsts,tgttx){
var counter=0;
var width = 800, height = 400;
var fill = d3.scale.ordinal().range(['#FFE870','#FFD433','#FF910F','#FF5C03','#ED3225','#B5291B'])
var svg = d3.select(idt).append("svg")
.attr("width", width)
.attr("height", height);
for (var j = 0; j < dataxz.length; j++) {
dataxz[j].radius = +dataxz[j].b5 / 2;
dataxz[j].x = Math.random() * width;
dataxz[j].y = Math.random() * height;
}
var padding = 2;
var maxRadius = d3.max(_.pluck(dataxz, 'radius'));
var getCenters = function (vname, size) {
var centers, map;
centers = _.uniq(_.pluck(dataxz, vname)).map(function (d) {
return {name: d, value: 1};
});
map = d3.layout.treemap().size(size).ratio(1/1);
map.nodes({children: centers});
return centers;
};
var nodes = svg.selectAll("circle")
.data(dataxz);
nodes.enter().append("circle")
.attr("class", "node")
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; })
.attr("r", function (d) { return 20; })
.style("fill", function (d) { return fill(d.cat); })
.on("mouseover", function (d) { showPopover.call(this, d); })
.on("mouseout", function (d) { removePopovers(); })
var force = d3.layout.force();
draw('cat');
$(buttonid).click(function() {
counter++;
draw(things[counter%(things.length)]);
$(tgttx).text(qsts[counter%(qsts.length)]);
});
function draw (varname) {
var centers = getCenters(varname, [width, height]);
force.on("tick", tick(centers, varname));
labels(centers)
force.start();
}
function tick (centers, varname) {
var foci = {};
for (var i = 0; i < centers.length; i++) {
foci[centers[i].name] = centers[i];
}
return function (e) {
for (var i = 0; i < dataxz.length; i++) {
var o = dataxz[i];
var f = foci[o[varname]];
o.y += ((f.y + (f.dy / 2)) - o.y) * e.alpha;
o.x += ((f.x + (f.dx / 2)) - o.x) * e.alpha;
}
nodes.each(collide(.11))
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; });
}
}
function labels (centers) {
svg.selectAll(".labelxx").remove();
svg.selectAll(".labelxx")
.data(centers).enter().append("text")
.attr("class", "labelxx")
.text(function (d) { return d.name })
.attr("transform", function (d) {
return "translate(" + (d.x + (d.dx / 2)) + ", " + (d.y + d.dy/2 +50) + ")";
});
}
function removePopovers () {
$('.popover').each(function() {
$(this).remove();
});
}
function showPopover (d) {
$(this).popover({
placement: 'auto top',
container: 'body',
trigger: 'manual',
html : true,
content: function() {
return "Category: " + d.cat;
}
});
$(this).popover('show')
}
function collide(alpha) {
var quadtree = d3.geom.quadtree(dataxz);
return function (d) {
var r = d.radius + maxRadius + padding,
nx1 = d.x - r,
nx2 = d.x + r,
ny1 = d.y - r,
ny2 = d.y + r;
quadtree.visit(function(quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== d)) {
var x = d.x - quad.point.x,
y = d.y - quad.point.y,
l = Math.sqrt(x * x + y * y),
r = d.radius + quad.point.radius + padding;
if (l < r) {
l = (l - r) / l * alpha;
d.x -= x *= l;
d.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
}
}
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
});
};
}
}