-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjourneys-vis.html
More file actions
271 lines (196 loc) · 5.91 KB
/
journeys-vis.html
File metadata and controls
271 lines (196 loc) · 5.91 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
<!doctype html>
<html>
<!-- copy this template into a new file to start a new project -->
<head>
<script type="text/javascript" src="d3/d3.v3.js"></script>
</head>
<body>
<p id="remove">click here to remove a link</p>
<p id="add">click here to add a link</p>
<p id="timeframe">Click here to advance the timeframe</p>
<script type="text/javascript">
// load dataset.
// nodes represent nodes in the drupal db.
// links represent user motion from node to node.
// links all have a time associated with them.
// var dataset = {
var nodes = [
{ id: "0"},
{ id: "1" },
{ id: "2" },
{ id: "3" },
{ id: "4" },
{ id: "5" },
{ id: "6" },
{ id: "7" },
{ id: "8" },
{ id: "9" }
];
// using links instead of edges for name of this variable,
// because d3 force layout calls them links.
var links = [
{ source: nodes[0], target: nodes[1], time: 1 },
{ source: nodes[2], target: nodes[3], time: 7 },
{ source: nodes[3], target: nodes[4], time: 8 },
{ source: nodes[3], target: nodes[5], time: 9 }
];
// };
var timeFrame = [0, 5];
// set up the remove button
d3.select("#remove")
.on("click", function() {
links.pop();
start(nodes, links);
});
d3.select("#add")
.on("click", function() {
var done = true;
do {
var randomSource = Math.floor(Math.random() * nodes.length);
var randomTarget = Math.floor(Math.random() * nodes.length);
console.log("source: " + randomSource + " target: " + randomTarget);
for(var i; i < links.length; i++) {
if ((nodes[i].source === randomSource && nodes[i].target === randomTarget)
||
(nodes[i].target === randomSource && nodes[i].source === randomTarget))
{
done = false;
} // sending us through the loop again
}
} while (!done);
links.push({source: nodes[randomSource], target: nodes[randomTarget], time: 2});
start(nodes, links);
});
d3.select("#timeframe")
.on("click", function() {
timeFrame[1] = timeFrame[1] + 1;
start(nodes, links);
d3.select("#timeframe")
.text("timeFrame[1]: " + timeFrame[1]);
});
// create the canvas
var svg = d3.select("body").append("svg");
// var controlsvg = d3.select("body").append("svg");
var height = 500;
var width = 500;
svg.style("width", width)
.style("height", height)
.style("border", "5px black solid")
;
/*
controlsvg.style("width", width)
.style("height", height)
.style("border", "5px black solid")
;
*/
// create the force layout
var force = d3.layout.force()
.charge(-400)
.linkDistance(120)
.size([width, height])
.nodes(nodes)
.links(links)
.on("tick", tick)
;
var link = svg.selectAll(".link")
// .data(dataset.links)
// .enter().append("line")
// .style("stroke", "#ccc")
// .style("stroke-width", 3)
;
// function drawLinks() {
// // console.log(links);
// // links = links.data(force.links(), function(d) { return d.source.id + "-" + d.target.id; });
// // // console.log(links);
// // links.enter().append("line", ".node").attr("class", "link");
// // links.exit().remove();
// // var links = svg.selectAll("line")
// // links.data(dataset.links)
// // .enter().append("line")
// // ;
// link = link.data(force.links())
// .enter().append("line")
// .style("stroke", "#ccc")
// .style("stroke-width", 3)
// ;
// // // update
// link.attr("id", function(d) { return d.id; })
// .style("stroke", "#ccc")
// .style("stroke-width", 3);
// link.exit().remove();
// // tick();
// }
// drawLinks();
// var node = svg.selectAll("circle")
// .data(nodes)
// .enter().append("circle")
// .attr("r", 10)
// .style("fill", "salmon")
// .style("stroke", "#000")
// .style("stroke-width", 2)
// .call(force.drag)
// ;
// necessary to get d3 to draw elements in the right order (nodes on
// top of links, rather than under them)
svg.append("g").attr("id", "links");
svg.append("g").attr("id", "nodes");
var link = svg.select("#links").selectAll(".link");
var node = svg.select("#nodes").selectAll(".node");
function start(nodes, links){
// update links
link = link.data(links, function(d) { return d.source.id + "-" + d.target.id; });
link.enter().append("line")
.style("stroke", "#ccc")
.style("stroke-width", 3)
;
link.exit().remove();
// update
link.attr("id", function(d) { return d.id; });
link.classed("link", true );
;
node = node.data(nodes, function(d) { return d.id;});
node.enter().append("circle");
node.attr("r", 10)
.style("fill", "salmon")
.style("stroke", "#000")
.style("stroke-width", 2)
.classed("node", true)
.call(force.drag);
node.exit().transition()
.style("stroke-width", 0)
.remove();
force.start();
}
// force.on("tick", tick);
function tick() {
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; });
}
function selectTimeFrame(timeFrame)
{
var selectedLinks = [];
for (var i = 0; i < links.length; i++) {
if (links[i].time > timeFrame[0] &&
links[i].time <= timeFrame[1])
{
selectedLinks.push(links[i]);
}
}
return selectedLinks;
}
// dataset = selectTimeFrame(timeFrame);
start(nodes, selectTimeFrame(timeFrame));
// push a new link to dataset.links
setTimeout(function () {
links.push({source: nodes[7], target: nodes[5], time: 2});
start(nodes, selectTimeFrame(timeFrame));
}, 1000);
// drawLinks();
// start(dataset.nodes, selectTimeFrame(timeFrame));
</script>
</body>
<html>