-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
173 lines (145 loc) · 4.51 KB
/
index.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
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="sidebar">
<h1>Movie Popularity Network</h1>
<p>
A grapical visualization of recent movies' popularity with their main actors and directors.
</p>
<p>
More features are under construction.
</p>
<div id="footer">
<div><a href="https://github.com/liusiqi43/movie-impact-graph/tree/master">SOURCE</a> -- Hacked during a super productive Sunday afternoon.</div>
<br>
<div>Data provided by: themoviedb.org</div>
</div>
<h2>Nodes</h2>
<ul class="nodes-info">
<li>
<span class="circle movie"></span>
<span class="title">Movie</span>
</li>
<li>
<span class="circle actor"></span>
<span class="title">Actor</span>
</li>
<li>
<span class="circle director"></span>
<span class="title">Director</span>
</li>
</ul>
</div>
<div id="d3-canvas"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.13/d3.js"></script>
<script>
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('#d3-canvas')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
var format = d3.format(",d"),
color = d3.scale.category10();
var zoom = d3.behavior.zoom()
.scaleExtent([1, 5])
.on("zoom", zoomed);
var drag = d3.behavior.drag()
.origin(function(d) { return d; })
.on("dragstart", dragstarted)
.on("drag", dragged)
.on("dragend", dragended);
var force = d3.layout.force()
.charge(-800)
.linkDistance(100)
.size([x, y]);
var svg = d3.select("body").append("svg")
.attr("width", x)
.attr("height", y)
.append("g")
.call(zoom);
var rect = svg.append("rect")
.attr("width", x)
.attr("height", y)
.style("fill", "none")
.style("pointer-events", "all");
var container = svg.append("g");
// var svg = d3.select("body").append("svg")
// .attr("width", width)
// .attr("height", height);
d3.json("data/graph.json", function(error, graph) {
force
.nodes(graph.nodes)
.links(graph.links)
.start();
var link = container.append("g")
.selectAll("link")
.data(graph.links)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); })
.style("stroke", function(d) {return color(d.group)});
var gnodes = container.append("g")
.selectAll("g.gnode")
.data(graph.nodes)
.enter().append("g")
.call(drag)
.call(force.drag); // comment to disable dragging on single node.
var node = gnodes.append("circle")
.attr("class", "node")
// .attr("r", function(d) {return Math.min(50, d.popularity)})
.attr("r", function(d) {return d.vote_average})
.style("fill", function(d) { return color(d.group); });
gnodes.on("mouseover", fade(node, link, .2))
.on("mouseout", fade(node, link, 1));
gnodes.append("text")
.attr("class", function(d){ return "nodetext" })
.attr("dx", 0)
.attr("dy", ".35em")
.style("font-size","15px")
.attr("text-anchor", "middle")
.style("fill", "white")
.text(function(d) { return d.name });
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; });
gnodes.attr("transform", function(d) {
return 'translate(' + [d.x, d.y] + ')';
});
});
});
function zoomed() {
container.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
function dragstarted(d) {
d3.event.sourceEvent.stopPropagation();
d3.select(this).classed("dragging", true);
}
function dragged(d) {
}
function dragended(d) {
d3.select(this).classed("dragging", false);
}
function isConnected(a, b) {
return b.connected_group != -1 && a.connected_group != -1 && a.connected_group == b.connected_group;
}
function fade(node, link, opacity) {
return function(d) {
node.style("stroke-opacity", function(o) {
node_opacity = isConnected(d, o) ? 1 : opacity;
this.setAttribute('fill-opacity', node_opacity);
return node_opacity;
});
link.style("stroke-opacity", function(o) {
// return o.source === d || o.target === d ? 1 : opacity;
return isConnected(graph.nodes[d], graph.nodes[o]) ? 1 : opacity;
})
};
}
</script>
</body>