-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·175 lines (145 loc) · 4.88 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
174
175
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<link href="https://fonts.googleapis.com/css?family=Tajawal" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>What's your Rover?</h1>
<svg width="1600" height="800"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(function(d) { return d.id; }).distance(10).strength(0.05))
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
var color = d3.scaleOrdinal(d3.schemeCategory20);
d3.json("web_data.json", function(error, data) {
if (error) throw error;
var web_data = data.users;
var graph = add_group_merge(web_data);
var nodes = create_nodes(graph);
var links = create_links(nodes, graph);
var link = svg.append("g")
.attr("class", "links")
.selectAll("line")
.data(links)
.enter().append("line")
.attr("stroke-width", function(d) { return d.value; });
var node = svg.append("g")
.attr("class", "nodes")
.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r", function(d) { return d.value; })
.attr("fill", function(d) { return color(d.group)})
.on("mouseover", function(d) {
d3.select(this).attr("fill", "gold");
})
.on("mouseout", function(d) {
d3.select(this).attr("fill", function(d) { return color(d.group)})
})
.on("click", function (d) { location.href = d.id;})
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
node.append("title")
.text(function(d) { return d.id; });
simulation
.nodes(nodes)
.on("tick", ticked);
simulation.force("link")
.links(links);
function ticked() {
link
.attr("x1", function(d) { return d.source.x = Math.max(d.value, Math.min(width - d.value, d.source.x)); })
.attr("y1", function(d) { return d.source.y = Math.max(d.value, Math.min(height - d.value, d.source.y)); })
.attr("x2", function(d) { return d.target.x = Math.max(d.value, Math.min(width - d.value, d.target.x)); })
.attr("y2", function(d) { return d.target.y = Math.max(d.value, Math.min(height - d.value, d.target.y)); });
node
.attr("cx", function(d) { return d.x = Math.max(d.value, Math.min(width - d.value, d.x)); })
.attr("cy", function(d) { return d.y = Math.max(d.value, Math.min(height - d.value, d.y)); });
}
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.01).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;
}
///////////////////////////////////////////////////////////////////////////////
function create_nodes(data) {
nodes = [];
var nodes_source = d3.nest()
.key(function(d) { return d.source; })
.entries(data);
var nodes_target = d3.nest()
.key(function(d) { return d.target; })
.entries(data);
var nodes_dict = {}
for (i of nodes_target) {
group = i['values'][0]['group'];
nodes_dict[i.key] = {"value" : i.values.length, "group" : group};
}
console.log(nodes_dict)
for (var key in nodes_dict) {
nodes.push({'id' : key,
'value': (Math.log(nodes_dict[key]['value'] + 1) * 10),
'group' : nodes_dict[key]['group']});
}
return nodes;
}
function add_group_merge(data) {
group = 1
graph = []
diff = 600000;
for (user in data) {
for (session in data[user]) {
if (group == 1) { start = data[user][session].events[0].time_in;}
for (link in data[user][session].events) {
// while ((data[user][session].events[link].time_in - start) >= diff) {
// diff += 600000;
// group++;
// }
data[user][session].events[link]['group'] = group;
graph.push(data[user][session].events[link]);
}
}
group++;
}
return graph;
}
function create_links(nodes, data) {
links = [];
for (var i = 0; i < nodes.length; i++) {
for (var j = 0; j < nodes.length; j++) {
value = 0;
for (var k = 0; k < data.length; k++) {
if ((data[k]['source'] == nodes[i]['id'] &&
data[k]['target'] == nodes[j]['id']) ||
(data[k]['source'] == nodes[j]['id'] &&
data[k]['target'] == nodes[i]['id'])) {
value++;
}
}
if (value != 0) { links.push({'source': nodes[i],
'target': nodes[j],
'value': value})}
}
}
return links;
}
});
</script>
</body>