-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
415 lines (352 loc) · 13.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
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<script src="lib/leaflet.js"></script>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />
<script src="lib/d3.min.js" charset="utf-8"></script>
<script type="text/javascript" src="lib/q.js"></script>
<script type="text/javascript" src="lib/d3.tip.js"></script>
<style type="text/css">
p.disclaimer{
text-align: center;
}
#index{
width:960px;
margin-left: auto;
margin-right: auto;
margin-bottom: 10px;
}
#index > div > table {
display: inline-table;
border: 1px black solid;
text-align: center;
border-collapse: collapse;
}
#index > div > table > tbody > tr > td {
border: 1px black solid;
text-align: center;
}
.sevColor {
height: 20px;
width: 30px
}
#map {
width: 960px;
height: 500px;
margin-left: auto;
margin-right: auto;
}
svg {
display: block;
position: relative;
margin: auto;
}
#crashData{
border: 1px solid black;
border-collapse: collapse;
margin-left: auto;
margin-right: auto;
}
#crashData td {
border: 1px solid black;
padding: 3px;
}
.d3-tip {
line-height: 1;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
pointer-events: none;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
position: absolute;
pointer-events: none;
}
/* Northward tooltips */
.d3-tip.n:after {
content: "\25BC";
margin: -1px 0 0 0;
top: 100%;
left: 0;
text-align: center;
}
/* Eastward tooltips */
.d3-tip.e:after {
content: "\25C0";
margin: -4px 0 0 0;
top: 50%;
left: -8px;
}
/* Southward tooltips */
.d3-tip.s:after {
content: "\25B2";
margin: 0 0 1px 0;
top: -8px;
left: 0;
text-align: center;
}
/* Westward tooltips */
.d3-tip.w:after {
content: "\25B6";
margin: -4px 0 0 -1px;
top: 50%;
left: 100%;
}
</style>
</head>
<body>
<p class="disclaimer">Bike data from <a href="https://github.com/anton612/bicycledata">here</a> (thanks Anton!), geocoded from Google or done manually.<br/>
Please don't read too much into the intersection's weighted score. I need to consult with a domain expert on an accurate weighting. Mine is <a href="https://github.com/gelicia/bikeCrashViz/blob/master/index.html#L275">very simple</a>.<br/>
Questions or problems, contact <a href="mailto:[email protected]">me</a>.</p>
<div id="index">
<div>
<table>
<tr>
<td colspan="3"><b>Number of Crashes</b></td>
</tr>
<tr>
<td><svg id="sizeCircMin"></svg></td>
<td><svg id="sizeCircMid"></svg></td>
<td><svg id="sizeCircMax"></svg></td>
</tr>
<tr>
<td><div id="sizeTextMin"></div></td>
<td><div id="sizeTextMid"></div></td>
<td><div id="sizeTextMax"></div></td>
</tr>
</table>
<table>
<tr>
<td colspan="3"><b>Severity Score</b></td>
</tr>
<tr>
<td><div id="sevRectMin" class="sevColor"></svg></td>
<td><div id="sevRectMid" class="sevColor"></svg></td>
<td><div id="sevRectMax" class="sevColor"></svg></td>
</tr>
<tr>
<td><div id="sevTextMin"></div></td>
<td><div id="sevTextMid"></div></td>
<td><div id="sevTextMax"></div></td>
</tr>
</table>
</div>
</div>
<div id="map"></div>
<div >
<table id="crashData">
</table>
</div>
<script type="text/javascript">
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
var map = L.map('map').setView([44.96, -93.27], 12);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiZ2VsaWNpYSIsImEiOiJRLUZOV2Z3In0.XYdXJ73QuT8Frxsn1BJ5MA', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
minZoom: 11,
id: 'gelicia.mifl322k',
accessToken: 'pk.eyJ1IjoiZ2VsaWNpYSIsImEiOiJRLUZOV2Z3In0.XYdXJ73QuT8Frxsn1BJ5MA'
}).addTo(map);
var svg = d3.select(map.getPanes().overlayPane).append("svg");
var g = svg.append("g").attr("class", "leaflet-zoom-hide");
var nestData;
loadData().then(function(){
var tip = d3.tip().attr('class', 'd3-tip').html(function(d) {
var intersectString = "Intersection: <b>" + d.values[0]["Primary Road"] + " and " + d.values[0]["Intersecting Road"] + "</b>";
var accidentsHeader = "Accidents With..."
var propDamageString = "Property Damage Only: <b>" + d.intersectPropDamage + "</b>";
var possInjuryString = "Possible Injury: <b>" + d.intersectPossInjury + "</b>";
var nonIncapInjuryString = "Non-Incapacitated Injury: <b>" + d.intersectNonIncapInjury + "</b>";
var incapInjuryString = "Incapacitated Injury: <b>" + d.intersectIncapInjury + "</b>";
var fatalityString = "Fatality: <b>" + d.intersectFatality + "</b>";
var totalScoreString = "Weighted Total Score: <b>" + d.intersectTotal + "</b>";
return intersectString + "<br/>" + accidentsHeader + "<br/>" + propDamageString + "<br/>" + possInjuryString + "<br/>"
+ nonIncapInjuryString + "<br/>" + incapInjuryString + "<br/>" + fatalityString + "<br/>"
+ totalScoreString ;
});
g.call(tip);
var crashes = g.selectAll("circle").data(nestData).enter().append("circle")
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.on('click', function(d){
displayData(d);
});
map.on("viewreset", function(){
reset();
});
reset();
function reset() {
var top = 0;
var bottom = 0;
var left = 0;
var right = 0;
for (var i = 0; i < nestData.length; i++) {
if (i==0){
top = nestData[i].values[0].lat;
bottom = nestData[i].values[0].lat;
left = nestData[i].values[0].long;
right = nestData[i].values[0].long;
}
else {
if (nestData[i].values[0].lat > top){
top = nestData[i].values[0].lat;
}
if (nestData[i].values[0].lat < bottom){
bottom = nestData[i].values[0].lat;
}
if (nestData[i].values[0].long > left){
left = nestData[i].values[0].long;
}
if (nestData[i].values[0].long < right){
right = nestData[i].values[0].long;
}
}
}
var topLeft = map.latLngToLayerPoint(new L.LatLng(top, left));
var bottomRight = map.latLngToLayerPoint(new L.LatLng(bottom, right));
svg.attr("width", bottomRight.x - topLeft.x)
.attr("height", bottomRight.y - topLeft.y)
.style("left", topLeft.x + "px")
.style("top", topLeft.y + "px");
g.attr("transform", "translate(" + -topLeft.x + "," + -topLeft.y + ")");
//Color is intersection score and size being # of accidents
var intersectionSevTotalRange = d3.extent(nestData, function(d){return d.intersectTotal;});
var colorRange = d3.scale.linear().domain(intersectionSevTotalRange).range(['#fff', '#ff0000']);
var intersectionCrashTotalRange = d3.extent(nestData, function(d){return d.values.length;});
//map the number of crashes between a value 3px and 30px (max)
//these will be further influenced by the zoom
var sizeRange = d3.scale.linear().domain(intersectionCrashTotalRange).range([5, 30]);
var zoomRange = d3.scale.linear().domain([11,18]).range([0.1, 1]);
var zoomLevel = map.getZoom();
crashes.attr({
fill:function(d){return colorRange(d.intersectTotal);},
"stroke-width": 1,
"stroke": "black",
opacity: 0.7,
r: function (d) {
return Math.ceil(sizeRange(d.values.length) * zoomRange(zoomLevel));
},
cx: function(d){
var longLat = [d.values[0].long, d.values[0].lat];
var point = map.latLngToLayerPoint(new L.LatLng(longLat[1], longLat[0]));
return point.x;
},
cy: function(d){
var longLat = [d.values[0].long, d.values[0].lat];
var point = map.latLngToLayerPoint(new L.LatLng(longLat[1], longLat[0]));
return point.y;
}
}).moveToFront();
//build the size index. done in a table for easier spacing/organization, unfortunately it means we need to do this in a loop
//not a selection
var indexRange = d3.range(0,1.5,0.5);
for (var i = 0; i < indexRange.length; i++) {
var sizeCircSvg = d3.select("svg#sizeCirc" + (i == 0 ? "Min" : i == 1 ? "Mid" : "Max"));
var sizeText = d3.select("div#sizeText" + (i == 0 ? "Min" : i == 1 ? "Mid" : "Max"));
var sevRectDiv = d3.select("div#sevRect" + (i == 0 ? "Min" : i == 1 ? "Mid" : "Max"));
var sevText = d3.select("div#sevText" + (i == 0 ? "Min" : i == 1 ? "Mid" : "Max"));
//I really wanted to think of a better way to do this but I couldn't :'(
var sizeNum = (indexRange[i] === 0 || indexRange[i] === 1 ? intersectionCrashTotalRange[indexRange[i]] : d3.mean(intersectionCrashTotalRange));
var radius = Math.ceil(sizeRange(sizeNum) * zoomRange(zoomLevel));
sizeCircSvg.attr({
width: 2 * (radius + 1),
height: 2 * (radius + 1)
});
sizeCircSvg.selectAll("circle").remove();
sizeCircSvg.append("circle").attr({
fill: "#fff",
"stroke-width" : 1,
"stroke": "black",
r: radius,
cx: radius + 1,
cy: radius + 1
});
sizeText.text(sizeNum);
var colorNum = (indexRange[i] === 0 || indexRange[i] === 1 ? intersectionSevTotalRange[indexRange[i]] : d3.mean(intersectionSevTotalRange));
sevRectDiv.attr("style", "background-color:" + colorRange(colorNum));
sevText.text(colorNum);
}
}
function displayData(d){
var dataTable = d3.select("#crashData");
dataTable.selectAll("tr").remove();
var rows = dataTable.selectAll("tr>th").data(d.values).enter().append("tr").style("background-color", function(d,i){return i%2 == 1 ? "#dfdfdf" : "#fff"});
rows.append("td").text(function(d){return d["Primary Road"]});
rows.append("td").text(function(d){return d["Intersecting Road"]});
rows.append("td").text(function(d){
var inDate = d["Date"];
var inTime = Array(4-String(d["Time"]).length+1).join("0")+ String(d["Time"]);
var tryDate = new Date(inDate);
tryDate.setHours(inTime.substring(0,2));
tryDate.setMinutes(inTime.substring(2,4))
return tryDate.toLocaleString();
});
rows.append("td").text(function(d){return d["Level"]});
rows.append("td").text(function(d){return d["lat"]});
rows.append("td").text(function(d){return d["long"]});
}
});
function loadData(){
var def = Q.defer();
d3.csv("bicycle-data.csv", function(data) {
nestData = d3.nest().key(function(d){ return d.lat + "," + d.long}).entries(data);
for (var i = 0; i < nestData.length; i++) {
var intersectTotal = 0;
var intersectPropDamage = 0;
var intersectPossInjury = 0;
var intersectNonIncapInjury = 0;
var intersectIncapInjury = 0;
var intersectFatality = 0;
for (var j = 0; j < nestData[i].values.length; j++) {
if (nestData[i].values[j].Level == "Possible Injury (C)"){
intersectPossInjury++;
intersectTotal+=2;
}
else if (nestData[i].values[j].Level == "Fatalities"){
intersectFatality++;
intersectTotal+=50;
}
else if (nestData[i].values[j].Level == "Property Damage Only"){
intersectPropDamage++;
intersectTotal+=1;
}
else if (nestData[i].values[j].Level == "Non-Incap. Injury (B)"){
intersectNonIncapInjury++;
intersectTotal+=3;
}
else if (nestData[i].values[j].Level == "Incap. Injury (A)"){
intersectIncapInjury++;
intersectTotal+=15;
}
}
nestData[i].intersectPropDamage = intersectPropDamage;
nestData[i].intersectPossInjury = intersectPossInjury;
nestData[i].intersectNonIncapInjury = intersectNonIncapInjury;
nestData[i].intersectIncapInjury = intersectIncapInjury;
nestData[i].intersectFatality = intersectFatality;
nestData[i].intersectTotal = intersectTotal;
}
var sortedNestData = nestData.sort(function(a,b){
return b.intersectTotal - a.intersectTotal;
});
console.log(sortedNestData);
def.resolve();
});
return def.promise;
}
</script>
</body>
</html>