-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
280 lines (242 loc) · 7.87 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
<!DOCTYPE html>
<head>
<meta property="og:url" content="http://mahdisadjadi.com/phoenixcrime/" />
<meta property="og:image" content="https://github.com/Mahdisadjadi/phoenixcrime/raw/master/heatmap.png" />
<meta property="og:type"/>
<meta property="og:title" content="Weekly trends of crimes in Phoenix, AZ" />
<meta property="og:description" content="Study crime trends" />
<meta property="fb:app_id"/>
<meta name="keywords" content="phoenix,crime,datascience,dataanalysis,d3,arizona">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<meta charset="utf-8">
<style>
body{
top:10%;
background-color: white;
font-size: 18px;
font-family: 'Open Sans', sans-serif;
text-align: center;
}
h1 {
text-align: center;
}
h4 {
text-align: center;
}
#header{
margin: 40px auto;
width: 50%;
}
#container{
/*background: #a1d99b;*/
height: 500;
margin: 2% 14% 5% 14%;
border-radius:8px;
/*border: 1px solid black;*/
}
#chart {
background-color: #e5f5e0;
width: 400;
height: 300;
margin: 2% 10% 4% 10%;
display:block;
border-radius:8px;
color:black;
border: 0.1px solid black;
}
#share{
}
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* 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);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
<body>
<div id='header'>
<H1>Weekly trends of crimes in Phoenix, AZ</H1>
<h4>Updated on: 30 Jan 2018</h4>
<p align="justify">
The City of Phoenix has collected and published its crime dataset
since Novermber 2015 (<a href="https://www.phoenix.gov/opendata"
target="_blank">Phoenix's open data</a>). This website shows the weekly trend of crimes
in various categories. In the future, the website will also show
the monthly and annual trends as well as a map of crimes.
Feel free to explore and share!
</p>
</div>
<div id="container" align="center">
<div>
<select onchange="loadData()" id="select_crime"></select>
<input type=checkbox>Sort</input>
</div>
<div id='chart'></div>
<div id = 'share'>
SHARE: <a href="https://www.facebook.com/sharer/sharer.php?u=http://mahdisadjadi.com/phoenixcrime/" target="_blank">
<i class="fa fa-facebook-square fa-2x" aria-hidden="true"></i>
</a>
<a href="https://twitter.com/share?url=http://mahdisadjadi.com/phoenixcrime/&text=Weekly%20Crime%20Trend%20in%20Phoenix&hashtags=phoenix,arizona,crime,datascience,dataanalysis,d3" target="_blank">
<i class="fa fa-twitter-square fa-2x" aria-hidden="true"></i></a> /
<a href="https://github.com/Mahdisadjadi/phoenixcrime">Github Repo</a> /
<a href="https://github.com/Mahdisadjadi/phoenixcrime/blob/master/LICENSE" target="_blank">MIT License</a> /
<a href="http://mahdisadjadi.com" target="_blank">Mahdi Sadjadi</a> /
2018.
</div>
</div>
<div>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<script>
/*
Almost an exact replica of:
http://www.alexrothenberg.com/2014/01/06/learning-d3-by-building-a-chart.js.html
Tooltip from here:
http://bl.ocks.org/Caged/6476579
*/
// add all types of crime to select menu
crimeNames = ['ARSON','ASSAULT','BURGLARY','DRUG_OFFENSE',
'LARCENY_THEFT','MURDER','RAPE','ROBBERY','VEHICLE_THEFT'];
var list = document.getElementById("select_crime");
for(var i in crimeNames) {
name = crimeNames[i];
//name = crimeNames[i].toLowerCase();
//name = name[0].toUpperCase() + name.slice(1);
list.add(new Option(name));
};
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "Count:<span style='color:red'>"+d.count+"</span>";
});
var width = 500,
height = 200;
var svg = d3.select('body').select('#chart').append('svg')
.attr('height', height)
.attr('width', width)
.attr("align","center");
var g = svg.append('g')
.attr("transform", "translate(40, 20)");
svg.call(tip);
g.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0, 150)');
g.append('g')
.attr('class', 'y axis');
var parseRow = function(row) {
return {
weekday: row.weekday,
count: parseInt(row.count)
}
};
var dayIndex = function(dayName) {
var days = {
Monday:1, Tuesday:2, Wednesday:3,
Thursday:4, Friday:5, Saturday:6,
Sunday:7};
return days[dayName];
};
var loadData = function() {
var crime_type = document.getElementById('select_crime').selectedOptions[0].text;
var dataFile = 'crime_weekly/' + crime_type + '.csv';
console.log(dataFile)
d3.csv(dataFile, parseRow, function(data) {
var days = data.map(function(d) { return d.weekday });
var x = d3.scale.ordinal()
.rangeRoundBands([0, 400], .1)
.domain(days);
var counts = data.map(function(d) { return d.count });
var y = d3.scale.linear()
.range([150, 0])
.domain([0, d3.max(counts)]);
var rect = g.selectAll('.bar').data(data);
rect.enter().append('rect');
rect.exit().remove();
rect.attr('class', 'bar')
.attr('width', x.rangeBand())
.attr('height', function(d) { return 150 - y(d.count)})
.attr('x', function(d) { return x(d.weekday) })
.attr('y', function(d) { return y(d.count) })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
/*.on("mouseover", function(d) {
d3.select(this).attr("r", 10).style("fill", "red");
})
.on("mouseout", function(d) {
d3.select(this).attr("r", 10).style("fill", "steelblue");
});*/
var xAxis = d3.svg.axis()
.scale(x)
.orient('bottom');
var yAxis = d3.svg.axis()
.scale(y)
.orient('left')
.tickFormat(d3.format('.0'));
d3.select('.x.axis').call(xAxis);
d3.select('.y.axis').call(yAxis);
d3.select('input').on('change', function() {
var sortByCount = function(a, b) { return b.count - a.count; };
var sortByDay = function(a, b) { return d3.ascending(dayIndex(a.weekday), dayIndex(b.weekday)); };
var sortedDays = data.sort(this.checked ? sortByCount : sortByDay)
.map(function(d) { return d.weekday; })
x.domain(sortedDays)
var transition = svg.transition().duration(750);
var delay = function(d, i) { return i * 50; };
transition.selectAll(".bar")
.delay(delay)
.attr("x", function(d) { return x(d.weekday); });
transition.select(".x.axis")
.call(xAxis)
.selectAll("g")
.delay(delay);
})
})
}
loadData()
</script>
</body>