-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualization.html
184 lines (163 loc) · 5.79 KB
/
visualization.html
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
<!DOCTYPE html>
<!-- Adapted from
http://bl.ocks.org/mbostock/950642
Tree options:
http://bl.ocks.org/mbostock/4063550
https://networkx.github.io/documentation/networkx-1.10/reference/algorithms.traversal.html
-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Music Collaboration Graph</title>
</head>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<meta charset="utf-8">
<style>
.node text {
pointer-events: none;
font: 10px sans-serif;
}
</style>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.html">Musical Collaboration Graph</a>
</div>
<ul class="nav navbar-nav">
<li><a href="visualization.html?graph2">Graph 2</a></li>
<li><a href="visualization.html?graph3">Graph 3</a></li>
<li><a href="visualization.html?graph4">Graph 4</a></li>
<li><a href="visualization.html?graphDrake">Drake</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Artists <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="artistvis.html?graphDrake">Drake</a></li>
<li><a href="artistvis.html?graphRihanna">Rihanna</a></li>
<li><a href="artistvis.html?graphTPain">TPain</a></li>
<li><a href="artistvis.html?graphBieber">Justin Bieber</a></li>
<li><a href="artistvis.html?graphWayne">Lil Wayne</a></li>
</ul>
</li>
</ul>
</div>
</nav>
</div>
</nav>
<body style="padding-top: 55px;">
</body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
JSON_DIR = "http://www.akashlevy.com/Musical-Collab-Graph/graph_jsons/"
// Setup canvas
var highlightedNode = null;
var width = document.documentElement.clientWidth,
height = 2*document.documentElement.clientHeight,
margin = 20;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height - 70)
// Setup force layout
var force = d3.layout.force()
.gravity(0.5)
.distance(200)
.charge(-500)
.size([width, height]);
// Create color scale
var colorScale = d3.scale.linear().domain([1,20]).range(["grey","grey"]);
// Figure out which file to load
i = window.location.href.indexOf("?");
sourceURL = JSON_DIR + window.location.href.substring(i + 1) +".json";
if (i == -1) {
sourceURL = JSON_DIR + "graph4.json";
}
// Fetch graph data
d3.json(sourceURL, function(error, json) {
if (error) throw error;
console.log(json.nodes[0]);
// Assign graph data to the force simulation
force
.nodes(json.nodes)
.links(json.links)
.start();
// Associate a line for each link in the simulation
var link = svg.selectAll(".link")
.data(json.links)
.enter().append("line")
.attr("class", "link")
.attr("stroke", function(d) { return colorScale(d.freq); })
// Setup dragging
var node = svg.selectAll(".node")
.data(json.nodes)
.enter().append("g")
.attr("class", "node")
.call(force.drag);
// Add image to each node
node.append("image")
.attr("xlink:href", function(d) { return d.image_url; })
.attr("x", -8)
.attr("y", -8)
.attr("width", 24)
.attr("height", 24)
// Set mouseover behavior
.on("mouseover", function(d) {
highlightedNode = d;
});
// Add text label to each node
var text = node.append("text")
.attr("dx", 12)
.attr("dy", ".35em")
.text(function(d) { return d.name });
// Set callback to update positions of nodes based on simulation
force.on("tick", function() {
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; })
// Change color if highlighed
.attr("stroke", function(d) {
if (d.source == highlightedNode || d.target == highlightedNode) {
return "red";
} else {
return "grey";
}
})
// Move highlighted links to front
.sort(function(a, b) {
if (a.source == highlightedNode || a.target == highlightedNode) {
return 1;
} else {
if (b.source == highlightedNode || b.target == highlightedNode) {
return -1;
} else {
return 0;
}
}
});
// Set text properties
text.attr("fill", function(d) {
if (d == highlightedNode) {
return "red";
} else {
return "black";
}
})
// Move nodes based on simulation
node.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
// Force nodes to stay on screen
node.attr("cx", function(d) { return d.x = Math.min(Math.max(d.x, margin), width - margin) });
node.attr("cy", function(d) { return d.y = Math.min(Math.max(d.y, margin), height - margin) });
});
});
</script>