Visualizing time dependent networks with d3.js

For a change, here is a post about data visualization. The other day I was thinking about a way of visualizing a time-dependent network in d3.js and in this post I will show a prototype solution.

And there it is, the solution:

As you can see, there is a time slider that allows you to visualize the graph as a function of time. The width of each edge gives information about the strength of the connection between two nodes, the size of each node represents how important a particular node is in a given year.

You can drag the nodes around for a little bit of fun and hovering over each node will tell you the id of the node.

Here is the code that is used to generate the graph (vizgraph.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
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
<!DOCTYPE html>
<meta charset="utf-8">

<link rel="stylesheet" href="vizstyle.css"/>

<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>


<script>

var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
color = d3.scaleOrdinal(d3.schemeCategory10);


/////// SLIDER ///////
var L = 10;
var slider_size = 0.75*width;
var left_margin = 0.5*(width - slider_size);

var x = d3.scaleLinear()
.domain([0,10])
.range([left_margin, slider_size + left_margin])
.clamp(true);

var slider = svg.append("g")
.attr("transform", "translate(15,"+(height-50)+")");

slider.append("line")
.attr("class", "track")
.attr("x1", x.range()[0])
.attr("x2", x.range()[1])
.select(function() { return this.parentNode.appendChild(this.cloneNode(true)); })
.attr("class", "track-inset")
.select(function() { return this.parentNode.appendChild(this.cloneNode(true)); })
.attr("class", "track-overlay")
.call(d3.drag()
.on("start.interrupt", function() { slider.interrupt(); })
.on("start drag", function() { return hue(x.invert(d3.event.x)); }));

var years = d3.range(2010,2016,1)
var dx = L/(years.length-1)
var xticks = d3.range(0,L+dx,dx)

slider.insert("g", ".track-overlay")
.attr("class", "ticks")
.attr("transform", "translate(0," + 25 + ")")
.selectAll("text")
.data(xticks)
.enter().append("text")
.attr("x", x)
.attr("text-anchor", "middle")
.text(function(d,i) { return years[i]; });

var handle = slider.insert("circle", ".track-overlay")
.attr("class", "handle")
.attr("r", 9)
.attr("cx", x.range()[0]); //initial position to zero

function hue(h) {
handle.attr("cx", x(h));
}

function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0).restart();
d.fx = d.x;
d.fy = d.y;
}

function dragged(d) {
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;
}


/////// GRAPH ////////
d3.json("graph.json", function(error, graph) {
if (error) throw error;

//user-defined parameters
var maxDistance = 300, //max distance between two nodes
minDistance = 10, //min distance betwween two nodes
maxRadius = 30, //max radius of circle
minRadius = 8, //min radius of circle
minLinkwidth = 0, //min width of link
maxLinkwidth = 6 //max width of link

var [maxConnect, maxFraction] = getnetworkProp(graph);

var nodes = graph.nodes,
nodeById = d3.map(nodes, function(d) { return d.id; }),
links = graph.links,
value = links.map(function(d){return d.value}),

l = []
links.forEach(function(link) {
var s = nodeById.get(link.source),
t = nodeById.get(link.target),
v = link.value,
y = link.year;

l.push({source: s, target: t, year: y, value:v});
});

links = l

simulation = d3.forceSimulation(nodes)

simulation.force("charge", d3.forceManyBody())
.force("link", d3.forceLink(links))
.on("tick", ticked);

var g = svg.append("g").attr("transform", "translate(" + width / 2 + "," + 0.45 * height + ")"),
link = g.append("g").attr("stroke", "#000").attr("stroke-width", 1.5).selectAll(".link"),
node = g.append("g").attr("stroke", "#fff").attr("stroke-width", 1.5).selectAll(".node");

restart();

d3.interval(function() {
restart();
},150)


function restart() {

var current_year = years[Math.round(x.invert(jQuery(".handle").attr("cx"))/dx)];
//get "radius" of each node for current_year
var fraction = graph.nodes.map(function(d) {return d.fraction}).map(function(d) {return d[current_year.toString()];})

// Apply the general update pattern to the nodes.
node = node.data(nodes, function(d) { return d.id;});
node.exit().remove();
node = node.enter().append("circle")
.attr("class","node")
.attr("fill", function(d) { return color(d.id); })
.merge(node)
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));

node.append("title")
.text(function(d) { return d.id; });

//apply transition to radii of nodes
node.transition()
.duration(50)
.attr("r",function(d) {return Math.max(minRadius,d.fraction[current_year.toString()]/maxFraction * maxRadius);})

// Apply the general update pattern to the links
links_filtered = links.filter(function(d) {return d.year==current_year;});
link = link.data(links_filtered, function(d) { return d.source.id + "-" + d.target.id; });
link.exit().remove();
link = link.enter()
.append("line")
.attr("class", "link")
.merge(link);

//define transition to width of edges
link.transition()
.duration(50)
.attr("stroke-width", function(d,i) { return Math.max(minLinkwidth,d.value/maxConnect * maxLinkwidth);})


// Update and restart the simulation.
simulation.nodes(nodes);
simulation.force("link").links(links)
simulation.force("link", d3.forceLink(links)
.distance(function(d) {
if(d.year==current_year){
return Math.min(maxConnect/d.value * minDistance,maxDistance);
}
else{
return maxDistance;
}})
)

simulation.alpha(0.4).restart();
}

//this function defines position of nodes and links
//at each "simulation time step"
function ticked() {
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })

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; });
}


})

//this function calculates properties of network
//(i.e., max connection between nodes, max fraction value of node)
function getnetworkProp(graph){
//1) max connection between nodes
var maxConnect = Math.max.apply(Math,graph.links.map(function(d) {return d.value;})); //max connection between nodes

//2) max fraction value (used to draw nodes radii)
var maxFraction = 0;
var arr, obj, maxf;
for (i=0;i<graph.nodes.length;i++){
obj = graph.nodes[i].fraction
arr = Object.keys( obj ).map(function (key) { return obj[key]; });
maxf = Math.max.apply( null, arr );
maxFraction = Math.max(maxFraction,maxf);
}
return [maxConnect, maxFraction];
}

</script>

Two more things are missing: the css file and the data file.

Let’s first start with the css file (vizstyle.css)

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
.node {
cursor: pointer;
}

.link {
fill: none;
stroke: #8b8b8b;
}

.ticks {
font: 12px sans-serif;
fill: black;
}

.track,
.track-inset,
.track-overlay {
stroke-linecap: round;
}

.track {
stroke: #000;
stroke-opacity: 0.3;
stroke-width: 10px;
}

.track-inset {
stroke: #ddd;
stroke-width: 8px;
}

.track-overlay {
pointer-events: stroke;
stroke-width: 50px;
cursor: pointer;
}

.handle {
fill: #fff;
stroke: #000;
stroke-opacity: 0.5;
stroke-width: 1.25px;
}

And finally the json file used to generate the graph (graph.json)

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
{
"nodes": [
{"id": "A","fraction":{"2010":0.2,"2011":0.05,"2012":0.1,"2013":0.03,"2014":0.1,"2015":0.02}},
{"id": "B","fraction":{"2010":0.08,"2011":0.07,"2012":0.02,"2013":0.03,"2014":0.1,"2015":0.2}},
{"id": "C","fraction":{"2010":0.12,"2011":0.04,"2012":0.2,"2013":0.02,"2014":0.4,"2015":0.02}},
{"id": "D","fraction":{"2010":0.1,"2011":0.05,"2012":0.02,"2013":0.01,"2014":0.1,"2015":0.03}},
{"id": "E","fraction":{"2010":0.1,"2011":0.4,"2012":0.02,"2013":0.01,"2014":0.1,"2015":0.03}}
],
"links": [
{"source": "A", "target": "B", "value": 30, "year":2010},
{"source": "A", "target": "C", "value": 20, "year":2010},
{"source": "A", "target": "D", "value": 7,"year":2010},
{"source": "A", "target": "E", "value": 3,"year":2010},
{"source": "B", "target": "C", "value": 0,"year":2010},
{"source": "B", "target": "D", "value": 7,"year":2010},
{"source": "B", "target": "E", "value": 3,"year":2010},
{"source": "C", "target": "D", "value": 10,"year":2010},
{"source": "C", "target": "E", "value": 3,"year":2010},
{"source": "D", "target": "E", "value": 7,"year":2010},
{"source": "A", "target": "B", "value": 15, "year":2011},
{"source": "A", "target": "C", "value": 17, "year":2011},
{"source": "A", "target": "D", "value": 1,"year":2011},
{"source": "A", "target": "E", "value": 3,"year":2011},
{"source": "B", "target": "C", "value": 3,"year":2011},
{"source": "B", "target": "D", "value": 9,"year":2011},
{"source": "B", "target": "E", "value": 3,"year":2011},
{"source": "C", "target": "D", "value": 22,"year":2011},
{"source": "C", "target": "E", "value": 10,"year":2011},
{"source": "D", "target": "E", "value": 0,"year":2011},
{"source": "A", "target": "B", "value": 4, "year":2012},
{"source": "A", "target": "C", "value": 3, "year":2012},
{"source": "A", "target": "D", "value": 3,"year":2012},
{"source": "A", "target": "E", "value": 8,"year":2012},
{"source": "B", "target": "C", "value": 10,"year":2012},
{"source": "B", "target": "D", "value": 40,"year":2012},
{"source": "B", "target": "E", "value": 23,"year":2012},
{"source": "C", "target": "D", "value": 3,"year":2012},
{"source": "C", "target": "E", "value": 30,"year":2012},
{"source": "D", "target": "E", "value": 16,"year":2012},
{"source": "A", "target": "E", "value": 8,"year":2012},
{"source": "A", "target": "B", "value": 4, "year":2013},
{"source": "A", "target": "C", "value": 6, "year":2013},
{"source": "A", "target": "D", "value": 7,"year":2013},
{"source": "A", "target": "E", "value": 12,"year":2013},
{"source": "B", "target": "C", "value": 0,"year":2013},
{"source": "B", "target": "D", "value": 11,"year":2013},
{"source": "B", "target": "E", "value": 12,"year":2013},
{"source": "C", "target": "D", "value": 0,"year":2013},
{"source": "C", "target": "E", "value": 20,"year":2013},
{"source": "D", "target": "E", "value": 18,"year":2013},
{"source": "A", "target": "B", "value": 10, "year":2014},
{"source": "A", "target": "C", "value": 10, "year":2014},
{"source": "A", "target": "D", "value": 22,"year":2014},
{"source": "A", "target": "E", "value": 22,"year":2014},
{"source": "B", "target": "C", "value": 2,"year":2014},
{"source": "B", "target": "D", "value": 2,"year":2014},
{"source": "B", "target": "E", "value": 26,"year":2014},
{"source": "C", "target": "D", "value": 4,"year":2014},
{"source": "C", "target": "E", "value": 22,"year":2014},
{"source": "D", "target": "E", "value": 28,"year":2014},
{"source": "A", "target": "B", "value": 18, "year":2015},
{"source": "A", "target": "C", "value": 0, "year":2015},
{"source": "A", "target": "D", "value": 40,"year":2015},
{"source": "A", "target": "E", "value": 40,"year":2015},
{"source": "B", "target": "C", "value": 9,"year":2015},
{"source": "B", "target": "D", "value": 3,"year":2015},
{"source": "B", "target": "E", "value": 40,"year":2015},
{"source": "C", "target": "D", "value": 15,"year":2015},
{"source": "C", "target": "E", "value": 7,"year":2015},
{"source": "D", "target": "E", "value": 11,"year":2015}
]
}