forked from RandomEtc/travel-time-tube-d3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
365 lines (299 loc) · 14.1 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Travel Time Tube Map</title>
<script type="text/javascript" src="js/d3.js?1.8.3"></script>
<script type="text/javascript" src="js/d3.csv.js?1.8.3"></script>
<style type="text/css">
body {
margin: 0px 0px 0px 0px;
font-family: helvetica, verdana, geneva, arial, sans-serif;
font-size: 11px;
background-color: #fff;
text-decoration: none;
font-weight: normal;
line-height: normal;
}
a, a:link, a:visited, a:hover, a:active {
color: #3399CC;
text-decoration: underline;
}
#content {
width: 802px;
position: absolute;
margin-left: 25px;
margin-top: 25px;
padding-bottom: 25px;
}
#map {
width: 800px;
height: 500px;
border: 1px solid #aaa;
}
ul {
list-style: none;
padding: 0;
margin: 0;
}
h1 {
margin:0;
padding:0;
}
</style>
<script type="text/javascript">
var pixelsPerMinute = 800.0/150.0;
window.onload = function() {
d3.csv('data/stations.csv', function(stations) {
var w = 800,
h = 500,
minLat = 90,
minLon = 180,
maxLat = -90,
maxLon = -180,
stationsById = {};
// measure...
stations.forEach(function(s) {
stationsById[s.id] = s;
s.conns = [];
s.display_name = (s.display_name == 'NULL') ? null : s.display_name;
s.rail = parseInt(s.rail,10);
s.totalLines = parseInt(s.total_lines,10);
//console.log(s.rail, s.totalLines);
s.latitude = parseFloat(s.latitude);
s.longitude = parseFloat(s.longitude);
minLat = Math.min(minLat, s.latitude);
maxLat = Math.max(maxLat, s.latitude);
minLon = Math.min(minLon, s.longitude);
maxLon = Math.max(maxLon, s.longitude);
});
var padding = 10;
stations.forEach(function(s) {
s.mapx = padding + (w-padding*2) * (s.longitude-minLon) / (maxLon-minLon);
s.mapy = h-padding - (h-padding*2) * (s.latitude-minLat) / (maxLat-minLat);
});
//console.log(stations);
d3.csv('data/lines2.csv', function(connections) {
//console.log(connections);
connections.forEach(function(c) {
c.station1 = stationsById[c.station1];
c.station2 = stationsById[c.station2];
c.station1.conns.push(c);
c.station2.conns.push(c);
c.time = parseInt(c.time,10);
});
d3.csv('data/routes.csv', function(routes) {
//console.log(routes);
var routesById = {};
routes.forEach(function(r) {
routesById[r.line] = r;
});
var vis = d3.select("#map")
.append("svg:svg")
.attr("width", w)
.attr("height", h);
var radii = d3.range(pixelsPerMinute*10, Math.max(w,h), pixelsPerMinute*10);
radii.reverse();
var radius = vis.selectAll('circle.radius')
.data(radii)
.enter().append('svg:circle')
.attr('class','radius')
.attr('fill',function(d,i) { return i%2==0 ? '#e6e6e6' : '#fff' })
.attr('cx',w/2)
.attr('cy',h/2)
.attr('r',Number)
.style('opacity',0);
var route = vis.selectAll("line.route")
.data(connections)
.enter().append("svg:line")
.attr("class", "route")
.attr("stroke", function(d) { return '#'+routesById[d.line].colour; })
.attr("stroke-width", 5)
.attr("stroke-linecap", 'round')
.attr("x1", function(d) { return d.station1.mapx; })
.attr("y1", function(d) { return d.station1.mapy; })
.attr("x2", function(d) { return d.station2.mapx; })
.attr("y2", function(d) { return d.station2.mapy; });
var stripe = vis.selectAll("line.stripe")
.data(connections.filter(function(d) { return routesById[d.line].stripe != "NULL"; }))
.enter().append("svg:line")
.attr("class", "stripe")
.attr("stroke", function(d) { return '#'+routesById[d.line].stripe; })
.attr("stroke-width", 3)
.attr("stroke-linecap", 'round')
.attr("x1", function(d) { return d.station1.mapx; })
.attr("y1", function(d) { return d.station1.mapy; })
.attr("x2", function(d) { return d.station2.mapx; })
.attr("y2", function(d) { return d.station2.mapy; });
var connect = vis.selectAll("circle.connect")
.data(stations.filter(function(d) { return d.totalLines - d.rail > 1; }))
.enter().append("svg:circle")
.attr("class", "connect")
.attr("cx", function(d) { return d.mapx; })
.attr("cy", function(d) { return d.mapy; })
.attr("r", 2.5)
.style("fill", 'white')
.style("stroke", 'black')
.style("stroke-width", 0.5);
var station = vis.selectAll("circle.station")
.data(stations)
.enter().append("svg:circle")
.attr("class", "station")
.attr("id", function(d) { return 'station'+d.id })
.attr("cx", function(d) { return d.mapx; })
.attr("cy", function(d) { return d.mapy; })
.attr("r", 2.5)
.attr("title", function(d) { return d.name })
.style("fill", '#ffff00')
.style("opacity", 0)
.on('mouseover', function(d,i) {
d3.selectAll('#station'+d.id)
.transition()
.duration(25)
.style('opacity', 1.0);
})
.on('mouseout', function(d,i) {
d3.selectAll('#station'+d.id)
.transition()
.duration(25)
.style('opacity', 0.0);
})
.on('click', selectStation);
var option = d3.select('#navi')
.on('change', function() {
var box = document.getElementById('navi'),
destination = box.options[box.selectedIndex].value;
selectStation(stationsById[destination]);
})
.selectAll('option')
.data( [ { id: 0, name: "Geographic Layout" } ].concat(stations) )
.enter()
.append('option')
.attr('value', function(d) { return d.id })
.text(function(d) { return d.name });
window.onkeyup = function(e) {
if (String.fromCharCode(e.keyCode).toLowerCase() == 'g') {
var box = document.getElementById('navi');
box.selectedIndex = 0;
selectStation(null);
}
}
function selectStation(d, i) {
updateShortestPaths(d, stations);
if (i) {
var box = document.getElementById('navi');
box.selectedIndex = i+1;
}
d3.selectAll('circle.radius')
.transition()
.duration(0)
.style("opacity", d ? 1 : 0)
.attr("cx", d ? d.x : w/2)
.attr("cy", d ? d.y : h/2);
d3.selectAll('circle.connect, circle.station')
.transition()
.duration(1000)
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
d3.selectAll("line.route, line.stripe")
.transition()
.duration(1000)
.attr("x1", function(d) { return d.station1.x; })
.attr("y1", function(d) { return d.station1.y; })
.attr("x2", function(d) { return d.station2.x; })
.attr("y2", function(d) { return d.station2.y; });
};
}); // load routes
}); // load lines
}); // load stations
}
// cribbed from here in double quick time: http://www.cs.cmu.edu/~crpalmer/sp/
function updateShortestPaths(centre, stations) {
if (!centre) {
stations.forEach(function(s) {
s.x = s.mapx;
s.y = s.mapy;
});
return;
}
stations.forEach(function(s) {
s.timeToCentre = (s == centre) ? 0 : Number.MAX_VALUE;
s.pathParent = null;
});
var queue = [];
queue.push(centre);
function compareTimes(a,b) {
return a.timeToCentre < b.timeToCentre ? -1 : a.timeToCentre > b.timeToCentre ? 1 : 0;
}
while (queue.length > 0) {
queue.sort(compareTimes);
var v = queue.pop();
for (var i = 0; i < v.conns.length; i++) {
var c = v.conns[i];
var u = (c.station1 == v) ? c.station2 : c.station1;
if (c.time + v.timeToCentre < u.timeToCentre) {
u.pathParent = v;
u.timeToCentre = c.time + v.timeToCentre;
queue.push(u);
}
}
}
stations.forEach(function(s) {
var ang = Math.atan2(s.mapy - centre.mapy, s.mapx - centre.mapx),
rad = pixelsPerMinute * s.timeToCentre; // todo: limit to min(width/2,height/2)?
s.x = centre.mapx + (rad * Math.cos(ang));
s.y = centre.mapy + (rad * Math.sin(ang));
});
}
</script>
</head>
<body>
<div id="content">
<table width="100%"><tr>
<td valign="middle"><h1>Travel Time Tube Map</h1></td>
<td valign="middle" align="right"><form style="float:right">
<select id="navi"></select>
</form></td>
</tr></table>
<p><big>Click on (or select, above) a station to see the London Underground map reorganise around the times of travel from that station. Shortest paths are used to place the other stations - radius is proportional to time to travel, and angle should be correct for as-the-crow-flies direction on a map. The concentric circles are at 10 minute intervals. Press 'g' to get back to the geographical tube map.
</big></p>
<div id="map">
</div>
<p><big><strong>NB:-</strong> times may not be accurate! This is really only a proof of concept, and to me it looks like I'm reporting times which are slightly too fast. You'll have to factor in interchange times and possibly times when the train is stopped if you want to actually make use of this in its current form. Also, it needs a browser that supports SVG and recent Javascript features, only tested in Safari so far, sorry.</big></p>
<p>Inspired by <a href="http://www.oskarlin.com/2005/11/29/time-travel">Oskar Karlin's reworking of the tube map</a> around the time to travel from Elephant and Castle, and Rod McLaren's subsequent sketch <a href="http://www.flickr.com/photos/rodcorp/74944750/"><em>10 Minutes tube travel from Oxford Circus</em></a>. Uses travel times from <a href="http://www.geofftech.co.uk/">Geoff</a> and Neil's <a href="http://www.geofftech.co.uk/tube/sillymaps/travel_times.jpg">travel times map</a> (plus fake ones for DLR, sorry). Station locations are from Wikipedia's <a href="http://commons.wikimedia.org/wiki/London_Underground_geographic_maps">London Underground Geographic Maps</a>. Yes, there are similar maps in existence elsewhere - you might have heard the terms anamorphosis, isochrone or isochronic map used to describe them. Here's <a href="http://www.visualcomplexity.com/vc/project_details.cfm?id=216&index_number=13&DomainName=Transportation Networks">one for Japan's railways</a>.</p>
<p>
Lots of things that need improving...
</p>
<p>Would be nice:
<br>- a key for the different lines
<br>- search for stations (type into the applet)
<br>- list out the shortest path between two stations (not including time to change)
<br>- sort out the 'white dot' connecting stations to match the proper tube map
<br>- proper DLR times to travel (just used 2 mins for everything - tube ones are better)
<br>- display travel card zones
<br>- etc.
</p>
<p>Wishlist:
<br>- sync with realtime TfL data (which lines are down etc.)
<br>- add average time to change lines
<br>- Harry Beck style layout
<br>- filter for stations with toilets, disabled access, etc.
<br>- use curves where appropriate
<br>- sort out overlapping lines
<br>- blobs/contours for time to travel
<br>- interchange times
<br>- time to surface
<br>- etc.
</p>
<p>Suggestions on a postcard (tom [at] tom [dash] carden [dot] co [dot] uk) - particularly for sources of data for travel times, vector versions of Harry
Beck's tube map, etc.</p>
<p>
Data: <a href="data/lines2.csv">lines</a>, <a href="data/routes.csv">routes</a>, <a href="data/stations.csv">stations</a>.
</p>
<p>
Built with <a href="http://mbostock.github.com/d3" title="d3.js">d3.js</a> (adapted from <a
href="http://www.tom-carden.co.uk/p5/tube_map_travel_times/applet/">my original, built with Processing</a>).
</p>
</div>
</body>
</html>