-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.htm
523 lines (464 loc) · 22.1 KB
/
index.htm
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="assets/css/style.css"/>
<title>Taxi</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<header><h1>Visualising New York Taxi Trip Duration</h1></header>
</div>
</div>
<div class="row content">
<div class="col-md-12">
<!-- introduction -->
</div>
</div>
<div class="row content">
<div class="col-md-12">
<nav aria-label="Slide navigation">
<ul id="slide-nav" class="pagination">
<li id="slide-nav-1" class="page-item active"><a class="page-link page" href="#" data-page="1">1</a></li>
<li id="slide-nav-2" class="page-item"><a class="page-link page" href="#" data-page="2">2</a></li>
<li id="slide-nav-3" class="page-item"><a class="page-link page" href="#" data-page="3">3</a></li>
<li id="slide-nav-4" class="page-item"><a class="page-link page" href="#" data-page="4">4</a></li>
</ul>
</nav>
</div>
<!-- charts -->
<div id="slide1">
<div class="col-md-12">
<p>The map of New York is shown and the pickup and drop off locations of the taxi trips in the dataset are shown. The toggle buttons at the side of the chart can be used to show or hide the pickup or dropoff locations.</p>
</div>
<div class="row justify-content-md-center">
<div class="col-md-8">
<figure class="figure">
<svg id="map"></svg>
<figcaption class="figure-caption">Map of New York and Taxi Pickups and Dropoffs</figcaption>
</figure>
</div>
<div class="col-md-2 align-items-center">
<div id="map-controls" class="invisible "><span class="legend-item">Show/Hide</span><span class="legend-item"><button class="btn btn-pickup" id="pickup-control" data-state="1" title="Click to toggle pickups">Pickups</button></span class="legend-item"><span><button id="dropoff-control" class="btn btn-dropoff" data-state="1">Dropoffs</button></span></div>
</div>
<div class="col-md-10">
<p class="annotation">The majority of the taxi pickups and dropoffs are in the Manhattan borough.</p>
</div>
</div>
</div>
<div id="slide2" style="display: none;">
<div class="col-md-12">
<p>The average trip duration and distace for each day of the week are shown. The month select can be used to view the averages for other months.</p>
</div>
<div class="row justify-content-md-center">
<div class="col-md-8">
<figure class="figure">
<svg id="s2-charts"></svg>
<figcaption class="figure-caption">Average Trip Duration and Distance Per Week</figcaption>
</figure>
</div>
<div class="col-md-2 align-items-center">
<div id="s2-chart-controls" class="invisible "><span class="legend-item"><select id="month-selector"></select></span></div>
</div>
<div class="col-md-10">
<p class="annotation">February has the lowest average taxi trip duration.</p>
</div>
</div>
</div>
<div id="slide3" style="display: none;">
<div class="col-md-12">
<p>The average trip duration for each hour of the day of the week are shown. The day select can be used to view average for different days.</p>
</div>
<div class="row justify-content-md-center">
<div class="col-md-8">
<figure class="figure">
<svg id="s3-charts"></svg>
<figcaption class="figure-caption">Average Trip Duration Per Day</figcaption>
</figure>
</div>
<div class="col-md-2 align-items-center">
<div id="s3-chart-controls" class="invisible "><span class="legend-item"><select id="day-selector"></select></span></div>
</div>
<div class="col-md-10 annotation">
<p>The best time to travel by taxi is Sunday at 8am. The worst time Tuesday midnight followed by Saturday at 11pm.</p>
</div>
</div>
</div>
<div id="slide4" style="display: none;">
<div class="col-md-12">
<p>The average trip distance for each hour of the day of the week are shown. The day select can be used to view average for different days.</p>
</div>
<div class="row justify-content-md-center">
<div class="col-md-8">
<figure class="figure">
<svg id="s4-charts"></svg>
<figcaption class="figure-caption">Average Trip Distance Per Day</figcaption>
</figure>
</div>
<div class="col-md-2 align-items-center">
<div id="s4-chart-controls" class="invisible "><span class="legend-item"><select id="day-selector1"></select></span></div>
</div>
<div class="col-md-10 annotation">
<p>The shortest commute times occur between 8 am and 9 am on weekdays. The longest commute times occur between 3 am and 5 am on weekdays.</p>
</div>
</div>
</div>
</div>
<div class="row">
<footer>
<div class="col-md-12">
<p>The dataset is from the New York Taxi Trip Duration on <a href="https://www.kaggle.com/c/nyc-taxi-trip-duration/data">kaggle</a>. The dataset is based on the <a href="https://cloud.google.com/bigquery/public-data/nyc-tlc-trips">2016 NYC Yellow Cab trip record data</a> made available in Big Query on Google Cloud Platform. The data was originally published by the <a href="http://www.nyc.gov/html/tlc/html/about/trip_record_data.shtml">NYC Taxi and Limousine Commission (TLC)</a>.</p>
</div>
</footer>
</div>
</div>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://d3js.org/d3.v5.min.js"></script>
<!-- load data -->
<script>
function togglePoints(btnElem, gElem, onClass, offClass) {
if ($(btnElem).attr("data-state") == 1) { //currently shown - dont show
$(gElem).removeClass("visible").addClass("invisible");
$(btnElem).removeClass(onClass).addClass(offClass);
$(btnElem).attr("data-state", 0);
}
else {
$(gElem).removeClass("invisible").addClass("visible");
$(btnElem).removeClass(offClass).addClass(onClass);
$(btnElem).attr("data-state", 1);
}
}
$(document).ready(function() {
var monthNames = ["January","February","March","April","May","June"];
var dayNames = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var svg = d3.select("#map"),
svg1 = d3.select("#s2-charts"),
svg2 = d3.select("#s3-charts"),
svg3 = d3.select("#s4-charts"),
width = 500,
height = 300,
margin = 50,
viewBoxWidth = width,
viewBoxHeight = height
currentSlide = 1;
svg
.attr("width", width)
.attr("height", height + 3 * margin);
//.attr("preserveAspectRatio", "xMinYMin meet")
//.attr("viewBox", "0 0 " + viewBoxWidth + " " + viewBoxHeight)
//.classed("svg-content", true);
var g = svg.append("g");
(async function() {
const nyc = await d3.json('assets/data/nyc.json');
const pickups = await d3.json('assets/data/pickups.json');
const tripAverages = await d3.json('assets/data/trip-averages-month.json');
const hourlyAverages = await d3.json('assets/data/hourly-averages-week.json');
var albersProjection = d3.geoAlbers()
.scale(19000)
.rotate([70, 0])
.center([0, 40])
.translate([width/2, height/2]);
var conicProjection = d3.geoConicConformal()
.parallels(33, 45)
.rotate(96, -39)
.fitSize([width, height], nyc);
var mercatorProjection = d3.geoMercator()
.translate([width/2, height/2])
.center([0, 40])
.fitSize([width, height + 3 * margin], nyc);
var geoPath = d3.geoPath()
.projection(mercatorProjection);
g.selectAll("path")
.data(nyc.features)
.enter()
.append("path")
.style("opacity", .5)
//.attr("fill", "#ccc")
//.attr("stroke", "#333")
.attr("d", geoPath)
.on("mouseenter", function(d) {
console.log(d);
d3.select(this)
.style("stroke-width", 1.5)
.style("stroke-dasharray", 0);
/*d3.select("#neighborhoodPopover")
.transition()
.style("opacity",1)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY) + "px")
.text(d.properties.neighborhood);*/
})
.on("mouseleave", function(d) {
d3.select(this)
.style("stroke-width", 0.25)
.style("stroke-dasharray", 1)
.text("");
/*d3.select("#neighborhoodPopover")
.transition()
.style("opacity",0);*/
});
var startMapPoints = svg.append("g");
startMapPoints.attr("id","startPoint");
startMapPoints.selectAll("circle")
.data(pickups).enter()
.append("circle")
.attr("fill", "#AC8553")
.attr("cx", function(d) {return mercatorProjection([d.plg, d.plt])[0];})
.attr("cy", function(d) {return mercatorProjection([d.plg, d.plt])[1];})
.attr("r", "2px");
var endMapPoints = svg.append("g");
endMapPoints.attr("id","endPoint");
endMapPoints.selectAll("circle")
.data(pickups).enter()
.append("circle")
.attr("fill", "#537AAC")
.attr("cx", function(d) {return mercatorProjection([d.dlg,d.dlt])[0];})
.attr("cy", function(d) {return mercatorProjection([d.dlg,d.dlt])[1];})
.attr("r", "2px")
.style("opacity", 0.5);
$("#pickup-control").on("click", function() { togglePoints(this, "#startPoint circle", "btn-pickup", "btn-light"); });
$("#dropoff-control").on("click", function() { togglePoints(this, "#endPoint circle", "btn-dropoff", "btn-light"); });
$("#map-controls").removeClass("invisible").addClass("visible");
// chart 2
var largestTime = 0;
var largestDist = 0;
for (i = 0; i < tripAverages.length; i++) {
if (tripAverages[i].averageDist > largestDist) {
largestDist = tripAverages[i].averageDist;
}
if (tripAverages[i].averageTime > largestTime) {
largestTime = tripAverages[i].averageTime;
}
}
monthNames.map(function(val, index) { $("#month-selector").append($("<option>", {value:index+1, text:val})); });
var slide2Data = tripAverages.filter(function(val) { return val.month === parseInt($("#month-selector").val()) });
var chartWidth = Math.trunc((width / 2) - (margin));
svg1
.attr("width", width + 2 * margin)
.attr("height", height + 2 * margin);
var chart1 = svg1.append("g");
const chart1x = d3.scaleBand().range([0, chartWidth]).domain(dayNames).padding(0.4);
const chart1y = d3.scaleLinear().domain([0,largestTime]).range([height,0]);
const chart1ht = d3.scaleLinear().domain([0,largestTime]).range([0,height]);
chart1.attr("transform", "translate("+margin+", "+margin+")")
.selectAll("rect")
.data(slide2Data)
.enter()
.append("rect")
.attr("x", function(d,i) { return chart1x(dayNames[d.dow-1])})
.attr("y", function(d,i) { return chart1y(d.averageTime) })
.attr("width", chart1x.bandwidth())
.attr("height", function(d,i) { return chart1ht(d.averageTime) })
.style("fill", "#B7C13E");
var chart1yaxes = svg1.append("g")
.attr("transform", "translate("+margin+","+margin+")")
.call(d3.axisLeft(chart1y));
var chart1xaxes = svg1.append("g")
.attr("transform", "translate("+margin+","+(height+margin)+")")
.call(d3.axisBottom(chart1x));
svg1.append('text')
.attr('class', 'label')
.attr('x', -(height / 2) - margin)
.attr('y', margin / 2.4)
.attr('transform', 'rotate(-90)')
.attr('text-anchor', 'middle')
.text('Average Time (s)');
var chart2 = svg1.append("g");
const chart2x = d3.scaleBand().range([chartWidth + 2 * margin, width]).domain(dayNames).padding(0.4);
const chart2y = d3.scaleLinear().domain([0,largestDist]).range([height,0]);
const chart2ht = d3.scaleLinear().domain([0,largestDist]).range([0,height]);
chart2.attr("transform", "translate("+margin+", "+margin+")")
.selectAll("rect")
.data(slide2Data)
.enter()
.append("rect")
.attr("x", function(d,i) { return chart2x(dayNames[d.dow-1])})
.attr("y", function(d,i) { return chart2y(d.averageDist) })
.attr("width", chart1x.bandwidth())
.attr("height", function(d,i) { return chart2ht(d.averageDist) })
.style("fill", "#483EC1");
var chart2yaxes = svg1.append("g")
.attr("transform", "translate("+(chartWidth + 3 * margin)+","+margin+")")
.call(d3.axisLeft(chart2y));
var chart2xaxes = svg1.append("g")
.attr("transform", "translate("+margin+","+(height+margin)+")")
.call(d3.axisBottom(chart2x));
svg1.append('text')
.attr('class', 'label')
.attr('x', (-(height / 2) - margin))
.attr('y', (chartWidth + 2 * margin) + (margin / 4))
.attr('transform', 'rotate(-90)')
.attr('text-anchor', 'middle')
.text('Average Distance (m)')
$("#month-selector").on("change", function() {
var selMonth = parseInt($(this).val());
var newData = tripAverages.filter(function(val) { return val.month === selMonth });
var rects1 = chart1.selectAll("rect").data(newData);
rects1.enter()
.append("rect")
.merge(rects1)
.transition()
.duration(1000)
.attr("x", function(d,i) { return chart1x(dayNames[d.dow-1])})
.attr("y", function(d,i) { return chart1y(d.averageTime) })
.attr("width", chart1x.bandwidth())
.attr("height", function(d,i) { return chart1ht(d.averageTime) });
var rects2 = chart2.selectAll("rect").data(newData);
rects2.enter()
.append("rect")
.merge(rects2)
.transition()
.duration(1000)
.attr("x", function(d,i) { return chart2x(dayNames[d.dow-1])})
.attr("y", function(d,i) { return chart2y(d.averageDist) })
.attr("width", chart2x.bandwidth())
.attr("height", function(d,i) { return chart2ht(d.averageDist) });
});
$("#s2-chart-controls").removeClass("invisible").addClass("visible");
//chart 3
largestTime = 0;
for (i = 0; i < hourlyAverages.length; i++) {
if (hourlyAverages[i].averageTime > largestTime) {
largestTime = hourlyAverages[i].averageTime;
}
}
dayNames.map(function(val, index) { $("#day-selector").append($("<option>", {value:index+1, text:val})); });
var slide3Data = hourlyAverages.filter(function(val) { return val.dow === parseInt($("#day-selector").val()) });
svg2
.attr("width", width + 2 * margin)
.attr("height", height + 2 * margin);
var chart3 = svg2.append("g");
const chart3x = d3.scaleBand().range([0, width]).domain([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]).padding(0.4);
const chart3y = d3.scaleLinear().domain([0,largestTime]).range([height,0]);
const chart3ht = d3.scaleLinear().domain([0,largestTime]).range([0,height]);
chart3.attr("transform", "translate("+margin+", "+margin+")")
.selectAll("rect")
.data(slide3Data)
.enter()
.append("rect")
.attr("x", function(d,i) { return chart3x(d.hr)})
.attr("y", function(d,i) { return chart3y(d.averageTime) })
.attr("width", chart3x.bandwidth())
.attr("height", function(d,i) { return chart3ht(d.averageTime) })
.style("fill", "#B7C13E");
var chart3yaxes = svg2.append("g")
.attr("transform", "translate("+margin+","+margin+")")
.call(d3.axisLeft(chart3y));
var chart3xaxes = svg2.append("g")
.attr("transform", "translate("+margin+","+(height+margin)+")")
.call(d3.axisBottom(chart3x));
svg2.append('text')
.attr('class', 'label')
.attr('x', -(height / 2) - margin)
.attr('y', margin / 2.4)
.attr('transform', 'rotate(-90)')
.attr('text-anchor', 'middle')
.text('Average Time (s)');
svg2.append('text')
.attr('class', 'label')
.attr('x', width / 2 + margin)
.attr('y', height + margin * 1.7)
.attr('text-anchor', 'middle')
.text('Hours of Day (24 Hour Clock)');
$('#day-selector').on("change", function() {
var selDay = parseInt($(this).val());
var newData = hourlyAverages.filter(function(val) { return val.dow === selDay });
var rects3 = chart3.selectAll("rect").data(newData);
rects3.enter()
.append("rect")
.merge(rects3)
.transition()
.duration(1000)
.attr("x", function(d,i) { return chart3x(d.hr)})
.attr("y", function(d,i) { return chart3y(d.averageTime) })
.attr("width", chart3x.bandwidth())
.attr("height", function(d,i) { return chart3ht(d.averageTime) })
.style("fill", "#B7C13E");
});
$("#s3-chart-controls").removeClass("invisible").addClass("visible");
//chart 4
largestDist = 0;
for (i = 0; i < hourlyAverages.length; i++) {
if (hourlyAverages[i].averageDist > largestDist) {
largestDist = hourlyAverages[i].averageDist;
}
}
dayNames.map(function(val, index) { $("#day-selector1").append($("<option>", {value:index+1, text:val})); });
var slide4Data = hourlyAverages.filter(function(val) { return val.dow === parseInt($("#day-selector1").val()) });
svg3
.attr("width", width + 2 * margin)
.attr("height", height + 2 * margin);
var chart4 = svg3.append("g");
const chart4x = d3.scaleBand().range([0, width]).domain([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]).padding(0.4);
const chart4y = d3.scaleLinear().domain([0,largestDist]).range([height,0]);
const chart4ht = d3.scaleLinear().domain([0,largestDist]).range([0,height]);
chart4.attr("transform", "translate("+margin+", "+margin+")")
.selectAll("rect")
.data(slide4Data)
.enter()
.append("rect")
.attr("x", function(d,i) { return chart4x(d.hr)})
.attr("y", function(d,i) { return chart4y(d.averageDist) })
.attr("width", chart4x.bandwidth())
.attr("height", function(d,i) { return chart4ht(d.averageDist) })
.style("fill", "#483EC1");
var chart4yaxes = svg3.append("g")
.attr("transform", "translate("+margin+","+margin+")")
.call(d3.axisLeft(chart4y));
var chart4xaxes = svg3.append("g")
.attr("transform", "translate("+margin+","+(height+margin)+")")
.call(d3.axisBottom(chart4x));
svg3.append('text')
.attr('class', 'label')
.attr('x', -(height / 2) - margin)
.attr('y', margin / 2.4)
.attr('transform', 'rotate(-90)')
.attr('text-anchor', 'middle')
.text('Average Distance (m)');
svg3.append('text')
.attr('class', 'label')
.attr('x', width / 2 + margin)
.attr('y', height + margin * 1.7)
.attr('text-anchor', 'middle')
.text('Hours of Day (24 Hour Clock)');
$('#day-selector1').on("change", function() {
var selDay = parseInt($(this).val());
var newData = hourlyAverages.filter(function(val) { return val.dow === selDay });
var rects4 = chart4.selectAll("rect").data(newData);
rects4.enter()
.append("rect")
.merge(rects4)
.transition()
.duration(1000)
.attr("x", function(d,i) { return chart4x(d.hr)})
.attr("y", function(d,i) { return chart4y(d.averageDist) })
.attr("width", chart4x.bandwidth())
.attr("height", function(d,i) { return chart4ht(d.averageDist) })
.style("fill", "#483EC1");
});
$("#s4-chart-controls").removeClass("invisible").addClass("visible");
})();
$("#slide-nav li a.page").on("click", function() {
var slideGoTo = parseInt($(this).attr("data-page"));
if (slideGoTo !== currentSlide) { //do nothing if on current slide
$("#slide-nav li").removeClass("active");
$("#slide-nav-"+slideGoTo).addClass("active");
$("#slide"+currentSlide).css("display", "none");
$("#slide"+slideGoTo).css("display","block");
currentSlide = slideGoTo;
}
return false;
});
});
</script>
</body>
</html>