-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
492 lines (425 loc) · 17.5 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/dc/1.6.0/dc.css">
<link rel="stylesheet" href="//api.tiles.mapbox.com/mapbox.js/v2.1.2/mapbox.css">
<style type="text/css">
body {
height: 100%;
width: 100%;
}
#hexbin-map {
width: 100%;
height: 500px;
border-bottom: 1px solid #ddd;
margin-bottom: 10px;
}
#stations {
height: 300px;
overflow-y: scroll;
}
#origins {
color: #3182bd;
}
#destinations {
color: #e6550d;
}
#color-scale {
height: 1em;
display: inline-block;
width: 10em;
vertical-align: middle;
background: linear-gradient(to right, #3182bd 0%, #e6550d 100%)
}
.origin {
background-color: #3182bd;
}
.destination {
background-color: #e6550d;
}
</style>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '', 'auto');
ga('send', 'pageview');
</script>
<body>
<div id="viz" style="display:none;">
<div id="hexbin-map"></div>
<div class="container">
<div class="row" style="text-align: center;">
<div class="col-sm-4" id="data-count">
<p>
<strong class="filter-count"></strong> selected out of <strong class="total-count"></strong> records
</p>
</div>
<div class="col-sm-4">
<p>
Origin Heavy <span id="color-scale"></span> Destination Heavy
</p>
</div>
<div class="col-sm-4">
<p>
<a id="showHideStations" class="btn btn-primary" href="javascript:showHideStations();">Show Stations</a>
<button class="btn btn-info" data-toggle="modal" data-target="#aboutModal">About</button>
</p>
</div>
</div>
<div class="row">
<div class="col-sm-2" id="month-chart">
<strong>Month</strong>
<a class="reset" href="javascript:charts.month.filterAll();dc.redrawAll('journeys');" style="display:none;">reset</a>
<div class="clearfix"></div>
</div>
<div class="col-sm-2" id="day-chart">
<strong>Day</strong>
<a class="reset" href="javascript:charts.day.filterAll();dc.redrawAll('journeys');" style="display:none;">reset</a>
<div class="clearfix"></div>
</div>
<div class="col-sm-4" id="hours-chart">
<strong>Start Time</strong>
<span class="filter"></span>
<a class="reset" href="javascript:charts.hour.filterAll();dc.redrawAll('journeys');" style="display:none;">reset</a>
<div class="clearfix"></div>
</div>
<div class="col-sm-4" id="duration-chart">
<strong>Duration (min)</strong>
<span class="filter"></span>
<a class="reset" href="javascript:charts.duration.filterAll();dc.redrawAll('journeys');" style="display:none;">reset</a>
<div class="clearfix"></div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<strong>Only show journeys that</strong>
<div class="list-group">
<div class="checkbox list-group-item">
<label id="origins">
<input type="checkbox" checked> Start in the selected stations
</label>
</div>
<div class="checkbox list-group-item">
<label id="destinations">
<input type="checkbox"> End in the selected stations
</label>
</div>
</div>
</div>
<div class="col-sm-6">
<strong>Stations <a href="javascript:selectAll(true)">select</a>/<a href="javascript:selectAll(false)">deselect</a> all</strong>
<div id="stations" class="list-group"></div>
</div>
</div>
</div>
</div>
<div class="modal fade in" id="aboutModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title"><a href="http://github.com/trevorgerhardt/hexacabi">Hexacabi</a></h4>
</div>
<div class="modal-body">
<p>This is a data explorer for 2.8 million rows of <a href="https://www.capitalbikeshare.com/trip-history-data">Capital Bikeshare</a> data spanning Q4 2013 to Q3 2014. It's an easy way to cut through a few dimensions of the data to help make assumptions that you could investigate further.</p>
<p>The name Hexacabi comes from the hexagonal binning of the data. Binning is a term for grouping a dataset into discrete groups which can be spatial or attribute based. Rectangles are the simplest form of binning, but hexagons are used because they look rounder yet still interlock with their neighbors.</p>
<p>The hexagons are projected based on the bounds and zoom level of the map, then sized by the amount of trips that start and end in their bin. They are filled with a color based on their leaning of origin or destination heavy trips.</p>
<p>All the graphs and inputs are interactive. Play around and have fun!</p>
<p><a href="https://twitter.com/trevorgerhardt">- Trevor</a></p>
<h4>Links & Libraries</h4>
<ul>
<li><a href="http://d3js.org/">D3</a></li>
<li><a href="http://square.github.io/crossfilter/">Crossfilter</a></li>
<li><a href="http://dc-js.github.io/dc.js/">Dimensional Charting with D3 and Crossfilter</a></li>
<li><a href="http://bl.ocks.org/mbostock/4330486">Mike Bostock's Bivariate Hexbinning Example</a></li>
<li><a href="http://indiemaps.com/blog/2011/10/hexbins/">History of Hexbinning</a></li>
</ul>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="//api.tiles.mapbox.com/mapbox.js/v2.1.2/mapbox.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/spin.js/2.0.1/spin.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.7/crossfilter.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/dc/1.6.0/dc.js"></script>
<script src="www/d3.hexbin.v0.min.js"></script>
<script src="www/hexbin.js"></script>
<script>
// Set the mapbox access token
L.mapbox.accessToken = 'INSERT TOKEN';
// Constants
var daysOfWeek = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' ];
var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
var margins = { top: 10, right: 10, bottom: 30, left: 40 };
var height = 150, width = 360;
// Globals
var charts = {}, dimensions, currentDimension, journeys, json, map, showStations = false, stations, terminalToCoords = {}, featureLayer;
$('#aboutModal').modal('show');
var spinner = new Spinner({
zIndex: 1000
}).spin();
document.body.appendChild(spinner.el);
d3.json('www/data/stations.geojson', function(err, data) {
if (err) return window.alert(err);
stations = data;
stations.features.forEach(function(f) {
terminalToCoords[f.properties.terminalName] = f.geometry.coordinates;
});
d3.csv('www/data/processed.csv', parseCSVRow, function(err, rows) {
if (err) return window.alert(err);
json = rows;
document.getElementById('viz').style.display = 'block';
// After displaying, setup the map
map = L.mapbox.map('hexbin-map', 'conveyal.gepida3i', {
touchZoom: false,
scrollWheelZoom: false
}).setView([38.899316235331575, -77.0309829711914], 11);
// GeoJSON feature layer
featureLayer = L.mapbox.featureLayer(stations)
.addTo(map);
// Generate Hexbins
map.on('viewreset', function() {
generateHexbins();
});
processJson();
// Attach to dc.js renderLet
charts.month.renderlet(function(c) {
dc.events.trigger(function() {
// Redraw the hexbins
generateHexbins();
// Count stations
countStations();
});
});
stationify(stations);
countStations();
updateStations();
spinner.stop();
});
});
function processJson() {
journeys = crossfilter(json);
dimensions = createDimensions(journeys);
// Current dimension
currentDimension = dimensions.month;
charts.count = dc.dataCount('#data-count', 'journeys')
.dimension(journeys)
.group(journeys.groupAll());
charts.month = dc.pieChart('#month-chart', 'journeys')
.height(height)
.label(function(d) { return months[d.data.key]; })
.dimension(dimensions.month)
.group(dimensions.month.group());
charts.day = dc.pieChart('#day-chart', 'journeys')
.height(height)
.label(function(d) { return daysOfWeek[d.data.key]; })
.dimension(dimensions.day)
.group(dimensions.day.group());
charts.hour = dc.barChart('#hours-chart', 'journeys')
.height(height)
.width(width)
.margins(margins)
.x(d3.scale.linear().domain([0, 23]))
.elasticY(true)
.round(Math.round)
.dimension(dimensions.hour)
.group(dimensions.hour.group());
charts.duration = dc.lineChart('#duration-chart', 'journeys')
.height(height)
.width(width)
.margins(margins)
.x(d3.scale.linear().domain([0, 60, 200]))
.elasticY(true)
.round(Math.round)
.dimension(dimensions.duration)
.group(dimensions.duration.group(function(d) { return Math.floor(d); }));
for (var i in charts) {
charts[i].on('filtered', function() {
currentDimension = dimensions[i];
});
}
dc.renderAll('journeys');
}
function createDimensions(j) {
return {
day: j.dimension(function(d) { return d.start_time.getDay(); }),
duration: j.dimension(function(d) { return d.duration / 60; }),
hour: j.dimension(function(d) { return d.start_time.getHours(); }),
month: j.dimension(function(d) { return d.start_time.getMonth(); }),
start_terminal: j.dimension(function(d) { return d.start_terminal; }),
end_terminal: j.dimension(function(d) { return d.end_terminal; })
};
}
var id = 0;
function parseCSVRow(d) {
return {
start_time: new Date(+d.start_time),
start_terminal: +d.start_station,
duration: +d.duration,
end_terminal: +d.end_station,
type: d.type
};
}
var _polys = [];
function generateHexbins() {
_polys.forEach(map.removeLayer.bind(map));
var bounds = map.getBounds();
var $map = document.getElementById('hexbin-map');
var rmax = (bounds.getNorth() - bounds.getSouth()) / 30;
// Allllll
var data = currentDimension.top(Infinity);
// Generate hexbins
var originsAndDestinations = data.map(function(d) {
var coords = terminalToCoords[d.start_terminal];
if (!coords) return false;
var j = coords.slice(0);
j.origin = 1;
return j;
}).concat(data.map(function(d) {
var coords = terminalToCoords[d.end_terminal];
if (!coords) return false;
var j = coords.slice(0);
j.origin = -1;
return j;
}));
originsAndDestinations = originsAndDestinations.filter(function(a) { return a; });
var hexbins = hexbin(originsAndDestinations, {
cscale: function(bs) {
var min = Infinity, max = -Infinity;
for (var i = 0; i < bs.length; i++) {
var b = bs[i];
b.color = 0;
for (var j = 0; j < b.length; j++) {
b.color += b[j].origin;
}
b.color /= b.length;
if (b.color > max) max = b.color;
if (b.color < min) min = b.color;
}
return d3.scale.linear()
.domain([ min, max ])
.range([ '#e6550d', '#3182bd' ]);
},
rscale: function(bs) {
return d3.scale.log()
.domain([ 1, d3.max(bs, function(b) { return b.length; }) || 1 ])
.range([ rmax / 3, rmax ]);
},
height: $map.clientHeight,
rmax: rmax,
width: $map.clientWidth
});
_polys = hexbins.map(function(d) {
return L.polygon(d.coords, {
stroke: true,
weight: 1,
color: '#e5e5e5',
fill: true,
fillOpacity: 0.75,
fillColor: d.color
}).addTo(map);
});
}
function stationify(stations) {
var $destinations = $('#destinations input');
var $origins = $('#origins input');
var $stations = $('#stations');
stations.features = stations.features.sort(function(a, b) {
if (a.properties.name > b.properties.name) {
return 1;
} else if (a.properties.name < b.properties.name) {
return -1;
} else {
return 0;
}
});
for (var i in stations.features) {
var station = stations.features[i];
var terminal = station.properties.terminalName;
$stations.append(
'<div class="checkbox list-group-item" id="' + terminal + '">' +
'<span class="badge destination"></span>' +
'<span class="badge origin"></span>' +
'<label>' +
'<input type="checkbox" value="' + terminal + '" checked>' + station.properties.name +
'</label>' +
'</div>');
}
$('input').on('change', function() { updateStations(); });
}
function selectAll(v) {
$('#stations input').prop('checked', v);
updateStations();
}
function updateStations() {
var selectedStations = $('#stations input:checked').map(function(i, input) {
return parseInt(input.value);
}).get();
var start = $('#origins input:checked').length > 0;
var end = $('#destinations input:checked').length > 0;
if (start) {
dimensions.start_terminal.filter(function(d) {
return selectedStations.indexOf(d) !== -1;
});
} else {
dimensions.start_terminal.filterAll();
}
if (end) {
dimensions.end_terminal.filter(function(d) {
return selectedStations.indexOf(d) !== -1;
});
} else {
dimensions.end_terminal.filterAll();
}
featureLayer.setFilter(function(feature) {
return showStations && selectedStations.indexOf(feature.properties.terminalName) !== -1;
});
dc.redrawAll('journeys');
}
function countStations() {
var startCount = dimensions.start_terminal.group().all();
var endCount = dimensions.end_terminal.group().all();
var total = journeys.groupAll().value();
var start = $('#origins input:checked').length > 0;
var end = $('#destinations input:checked').length > 0;
$('#stations .badge').text('0.00%');
for (var i = 0; i < startCount.length; i++) {
var row = startCount[i];
if (!start || $('#' + row.key + ' input:checked').length > 0)
$('#' + row.key + ' .origin').text((row.value / total * 100).toFixed(2) + '%');
}
for (var i = 0; i < endCount.length; i++) {
var row = endCount[i];
if (!end || $('#' + row.key + 'input:checked').length > 0)
$('#' + row.key + ' .destination').text((row.value / total * 100).toFixed(2) + '%');
}
}
function showHideStations() {
var button = document.getElementById('showHideStations');
if (showStations) {
button.innerText = 'Show Stations';
showStations = false;
} else {
button.innerText = 'Hide Stations';
showStations = true;
}
updateStations();
}
function drunkBikers() {
charts.hour.filterAll();
charts.hour.filter([0, 5]);
dc.redrawAll('journeys');
}
</script>
</body>
</html>