-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
254 lines (211 loc) · 6.95 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
<!DOCTYPE html>
<html class="no-js" data-ng-app="cdApp">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<title>Citibike Data</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/dc/1.7.0/dc.css">
<style>
</style>
</head>
<body>
<h1>Citibike Data</h1>
<div id="chart">
<a class="reset" href="javascript:myChart.filterAll();dc.redrawAll();" style="display: none;">reset</a>
</div>
</div>
<div id="day-totals"></div>
<div id="hourly-avg"></div>
<div id="trip-duration"></div>
<div style='clear:both;'>
<table id="dc-data-table">
<thead>
<tr class="header">
<th>Start Date</th>
<th>Start Station</th>
<th>End Station</th>
<th>Trip Duration</th>
</tr>
</thead>
</table>
</div>
<script src="http://cdn.binaryjs.com/0/binary.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.7/crossfilter.min.js"></script>
<script src="http://dc-js.github.io/dc.js/js/dc.js"></script>
<!-- anchor div for data table -->
<script>
var bikedata = [];
var leftover = "";
var bike = crossfilter();
var all = bike.groupAll();
var date = bike.dimension(function(d) { return d.startdate; });
var hour = bike.dimension(function(d) { return d.startdate.getHours(); });
var dates = date.group(d3.time.month && d3.time.day);
var hours = hour.group();
var tripDuration = bike.dimension(function(d) {return Math.round(d.tripduration/60); });
var tripDurationGroup = tripDuration.group();
var gender = bike.dimension(function(d) { return d.gender; });
var startstation = bike.dimension(function(d) {return d.startstationname; });
var endstation = bike.dimension(function(d) {return d.endstationname; });
var startStationGroup = startstation.group();
var formatNumber = d3.format(",d"),
formatChange = d3.format("+,d"),
formatDate = d3.time.format("%B %d, %Y"),
formatTime = d3.time.format("%I:%M %p");
function updateCharts() {
var dayTotals = dc.lineChart("#day-totals");
var hourlyAvg = dc.barChart("#hourly-avg");
var dataTable = dc.dataTable("#dc-data-table");
var tripDurationAvg = dc.barChart("#trip-duration");
var ridesPerDay = dates.reduceSum(dc.pluck('total'));
var ridesPerHour = hours.reduceSum(dc.pluck('total'));
var ridesPerStart = startStationGroup.reduceSum(dc.pluck('total'));
var minDate = date.bottom(1)[0].startdate;
var maxDate = date.top(1)[0].startdate;
dayTotals
.width(850).height(200)
.dimension(date)
.group(ridesPerDay)
.x(d3.time.scale().domain([minDate,maxDate]))
.yAxisLabel("Rides per day")
.xAxisLabel("Date");
hourlyAvg
.width(420)
.height(180)
.margins({top: 10, right: 50, bottom: 30, left: 40})
.dimension(hour)
.group(ridesPerHour)
.elasticY(true)
.centerBar(true)
.gap(1)
.round(dc.round.floor)
.alwaysUseRounding(true)
.x(d3.scale.linear().domain([0,24]))
.renderHorizontalGridLines(true)
.yAxisLabel("Rides per hour")
.xAxisLabel("Hour")
.filterPrinter(function (filters) {
var filter = filters[0], s = "";
s += numberFormat(filter[0]) + "% -> " + numberFormat(filter[1]) + "%";
return s;
});
hourlyAvg.xAxis().tickFormat(
function (v) { return v; });
hourlyAvg.yAxis().ticks(5);
tripDurationAvg
.width(420)
.height(180)
.margins({top: 10, right: 50, bottom: 30, left: 40})
.dimension(tripDuration)
.group(tripDurationGroup)
.elasticY(true)
.centerBar(true)
.gap(1)
.round(dc.round.floor)
.alwaysUseRounding(true)
.x(d3.scale.linear().domain([0,60]))
.renderHorizontalGridLines(true)
.yAxisLabel("Number of trips")
.xAxisLabel("Minutes")
.filterPrinter(function (filters) {
var filter = filters[0], s = "";
s += numberFormat(filter[0]) + "% -> " + numberFormat(filter[1]) + "%";
return s;
});
dataTable
.dimension(startstation)
.group(function (d) {
var format = d3.format("02d");
return d.startdate.getFullYear() + "/" + format((d.startdate.getMonth() + 1));
})
.size(10) // (optional) max number of records to be shown, :default = 25
.columns([
function (d) {
return formatDate(d.startdate);
},
function (d) {
return d.startstationname;
},
function (d) {
return d.endstationname;
},
function (d) {
return Math.round((d.tripduration)/60);
}
])
.sortBy(function (d) {
return d.startstationname;
})
.order(d3.ascending)
.renderlet(function (table) {
table.selectAll(".dc-table-group").classed("info", true);
});
// var dayOfWeek = bike.dimension(function (d) {
// var day = d.startdate.getDay();
// var name=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
// return day+"."+name[day];
// });
// var dayOfWeekGroup = dayOfWeek.group();
dc.renderAll();
dc.redrawAll();
};
// Connect to Binary.js server
var client = new BinaryClient('ws://localhost:9000');
// Received new stream from server!
client.on('stream', function(stream, meta){
//setInterval("updateCharts()", 10000);
ticker = 0;
// Got new data
stream.on('data', function(transportdata){
receivedData = String.fromCharCode.apply(null, new Uint8Array(transportdata));
parts = receivedData.split('},{');
//console.log(parts);
if (leftover === "") {
var total = "[{";
}
else if (leftover[0] === "{") {
var total = "[";
}
else {
var total = "[{";
}
total = total.concat(leftover);
if (parts[0][0] === "{") {
total = "[";
}
for (i=0; i<parts.length - 2; i++) {
total = total.concat(parts[i]);
total = total.concat('},{')
}
total = total.concat(parts[parts.length-2]);
total = total.concat('}]')
leftover = parts[parts.length - 1];
//console.log("this is total:")
//console.log( total );
//console.log("this is leftover:")
//console.log( leftover );
try {data = JSON.parse( total );}
catch(err) {console.log(total);}
data.forEach(function (d) {
d.startdate = new Date(d.starttime);
d.enddate = new Date(d.stoptime);
d.total = 1;
});
bikedata.push(data);
bike.add(data);
if (ticker % 300 == 0) {
updateCharts();
}
ticker++;
}),
stream.on('end', function(){
updateCharts();
});
});
</script>
</body>