-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-site.html
More file actions
445 lines (381 loc) · 14.1 KB
/
Copy pathbasic-site.html
File metadata and controls
445 lines (381 loc) · 14.1 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<!DOCTYPE html>
<html>
<head>
<title>EMPRESTA.ME-Network-Visualization</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Comfortaa:wght@400&display=swap" rel="stylesheet">
<link rel="shortcut icon" href="network.png">
<style>
body,h1 {
font-family: "Raleway", sans-serif;
/* font-family: 'Comfortaa', cursive; */
margin: 0;
overflow: hidden;
}
body, html {height: 100%}
.links line {
stroke-opacity: 1;
stroke-width: 9px;
}
.content {
position: relative;
height: 100%;
background-color: #232323;
z-index: 2;
}
/* Styles for mobile devices */
@media only screen and (max-width: 768px) {
.stats {
color: #232323; /* hide stats */
}
}
/* Styles for PC devices */
@media only screen and (min-width: 769px) {
.stats {
color: #ffffff;
}
}
.stats {
margin-left: 50px;
max-width: 500px;
word-wrap: break-word;
z-index: 0; /* behind the graph */
}
.nodes circle {
stroke: #ff8c00;
stroke-width: 10px;
pointer-events: all;
}
.tooltip {
position: absolute;
text-align: center;
padding: 6px;
font: 12px sans-serif;
background: #ddd;
pointer-events: none;
border-radius: 5px;
pointer-events: none;
display: none;
z-index: 5;
}
</style>
</head>
<body>
<div class="content w3-display-container w3-animate-opacity w3-text-white">
<div class="w3-display-topleft w3-padding-large w3-xlarge">
EMPRESTA.ME
</div>
<div id = "stats" class="w3-display-left w3-padding-large w3-xlarge stats" >
Network Stats
<ul id="node-list"></ul>
</div>
<div class="w3-display-middle" >
<div id="container">
<svg id = "mySvg" width="960" height="600"></svg>
</div>
</div>
</div>
</body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
const filePath = 'data/test_small_data.json';
// function used to compute sizes based on the user's trust value
var sizeScale = d3.scaleLinear()
.domain([0, 100]) // input (trust value)
.range([50, 80]); // output (radius)
//initilize svg or grab svg
var svg = d3.select("#mySvg");
// Create a zoom behavior
var zoom = d3.zoom()
.scaleExtent([1, 5]) // Set the minimum and maximum zoom level
.on(
"zoom", function () {
svg.attr("transform", d3.event.transform)
}
)
.filter(
function () {
return d3.event.type === "wheel";
}
);
// Check if the device is mobile and enable touch events
if ('ontouchstart' in document.documentElement) {
svg.call(zoom.touchable(true));
} else {
svg.call(zoom);
}
var width = svg.attr("width");
var height = svg.attr("height");
//intialize data
d3.json(filePath, function(data) {
//d3.json("data/big_data.json", function(data) {
var graph = data;
//console.log(graph.nodes);
var simulation = d3
.forceSimulation(graph.nodes)
.force(
"link",
d3
.forceLink()
.id(function(d) {
return d.name;
})
.distance(140).strength(0.2)
.links(graph.links)
)
.force("charge", d3.forceCollide().radius(75).strength(0.1))
.force("center", d3.forceCenter(width / 2, height / 2))
.on("tick", ticked);
// Define the div for the tooltip
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var link = svg
.append("g")
.attr("class", "links")
.selectAll("line")
.data(graph.links, function(d) { return d.source.id + "-" + d.target.id; }) // add identifier
.enter()
.append("line")
.on("mouseover", function(d) {
tooltip.style("display", "block")
tooltip.transition()
.duration(200)
.style("opacity", .9);
// Check the boolean value of the other respective link
var otherLink = graph.links.filter(function(link) {
return link.source === d.target && link.target === d.source;
})[0];
var otherValue = otherLink.value;
var thisName = d.source.name;
var thisMessage = d.message;
var otherName = otherLink.source.name;
var otherMessage = otherLink.message;
// display messages if they exist
if (thisMessage && otherMessage) {
tooltip.html(thisName + " : " + thisMessage + "<br>" + otherName + " : " + otherMessage);
} else if (otherMessage) {
tooltip.html( otherName + " : " + otherMessage );
} else if (thisMessage) {
tooltip.html(thisName + " : " + thisMessage );
} else{
tooltip.html("");
}
tooltip.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
// Add mouseout event listener to hide tooltip
.on("mouseout", function(d) {
tooltip.style("display", "none");
})
;
var node = svg
.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(graph.nodes)
.enter()
.append("circle")
.attr("r", function(d) {
// Set the radius to outline the user
return d.is_user ? sizeScale(d.trustValue)/2 : 0;
})
.attr("fill", function(d) {
return "#eee"
})
.attr("pointer-events", "all") // enable mouse events on the circle
.call(
d3
.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.on("mouseenter", function(d) {
//console.log("mouseover")
tooltip.style("display", "block")
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(d.name + " - " + d.trustValue.toFixed(2) + " %")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
// Add mouseout event listener to hide tooltip
.on("mouseout", function(d) {
tooltip.style("display", "none");
})
// Add contextmenu event listener for right button click
.on("contextmenu", function(d) {
d3.event.preventDefault(); // Prevent default right-click context menu
// Code for handling right button click goes here
});
var nodeAgain = svg.selectAll("g.node")
.data(graph.nodes, function(d) { return d.id; });
// Enter any new nodes.
var nodeEnter = nodeAgain.enter().append("svg:g")
.attr("class", "node");
// Append images
var images = nodeEnter.append("image")
.attr("xlink:href", function(d) {
return d.logo; })
.attr("transform", function(d) {
var size = sizeScale(d.trustValue);
return "translate(" + -size/15 + "," + -size/15 + ")";
})
// .attr("transform", function(d) {return d.is_user ? "translate(" + -60-(d.trustValue/4) + "," + -60-(d.trustValue/4) + ")" : "tralate";})
.attr("width", function(d) {
// Set the radius to outline the user
return sizeScale(d.trustValue);
})
.attr("height", function(d) {
// Set the radius to outline the user
return sizeScale(d.trustValue);
})
.attr("pointer-events", "all") // enable mouse events on the image
.call(
d3
.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.on("mouseenter", function(d) {
//console.log("mouseover")
tooltip.style("display", "block")
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(d.name + " - " + d.trustValue.toFixed(2) + " %")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
// Add mouseout event listener to hide tooltip
.on("mouseout", function(d) {
tooltip.style("display", "none");
})
// Add contextmenu event listener for right button click
.on("contextmenu", function(d) {
d3.event.preventDefault(); // Prevent default right-click context menu
// Code for handling right button click goes here
})
;
var names = nodeEnter.append("text")
.attr("text-anchor", "middle")
//.attr("dy", "2em")
.attr("dy", function(d) {
var size = sizeScale(d.trustValue);
return size/2 + size/10; // close enough to the center
})
.attr("fill", "white")
.attr("font-size", "20px")
.attr("font-weight", "bold")
.style("cursor", "default") // Stop cursor icon from changing to text select
.text(function(d) { return d.name; })
.attr("pointer-events", "all") // enable mouse events on the text
.call(
d3
.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended)
)
.on("mouseenter", function(d) {
tooltip.style("display", "block")
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(d.name + " - " + d.trustValue.toFixed(2) + " %")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
// Add mouseout event listener to hide tooltip
.on("mouseout", function(d) {
tooltip.style("display", "none");
})
// Add contextmenu event listener for right button click
.on("contextmenu", function(d) {
d3.event.preventDefault(); // Prevent default right-click context menu
console.log("Right button clicked on circle with data:", d);
})
;
function updateStats(nodes) {
const nodeList = document.getElementById("node-list");
// clear previous list of nodes
nodeList.innerHTML = "";
// add each node to the list
nodes.forEach(node => {
const li = document.createElement("li");
li.textContent = `${node.name} - ${node.trustValue} %`;
nodeList.appendChild(li);
});
}
function ticked() {
images
.attr("x", function(d) { return d.x - sizeScale(d.trustValue)/2 + sizeScale(d.trustValue)/15;}) // i have no idea wtf is going on here
.attr("y", function(d) { return d.y - sizeScale(d.trustValue)/2 + sizeScale(d.trustValue)/15 ;})
;
names
.attr("x", function(d) { return d.x;})
.attr("y", function(d) { return d.y - sizeScale(d.trustValue)/2 ;})
;
link
.attr("x1", function(d) {
return d.source.x;
})
.attr("y1", function(d) {
return d.source.y;
})
.attr("x2", function(d) {
return d.target.x;
})
.attr("y2", function(d) {
return d.target.y;
})
.attr("stroke", function(d) {
// each link is duplicated so bob->alice may be 1 and alice->bob may be 0
// if either is 0, the link is red, otherwise its green
var one_0_other_1 = graph.links.find(function(l) {
return l.source.id === d.target.id && l.target.id === d.source.id && l.value !== d.value;
});
if (one_0_other_1) {
return "#b53a2f";
} else if (d.value === 1) {
return "#00946c";
} else {
return "#b53a2f";
}
});
node
.attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
});
updateStats(graph.nodes); // call the function to update the stats div
}
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
if (tooltip.style("display") === "block") {
tooltip.style("display", "none");
}
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
});
</script>