-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrimeByState.html
401 lines (332 loc) · 11.4 KB
/
CrimeByState.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<h1 id = "title" align="center">Crime Distribution as a Percent Of Population (1994-2014)</h1>
<br><br>
<link rel="stylesheet" type="text/css" href="line.css">
<h3 id = "bottomMenu" align = "center">
<div id="navcontainer">
<ul id="navlist">
<li id="active"><li><a href="index.php">Crime Data in US</a></li>
<li><a href="CrimeByState.html">Crime in Percentages</a></li>
<li><a href="CrimeByWeapon.html">Crime By Weapon</a></li>
<li><a href="CrimeByRace.html">Crime By Race</a></li>
<li><a href="MostDangerousCities.html">Most Dangerous Cites</a></li>
<li><a href="VictimPerpAge.html">Victim Perpetrator Age</a></li>
<li><a href="CrimeSolved.html">Crimes Solved</a></li>
</ul>
</div>
</h3>
<br>
</head>
<style>
body {
background-color: #ffffff;
text-align: center;
margin: 0 auto;
}
#canvas-svg {
text-align: center;
}
/* stylesheet for your custom graph */
.states {
fill: none;
stroke: #fff;
stroke-linejoin: round;
}
.states-choropleth {
fill: #ccc;
}
.legend {
position:fixed;
right:150px;
top:330px;
font-size: 0.9em;
font-family: sans-serif;
font-weight: 600;
}
#tooltip-container {
position: absolute;
background-color: #fff;
color: #000;
padding: 10px;
font: 13px sans-serif;
background: white;
border: 0px;
border-radius: 5px;
display: none;
}
.tooltip_key {
font-weight: bold;
}
.tooltip_value {
margin-left: 20px;
float: right;
}
.option-select {
margin-top: 20px;
margin-left: 300px;
fill: #64b646;
width: auto;
padding: 5px;
font-weight: bold;
}
</style>
<div class="graphWrapper" id="graphLine"></div>
<div id="selectors"></div>
<div id="tooltip-container"></div>
<div id="canvas-svg" align="center">
<div id="map" align="right"> </div>
<div id="leg"> </div>
</div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.1.0/topojson.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
</script>
</body>
<script>
d3.csv("CrimeByStatePercent.csv", function(err, data) {
var config = {"color1":"#d3e5ff","color2":"#08306B","stateDataColumn":"State",
"defaultValue":"TotalCountOfIncidents","state":"State"};
var WIDTH = 1200, HEIGHT = 600;
var COLOR_COUNTS = 50;
var SCALE = 0.9;
function Interpolate(start, end, steps, count) {
var s = start,
e = end,
final = s + (((e - s) / steps) * count);
return Math.floor(final);
}
function Color(_r, _g, _b) {
var r, g, b;
var setColors = function(_r, _g, _b) {
r = _r;
g = _g;
b = _b;
};
setColors(_r, _g, _b);
this.getColors = function() {
var colors = {
r: r,
g: g,
b: b
};
return colors;
};
}
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
function valueFormat(d) {
if (d > 1000000000) {
return Math.round(d / 1000000000 * 10) / 10 + "B";
} else if (d > 1000000) {
return Math.round(d / 1000000 * 10) / 10 + "M";
} else if (d > 1000) {
return Math.round(d / 1000 * 10) / 10 + "K";
} else {
return d;
}
}
var fields = Object.keys(data[0]);
var option_select = d3.select('#selectors').append("select")
.attr("class", "option-select");
for (var i = 0; i < fields.length; i++) {
if (fields[i] !== config.state) {
var opt = option_select.append("option")
.attr("value", fields[i])
.text(fields[i]);
if (fields[i] === config.defaultValue) {
opt.attr("selected", "true");
}
}
}
var COLOR_FIRST = config.color1, COLOR_LAST = config.color2;
var rgb = hexToRgb(COLOR_FIRST);
var COLOR_START = new Color(rgb.r, rgb.g, rgb.b);
rgb = hexToRgb(COLOR_LAST);
var COLOR_END = new Color(rgb.r, rgb.g, rgb.b);
var width = WIDTH,
height = HEIGHT;
var startColors = COLOR_START.getColors(),
endColors = COLOR_END.getColors();
var colors = [];
for (var i = 0; i < COLOR_COUNTS; i++) {
var r = Interpolate(startColors.r, endColors.r, COLOR_COUNTS, i);
var g = Interpolate(startColors.g, endColors.g, COLOR_COUNTS, i);
var b = Interpolate(startColors.b, endColors.b, COLOR_COUNTS, i);
colors.push(new Color(r, g, b));
}
var quantize = d3.scaleQuantize()
.domain([0, 1.0])
.range(d3.range(COLOR_COUNTS).map(function(i) { return i }));
var path = d3.geoPath();
var svg = d3.select("#map").append("svg")
.attr("width", width)
.attr("height", height);
d3.tsv("us-state-names.tsv", function(error, names) {
d3.json("https://unpkg.com/us-atlas@1/us/10m.json", function(error, us) {
var name_id_map = {};
var id_name_map = {};
for (var i = 0; i < names.length; i++) {
name_id_map[names[i].name] = names[i].id;
id_name_map[names[i].id] = names[i].name;
}
var dataMap = {};
data.forEach(function(d) {
if (!dataMap[d[config.state]]) {
dataMap[d[config.state]] = {};
}
for (var i = 0; i < Object.keys(data[0]).length; i++) {
if (Object.keys(data[0])[i] !== config.state) {
dataMap[d[config.state]][Object.keys(data[0])[i]] =
+d[Object.keys(data[0])[i]];
}
}
});
function drawMap(dataColumn) {
var valueById = d3.map();
data.forEach(function(d) {
var id = name_id_map[d[config.state]];
valueById.set(id, +d[dataColumn]);
});
d3.select("svg.legend").remove();
key = d3.select("#metrics")
.append("svg")
.attr("width", w)
.attr("height", h)
.attr("class", "legend");
quantize.domain([
d3.min(data, function(d){ return +d[dataColumn] }),
d3.max(data, function(d){ return +d[dataColumn] })]);
svg.append("g")
.attr("class", "states-choropleth")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("transform", "scale(" + SCALE + ")")
.style("fill", function(d) {
if (valueById.get(d.id)) {
var i = quantize(valueById.get(d.id));
var color = colors[i].getColors();
return "rgb(" + color.r + "," + color.g +"," + color.b + ")";
} else {
return "";
}
})
.attr("d", path)
.on("mousemove", function(d) {
var html = "";
html += "<div class=\"tooltip_kv\">";
html += "<span class=\"tooltip_key\">";
html += id_name_map[d.id];
html += "</span>";
html += "</div>";
for (var i = 1; i < Object.keys(data[0]).length; i++) {
html += "<div class=\"tooltip_kv\">";
html += "<span class='tooltip_key'>";
html += Object.keys(data[0])[i];
html += "</span>";
html += "<span class=\"tooltip_value\">";
html += valueFormat(dataMap[id_name_map[d.id]][Object.keys(data[0])[i]]);
html += "%";
html += "";
html += "</span>";
html += "</div>";
}
$("#tooltip-container").html(html);
$(this).attr("fill-opacity", "0.7");
$("#tooltip-container").show();
var coordinates = d3.mouse(this);
var map_width = $('.states-choropleth')[0].getBoundingClientRect().width;
if (d3.event.layerX < map_width / 2) {
d3.select("#tooltip-container")
.style("top", (d3.event.layerY + 15) + "px")
.style("left", (d3.event.layerX + 15) + "px");
} else {
var tooltip_width = $("#tooltip-container").width();
d3.select("#tooltip-container")
.style("top", (d3.event.layerY + 15) + "px")
.style("left", (d3.event.layerX - tooltip_width - 30) + "px");
}
})
.on("mouseout", function() {
$(this).attr("fill-opacity", "1.0");
$("#tooltip-container").hide();
});
svg.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "states")
.attr("transform", "scale(" + SCALE + ")")
.attr("d", path);
// add a legend
var dataArray = [];
for (var d = 0; d < data.length; d++) {
//dataArray.push(parseFloat(data[d].TotalCountOfIncidents))
dataArray.push(parseFloat(data[d][dataColumn]))
}
var minVal = d3.min(dataArray)
var maxVal = d3.max(dataArray)
var ramp = d3.scaleLinear().domain([minVal,maxVal]).range([lowColor,highColor])
var lowColor = '#deebf7'
var highColor = '#08306b'
var w = 100, h = 240;
var key = d3.select("#leg")
.append("svg")
.attr("width", w)
.attr("height", h)
.attr("class", "legend");
var legend = key.append("defs")
.append("svg:linearGradient")
.attr("id", "gradient")
.attr("x1", "100%")
.attr("y1", "0%")
.attr("x2", "100%")
.attr("y2", "100%")
.attr("spreadMethod", "pad")
.attr("labelFormat",(d3.format("s")));
legend.append("stop")
.attr("offset", "0%")
.attr("stop-color", highColor)
.attr("stop-opacity", 1);
legend.append("stop")
.attr("offset", "100%")
.attr("stop-color", lowColor)
.attr("stop-opacity", 1);
key.append("rect")
.attr("width", w - 80)
.attr("height", h)
.style("fill", "url(#gradient)")
.attr("transform", "translate(0,10)");
var y = d3.scaleLinear()
.range([h, 0])
.domain([minVal, maxVal]);
var yAxis = d3.axisRight(y).tickFormat(function(d){return d + "%"});
key.append("g")
.attr("class", "y axis")
.attr("transform", "translate(20,10)")
.call(yAxis)
}
drawMap(config.defaultValue);
option_select.on("change", function() {
drawMap($("#selectors").find(".option-select").val());
});
});
});
});
</script>
<h4 align ="justify">
<li>Use the drop down to select type of crime and mouse over to see the totals during the period for each state. The results are quiet surprising. </li><br>
<li> Averages for each crime has been aggregated as a percent of the total population in the state for the particular year and aggregated again to compute the average for the state for the 2 decades. </li><br>
<li>Across the nation the percentages for Larceny, Burglary and Property Crimes are higher. But some of the least populous states like North Dakota, South Dakota, Wyoming etc have an increased precentages of these misdemeanor. Also it is likely that these numbers are slightly different across the states because it depends on the state law as to whether they get charged as a felony or a misdemeanor. </li><br>
</h4>
</body>
</html>