This repository has been archived by the owner on Dec 23, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heatmap.html
370 lines (334 loc) · 13.5 KB
/
heatmap.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Social Data - Project</title>
<script type="text/javascript" src="d3/d3.min.js"></script>
<link href="CSS/style.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript" src="Scripts/cssrefresher.js"></script>
<style>
text.dayLabel {
font-size: 10pt;
font-family: Calibri;
fill: black;
font-weight: bold;
}
text.timeLabel {
font-size: 8pt;
font-family: Consolas, courier;
fill: black;
}
input[type=button] {
-webkit-box-shadow:rgba(0,0,0,0.2) 0 1px 0 0;
-moz-box-shadow:rgba(0,0,0,0.2) 0 1px 0 0;
box-shadow:rgba(0,0,0,0.2) 0 1px 0 0;
border-bottom-color:#333;
border:1px solid #61c4ea;
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
color:#333;
font-family:'Verdana',Arial,sans-serif;
font-size:14px;
font-weight:700;
padding:5px;
}
.dataset-button {
background-color:#7cceee;
}
.dataset-button:hover {
background-color: #ee9c7c;
text-shadow:#ee9c7c 0 1px 0;
}
.download-button {
background-color:#FA2;
text-shadow:#FE6 0 1px 0;
}
.download-button:hover {
background-color: black;
color: #FA2;
font-style: italic;
}
.heatmapButtons {
border: 1px solid black;
width: 280px;
padding: 10px;
border-radius: 100px;
background-color: gainsboro;
display: inline-block;
}
</style>
</head>
<body>
<div class="col-md-2" data-spy="affix" id="container" style="background-color: teal;">
<div id="header" style="text-align:center">
<img src="logo.png" style="width: 170px;height: 170px;margin-bottom: 10px;" />
<h3>Social Data - 2016</h3>
</div>
<div id="body">
<button type="button" class="btn btn-primary btn-block" class="nav_button" id="nav1">Heatmap:<br/>Incidents</button>
<button type="button" class="btn btn-primary btn-block" class="nav_button" id="nav2">Choropleth:<br/>New York City</button>
<button type="button" class="btn btn-primary btn-block" class="nav_button" id="nav3">Bubble Chart:<br/>Example Text</button>
<button type="button" class="btn btn-primary btn-block" class="nav_button" id="nav4">Connection Map:<br/>Example Text</button>
</div>
<div id="footer">
<p style="font-size: 20px;margin: 0;">Developed by: <br/> <span id="name">Nicolai Dahl, s113411</span> <br/> <span id="name">Christian Andersern, s113990</span>
</div>
</div>
<div class="col-md-10" style="float: right;">
<!--
HEAT MAP {BEGIN}
-->
<div class="visualization" id="visual1">
<div class="header">
<h1>Heatmap: Incidents</h1>
</div>
<div class="description">
<h2>Description</h2>
<p>Some text..
</p>
</div>
<div class="graph">
<h2>Visualization</h2>
<div id="dataset-picker" style="text-align: center;">
<div class="heatmapButtons">
<input type="button" value="Heatmap 1" class="dataset-button" onclick="heatmapChart('heatmap1.csv', 1)" />
<input type="button" value="Download Dataset" class="download-button" onclick="location.href='http://andaweb.dk/Data/heatmap1.csv';" />
</div>
<div class="heatmapButtons">
<input type="button" value="Heatmap 2" class="dataset-button" onclick="heatmapChart('heatmap2.csv', 1)" />
<input type="button" value="Download Dataset" class="download-button" onclick="location.href='http://andaweb.dk/Data/heatmap2.csv';" />
</div>
</div>
<div style="clear: both;"></div>
<div id="center" style="text-align: center;margin-top: 20px;">
<p style="margin-right: 100px;">Visualizing: <span id="heatmaptTitle">Incident count in a borough at a specific hour</span></p>
<div id="svg1"></div>
</div>
</div>
</div>
<!--
HEAT MAP {END}
-->
</div>
<script type="text/javascript">
// INITIAL VARIABLES USED FOR HEATMAP
var margin = { top: 50, right: 0, bottom: 100, left: 60 },
width = 950 - margin.left - margin.right,
height = 430 - margin.top - margin.bottom,
gridSize = 30,
legendElementWidth = gridSize*2.5,
colors = ["#ffffd9","#edf8b1","#c7e9b4","#7fcdbb","#41b6c4","#1d91c0","#225ea8","#253494","#081d58"],
boroughs = ["Bronx", "Brooklyn", "Manhattan", "Queens", "Staten Island"],
categories = ["Cyclist Injured", "Cyclist Killed", "Motorist Injured", "Motorist Killed", "Pedestrians Injured", "Pedestrians Killed", "Persons Injured", "Persons Killed"],
times = ["0am", "1am", "2am", "3am", "4am", "5am", "6am", "7am", "8am", "9am", "10am", "11am",
"0pm", "1pm", "2pm", "3pm", "4pm", "5pm", "6pm", "7pm", "8pm", "9pm", "10pm", "11pm"],
datasets = ["heatmap1.csv", "heatmap2.csv"];
// SVG INITIALIZE
var svg = d3.select("#svg1").append("svg")
.attr("class", "heatmapSVG")
.attr("width", width + margin.left + margin.right + 50)
.attr("height", height + margin.top + margin.bottom - 150)
.attr("transform", "translate(140," + margin.top + ")")
.style("padding-left", 155)
.style("padding-top", 20);
// TIME LABELS
var timeLabels = svg.append("g").attr("class", "timeText").selectAll(".timeLabel")
.data(times)
.enter().append("text")
.text(function(d) { return d; })
.attr("x", function(d, i) { return i * gridSize; })
.attr("y", 0)
.style("text-anchor", "middle")
.attr("transform", "translate(-16, -6)")
.attr("class", "timeLabel");
// BOROUGH LABELS
var boroughLabel = svg.append("g").attr("class", "boroughText").selectAll(".dayLabel")
.data(boroughs)
.enter().append("text")
.text(function (d) { return d; })
.attr("x", 0)
.attr("y", function (d, i) { return i * gridSize; })
.style("text-anchor", "end")
.attr("transform", "translate(-45," + gridSize / 1.5 + ")")
.attr("class", "dayLabel");
var heatmapChart = function(csvFile, num) {
if (csvFile == "heatmap1.csv") {
var id = "BOROUGH";
var time = "TIME";
var value = "OccCount";
var labelsLeft = boroughs;
var svgHeight = 150;
var marginLeft = 140;
var visualBottom = 120;
var visualBottomText = 107;
var maxValue = 0;
var colorText = 5000;
document.getElementById("heatmaptTitle").innerHTML = "Incident count in a borough at a specific hour";
} else {
var id = "Category";
var time = "Time";
var value = "Count";
var labelsLeft = categories;
var svgHeight = 130;
var visualBottom = 30;
var visualBottomText = 18;
var marginLeft = 160;
var maxValue = 2000;
var colorText = 2400;
document.getElementById("heatmaptTitle").innerHTML = "Incident count of a damage type at a specific hour";
}
d3.csv("Data/Project/"+csvFile,
function(d) {
return { identifier: +d[id], hour: +d[time], count: +d[value] };
},
function(error, data) {
if (num == 0) {
// SETUP COLOR SCALE FOR HEATMAP
var colorScale = d3.scale.quantile()
.domain([0, d3.max(data, function (d) { return d.count; }) - maxValue])
.range(colors);
// FILL VARIABLE WITH GRID BLOCKS FOR EACH ROW IN DATASET
var gridBlocks = svg.append("g").attr("class", "grid").selectAll(".hour")
.data(data, function(d) {return d.identifier+' : '+d.hour;});
// kan måske bruges til at lave tooltips ?
//gridBlocks.enter().append("title").text(function(d) { return d.count; });
// GENERATING GRID BLOCKS AND THEIR ATTRIBUTES
gridBlocks.enter().append("rect")
.attr("x", function(d) { return (d.hour - 1) * gridSize; })
.attr("y", function(d) { return (d.identifier - 1) * gridSize; })
.attr("rx", 4)
.attr("ry", 4)
.attr("width", gridSize)
.attr("height", gridSize)
.style("fill", colors[0])
.style("stroke", "black")
.style("stroke-width", "1px");
// Transistion color shift
gridBlocks.transition()
.duration(1000)
.style("fill", function(d) { return colorScale(d.count); });
var indicatorsBelow = svg.append("g").attr("class", "bottomVisual").selectAll(".indicatorsBelow")
.data([0].concat(colorScale.quantiles()), function(d) { return d; });
indicatorsBelow.enter().append("g").attr("class", "indicatorsBelow")
.append("rect")
.attr("x", function(d, i) { return legendElementWidth * i; })
.attr("y", height - visualBottom)
.attr("width", legendElementWidth)
.attr("height", gridSize / 2)
.style("fill", function(d, i) { return colors[i]; })
.style("stroke", "black")
.style("stroke-width", "1px");
indicatorsBelow.append("text")
.attr("class", "mono")
.text(function(d) { return "≥ " + Math.round(d); })
.attr("x", function(d, i) { return legendElementWidth * i + 13; })
.attr("y", height - visualBottomText)
.style("font-family", "Tahoma")
.style("font-size", "9pt")
.style("fill", function(d) {
if (d < colorText) {
return "black";
} else {
return "white";
}
});
} else {
//Update all Labels on the left
svg.select(".boroughText").remove();
svg.select(".grid").remove();;
svg.select(".bottomVisual").remove();;
// SETUP COLOR SCALE FOR HEATMAP
var colorScale = d3.scale.quantile()
.domain([0, d3.max(data, function (d) { return d.count; })])
.range(colors);
// FILL VARIABLE WITH GRID BLOCKS FOR EACH ROW IN DATASET
var gridBlocks = svg.append("g").attr("class", "grid").selectAll(".hour")
.data(data, function(d) {return d.identifier+' : '+d.hour;});
// kan måske bruges til at lave tooltips ?
//gridBlocks.enter().append("title").text(function(d) { return d.count; });
// GENERATING GRID BLOCKS AND THEIR ATTRIBUTES
gridBlocks.enter().append("rect")
.attr("x", function(d) { return (d.hour - 1) * gridSize; })
.attr("y", function(d) { return (d.identifier - 1) * gridSize; })
.attr("rx", 4)
.attr("ry", 4)
.attr("width", gridSize)
.attr("height", gridSize)
.style("fill", colors[0])
.style("stroke", "black")
.style("stroke-width", "1px");
// Transistion color shift
gridBlocks.transition()
.duration(1000)
.style("fill", function(d) { return colorScale(d.count); });
var indicatorsBelow = svg.append("g").attr("class", "bottomVisual").selectAll(".indicatorsBelow")
.data([0].concat(colorScale.quantiles()), function(d) { return d; });
indicatorsBelow.enter().append("g").attr("class", "indicatorsBelow")
.append("rect")
.attr("x", function(d, i) { return legendElementWidth * i; })
.attr("y", height - visualBottom)
.attr("width", legendElementWidth)
.attr("height", gridSize / 2)
.style("fill", function(d, i) { return colors[i]; })
.style("stroke", "black")
.style("stroke-width", "1px");
indicatorsBelow.append("text")
.attr("class", "mono")
.text(function(d) { return "≥ " + Math.round(d); })
.attr("x", function(d, i) { return legendElementWidth * i + 13; })
.attr("y", height - visualBottomText)
.style("font-family", "Tahoma")
.style("font-size", "9pt")
.style("fill", function(d) {
if (d < 5000) {
return "black";
} else {
return "white";
}
});
svg.append("g").attr("class", "boroughText").selectAll(".dayLabel")
.data(labelsLeft)
.enter().append("text")
.text(function (d) { return d; })
.attr("x", 0)
.attr("y", function (d, i) { return i * gridSize; })
.style("text-anchor", "end")
.attr("transform", "translate(-45," + gridSize / 1.5 + ")")
.attr("class", "dayLabel");
d3.select("svg")
.transition()
.duration(500)
.attr("width", width + margin.left + margin.right + 120)
.attr("height", height + margin.top + margin.bottom - svgHeight);
//Update the rectangles inside the diagram
svg.selectAll(".hour rect")
.data(data)
.transition()
.duration(1000)
.attr("x", function(d) { console.log(d.hour); return (d.hour - 1) * gridSize; })
.attr("y", function(d) { return (d.identifier - 1) * gridSize; })
.attr("rx", 4)
.attr("ry", 4)
.attr("class", "hour bordered")
.attr("width", gridSize)
.attr("height", gridSize)
.style("fill", colors[0])
.style("stroke", "black")
.style("stroke-width", "1px");
}
});
};
heatmapChart("heatmap1.csv", 0);
</script>
<script type="text/javascript">
$("#nav1").click(function(){ $('html, body').animate({ scrollTop: $('#visual1').offset().top }, 'slow'); });
$("#nav2").click(function(){ $('html, body').animate({ scrollTop: $('#visual2').offset().top }, 'slow'); });
$("#nav3").click(function(){ $('html, body').animate({ scrollTop: $('#visual3').offset().top }, 'slow'); });
$("#nav4").click(function(){ $('html, body').animate({ scrollTop: $('#visual4').offset().top }, 'slow'); });
</script>
</body>
</html>