-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsources-index.html
More file actions
354 lines (320 loc) · 10.9 KB
/
Copy pathsources-index.html
File metadata and controls
354 lines (320 loc) · 10.9 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<!DOCTYPE html>
<meta charset="utf-8">
<svg></svg>
<div id="chart"></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// sample data, this will be factored out
var nodes = [
{ id: "Random", kind: "TALK", name: "asdfg", value: 1},
{ id: "Random123", kind: "TALK", name: "qwert", value: 1},
{ id: "Random234", kind: "BOOK", name: "zxcvb", value: 2},
{ id: "Random345", kind: "BOOK", name: "tyuio", value: 3},
{ id: "Random456", kind: "BOOK", name: "lkjhg", value: 1},
{ id: "Random567", kind: "PAPER", name: "ghjkl", value: 2},
{ id: "Random678", kind: "BLOG_POST", name: "mnbvc", value: 1},
{ id: "Random789", kind: "PODCAST", name: "xftyu", value: 2},
{ id: "Random890", kind: "DOCUMENTARY", name: "bhujm", value: 1},
{ id: "Random00", kind: "TALK", name: "asdfg", value: 2},
{ id: "Random1023", kind: "TALK", name: "qwert", value: 3},
{ id: "Random2304", kind: "BOOK", name: "zxcvb", value: 1},
{ id: "Random0345", kind: "BOOK", name: "tyuio", value: 2},
{ id: "Random4506", kind: "BOOK", name: "lkjhg", value: 3},
{ id: "Random5067", kind: "PAPER", name: "ghjkl", value: 2},
{ id: "Random0678", kind: "BLOG_POST", name: "mnbvc", value: 1},
{ id: "Random7890", kind: "PODCAST", name: "xftyu", value: 1},
{ id: "Random8090", kind: "DOCUMENTARY", name: "bhujm", value: 3},
{ id: "Random0", kind: "TALK", name: "asdfg", value: 2},
{ id: "Random1", kind: "TALK", name: "qwert", value: 1},
{ id: "Random2", kind: "BOOK", name: "zxcvb", value: 2},
{ id: "Random3", kind: "BOOK", name: "tyuio", value: 1},
{ id: "Random4", kind: "BOOK", name: "lkjhg", value: 1},
{ id: "Random5", kind: "PAPER", name: "ghjkl", value: 1},
{ id: "Random6", kind: "BLOG_POST", name: "mnbvc", value: 2},
{ id: "Random7", kind: "PODCAST", name: "xftyu", value: 1},
{ id: "Random8", kind: "DOCUMENTARY", name: "bhujm", value: 2},
{ id: "Random888", kind: "BLOG_POST", name: "mnbvc", value: 3},
{ id: "Random333", kind: "BLOG_POST", name: "mnbvc", value: 2},
{ id: "Random789", kind: "PODCAST", name: "xftyu", value: 2},
{ id: "Random890", kind: "DOCUMENTARY", name: "bhujm", value: 1},
{ id: "Random00", kind: "TALK", name: "asdfg", value: 2},
{ id: "Ran1023", kind: "TALK", name: "qwert", value: 1},
{ id: "Ran5067", kind: "PAPER", name: "ghjkl", value: 1},
{ id: "Ran0678", kind: "BLOG_POST", name: "mnbvc", value: 1},
{ id: "Ran7890", kind: "PODCAST", name: "xftyu", value: 1},
{ id: "Ran8090", kind: "DOCUMENTARY", name: "bhujm", value: 1},
{ id: "Ran0", kind: "TALK", name: "asdfg", value: 1},
{ id: "Ran1", kind: "TALK", name: "qwert", value: 1},
{ id: "Ran2", kind: "BOOK", name: "zxcvb", value: 1},
{ id: "Ran3", kind: "BOOK", name: "tyuio", value: 1},
{ id: "Ran4", kind: "BOOK", name: "lkjhg", value: 1},
{ id: "Ran5", kind: "OTHER", name: "ghjkl", value: 1},
{ id: "Ran6", kind: "BLOG_POST", name: "mnbvc", value: 1},
{ id: "Ran7", kind: "PODCAST", name: "xftyu", value: 1},
{ id: "Ran8", kind: "DOCUMENTARY", name: "bhujm", value: 1},
{ id: "Ran888", kind: "BLOG_POST", name: "mnbvc", value: 1},
]
// creating the container for the bubble graph
var width = window.innerWidth
var height = 600
var container = d3.select('svg')
.style("width", width)
.style("height", height)
.append("g")
.attr("class", "nodes")
// creates the bubbles and sets their styles and functionality
var nodeElements = container.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr('r', getRadius)
.attr('fill', getColor)
.on('mouseover', mouseOver)
.on('mousemove', mouseMove)
.on('mouseout', mouseOut)
// creates the tooltip when a bubble is hovered
var tooltip = d3.select("#chart").append("div")
.style("position", "absolute")
.style("visibility", "hidden")
.style("display", "block")
.style("color", "white")
.style("padding", "8px")
.style("background-color", "#626D71")
.style("border-radius", "6px")
.style("text-align", "center")
.style("font-family", "monospace")
.style("width", "400px")
.html("")
// starting positions for the nodes
nodes.forEach(function(node) {
node.x = splitNum(node);
node.y = height;
})
function mouseOver(node) {
node.isHovering = true
tooltip.text(node.id)
hoverColors();
simulation.alphaTarget(.3)
.force('collide', forceCollide)
.restart()
return tooltip.style("visibility", "visible")
}
function mouseMove() {
return tooltip.style("top", (d3.event.pageY - 10) + "px")
.style("left", (d3.event.pageX + 10) + "px")
}
function mouseOut(node) {
node.isHovering = false
resetColors();
simulation.alphaTarget(.3)
.force('collide', forceCollide)
.restart()
return tooltip.style("visibility", "hidden")
}
// returns the color of the node based on its kind
function getColor(node) {
kind = node.kind;
if (kind === 'BLOG_POST') {
return '#ffb68b';
} else if (kind === 'LETTER') {
return '#ff83d4';
} else if (kind === 'PAPER') {
return '#ffd88e';
} else if (kind === 'PODCAST') {
return '#ff8b8b';
} else if (kind === 'DOCUMENTARY') {
return '#df8bff';
} else if (kind ==='TALK') {
return '#ff83d4';
} else if (kind === 'OTHER') {
return '#bfc7d9';
} else {
return '#9176fb';
}
}
function getRadius(node) {
return node.value * 10
}
function hoverColors() {
nodeElements.style("fill-opacity", function(o) {
return o.isHovering ? 1 : 0.2;
});
};
function resetColors(d) {
d3.selectAll("circle").style("fill-opacity", 1);
};
//toggle switch largely taken from https://bl.ocks.org/ProQuestionAsker/79d0228ae7161e349770e7d553cf4c94
var atRight = true
var rect = d3.select("svg").append("rect")
.attr("x", 10)
.attr("y", 10)
.attr("rx", 22)
.attr("ry", 22)
.style("fill", "lightgray")
.attr("width", 64)
.attr("height", 40)
.on("click", function(){
if(atRight === true){
setAtRight(!atRight)
simulation.restart()
.force("x", forceX)
.force("y", forceY)
.force("collide", forceCollide)
.alphaTarget(0.2)
} else {
setAtRight(!atRight)
simulation.restart()
.force("x", forceX)
.force("y", forceY)
.force("collide", forceCollide)
.alphaTarget(0.2)
}
})
var circle = d3.select("svg").append("circle")
.attr("cx", 30)
.attr("cy", 30)
.attr("r", 16)
.attr("value", 1.6)
.style("fill", "white")
.on("click", function(){
if(atRight === true){
setAtRight(!atRight)
simulation.restart()
.force("x", forceX)
.force("y", forceY)
.force("collide", forceCollide)
.alphaTarget(0.2)
} else {
setAtRight(!atRight)
simulation.restart()
.force("x", forceX)
.force("y", forceY)
.force("collide", forceCollide)
.alphaTarget(0.2)
}
})
var setAtRight = function(newValue) {
atRight = newValue;
circle.transition().duration(250)
.attr("cx", (atRight? (30) : (54)))
.style("fill", "white");
rect.transition().duration(250)
.style("fill", atRight? "lightgray" : "#C06C84");
}
var res = {
'getValue': function() { return atRight; },
'setValue': setAtRight,
'remove': function() { circle.remove(); }
}
//toggle switch largely taken from https://bl.ocks.org/ProQuestionAsker/79d0228ae7161e349770e7d553cf4c94
var atRight2 = true
var rect2 = d3.select("svg").append("rect")
.attr("x", 10)
.attr("y", 60)
.attr("rx", 22)
.attr("ry", 62)
.style("fill", "lightgray")
.attr("width", 64)
.attr("height", 40)
.on("click", function(){
if(atRight2 === true){
setAtRight2(!atRight2)
d3.select(".nodes").selectAll("circle").each(function(d) {
d3.select(this).attr("r", 20);
})
simulation.restart()
.force("x", forceX)
.force("y", forceY)
.force('collide', forceCollide)
.alphaTarget(0.2)
} else {
setAtRight2(!atRight2)
d3.select(".nodes").selectAll("circle").each(function(node) {
d3.select(this).attr("r", getRadius);
})
simulation.alphaTarget(.2)
.force('collide', forceCollide)
.restart();
}
})
var circle2 = d3.select("svg").append("circle")
.attr("cx", 30)
.attr("cy", 80)
.attr("r", 16)
.attr("value", 1.6)
.style("fill", "white")
.on("click", function(){
if(atRight2 === true){
setAtRight2(!atRight2)
d3.select(".nodes").selectAll("circle").each(function(d) {
d3.select(this).attr("r", 20);
})
simulation.restart()
.force("x", forceX)
.force("y", forceY)
.force('collide', forceCollide)
.alphaTarget(0.2)
} else {
setAtRight2(!atRight2)
d3.select(".nodes").selectAll("circle").each(function(node) {
d3.select(this).attr("r", getRadius);
})
simulation.restart()
.force("x", forceX)
.force("y", forceY)
.force('collide', forceCollide)
.alphaTarget(0.2)
}
})
var setAtRight2 = function(newValue) {
atRight2 = newValue;
circle2.transition().duration(250)
.attr("cx", (atRight2? (30) : (54)))
.style("fill", "white");
rect2.transition().duration(250)
.style("fill", atRight2? "lightgray" : "#C06C84");
}
var res2 = {
'getValue': function() { return atRight2; },
'setValue': setAtRight2,
'remove': function() { circle.remove(); }
}
// forces
var forceX = d3.forceX(function(d) { return atRight ? (width/2) : (width * splitNum(d)) } ).strength(0.1)
var forceY = d3.forceY(height/2).strength(atRight ? 0.1 : 0.4 )
var forceCollide = d3.forceCollide().radius(function(d) { return atRight2 ? (d.value * 10 + 5) : 25 }).iterations(2)
// positioning of the groups when split
function splitNum(d) {
if (d.kind === 'LETTER') {
return 0.6;
} else if (d.kind === 'BLOG_POST') {
return 0.5;
} else if (d.kind === 'PAPER') {
return 0.4;
} else if (d.kind === 'PODCAST') {
return 0.3;
} else if (d.kind === 'DOCUMENTARY') {
return 0.2;
} else if (d.kind ==='TALK') {
return 0.1;
} else if (d.kind === 'OTHER') {
return 0;
} else {
return -0.1;
}
}
// creates the forces between elements
var simulation = d3.forceSimulation(nodes)
.alpha(1.5)
.force('charge', d3.forceManyBody().strength(-50))
.force('center', d3.forceCenter(width / 2, height / 2))
.force('forceX', forceX)
.force('forceY', forceY)
.force('collide', forceCollide)
simulation.nodes(nodes).on('tick', ticked)
function ticked() {
nodeElements
.attr('cx', function (node) { return node.x })
.attr('cy', function (node) { return node.y })
}
simulation.force('link')
</script>