Skip to content

Instantly share code, notes, and snippets.

@Paperpanic
Created April 26, 2015 02:36
Show Gist options
  • Save Paperpanic/790ab6aded84b5ec8009 to your computer and use it in GitHub Desktop.
Save Paperpanic/790ab6aded84b5ec8009 to your computer and use it in GitHub Desktop.
Module 6 exercise
Felonies Category 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
MURDER&NON-NEGL.MANSLAUGHTER TotalCrimes 673 649 587 597 570 539 596 496 523 471 536 515
RAPE TotalCrimes 2068 1981 2144 2070 1905 1858 1525 1351 1299 1205 1373 1420
ROBBERY TotalCrimes 32562 28202 27229 25989 24373 24722 23739 21809 22401 18601 19486 19717
FELONYASSAULT TotalCrimes 25924 23453 21147 19139 18622 17750 17309 17493 16284 16773 16956 18482
BURGLARY TotalCrimes 38352 32763 31275 29110 26976 24117 23143 21762 20725 19430 18600 18720
GRANDLARCENY TotalCrimes 49631 46329 45771 46751 48763 48243 46625 44924 44242 39580 37835 38501
GRANDLARCENYOFMOTORVEHICLE TotalCrimes 35442 29531 26656 23413 20884 18246 15745 13174 12482 10670 10329 9314
FELONYPOSSESSIONOFSTOLENPROPERTY TotalCrimes 21945 2114 19765 1545 13581 12501 1175 1073 1016 823 864 823
FORGERY/THEFT_FRAUD/IDENTITYTHEFT TotalCrimes 31891 5594 1902 8112 1789 1831 12579 12075 12216 10225 10055 9876
ARSON TotalCrimes 38088 2681 1513 1492 1263 1162 1679 1527 1896 1474 1467 1264
FELONYSEXCRIMES(3) TotalCrimes 2958 1831 29692 1295 25824 27391 1096 1031 909 914 1053 1028
FELONYDANGEROUSDRUGS(1) TotalCrimes 12934 30743 2804 24076 2987 3748 29516 31459 28936 26025 22913 21305
FELONYDANGEROUSWEAPONS(2) TotalCrimes 13945 3030 13291 2511 8074 7776 4315 5004 5263 5952 5413 5037
FEL.CRIMINALMISCHIEF&RELATEDOFFENSES TotalCrimes 26551 16026 13956 8400 12454 11800 7605 7090 7003 6397 6006 6374
OTHERFELONIES(4) TotalCrimes 32397 13059 22580 13786 20882 20332 11063 11699 11719 11950 11616 11533
TOTALFELONYOFFENSES TotalCrimes 265513 237986 28970 208286 26676 25610 197710 191967 186914 170490 268753 269755
TOTALARRESTS TotalCrimes 32397 28878 28970 25676 26676 25610 25455 26188 25128 23989 22050 20184
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Module 6 exercise</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<style type="text/css">
path:hover {
stroke: white;
stroke-width: 3px;
}
body {
background-color: white;
}
svg {
background-color: white;
background-image: url("http://orig09.deviantart.net/8e3d/f/2015/115/b/2/image_by_paperpanic-d8r2eba.jpg");
opacity: 0.50;
}
h1 {
font-size: 29px;
margin: 0;
font-family: arial;
}
p {
font-family: arial;
font-size: 20px;
margin: 10px 0 0 0;
}
path {
stroke: blue;
stroke-width: 1;
}
g.highlight path {
stroke: yellow;
stroke-width: 4;
}
.axis path,
.axis line {
fill: none;
stroke: black;
stroke-width: 1;
shape-rendering: crispEdges;
}
.axis text {
font-family: arial;
font-size: 10px;
color: Gainsboro
}
</style>
</head>
<body>
<h1>TOTAL FELONIES ON NEW YORK FROM 2000 TO 2011</h1>
<p>Major an Minor crimes Index &ldquo;Total Felonies&rdquo; by year. Source: <a href="https://data.cityofnewyork.us/Public-Safety/Historical-New-York-City-Crime-Data/hqhv-9zeg">NYC open Data</a>, 2011</p>
<script type="text/javascript">
var w = 800;
var h = 600;
var padding = [ 20, 10, 50, 100 ];
var dateFormat = d3.time.format("%Y");
var xScale = d3.time.scale()
.range([ padding[3], w - padding[1] - padding[3] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(10)
.tickFormat(function(d) {
return dateFormat(d);
});
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
var line = d3.svg.line()
.x(function(d) {
return xScale(dateFormat.parse(d.year));
})
.y(function(d) {
return yScale(+d.amount);
});
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("FelonyOffensesVSArrests-2000-2011.csv", function(data) {
var years = ["2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011"];
var dataset = [];
for (var i = 0; i < data.length; i++) {
dataset[i] = {
country: data[i].Felonies,
TotalCrimes: []
};
for (var j = 0; j < years.length; j++) {
if (data[i][years[j]]) {
dataset[i].TotalCrimes.push({
year: years[j],
amount: data[i][years[j]]
});
}
}
}
xScale.domain([
d3.min(years, function(d) {
return dateFormat.parse(d);
}),
d3.max(years, function(d) {
return dateFormat.parse(d);
})
]);
yScale.domain([
d3.max(dataset, function(d) {
return d3.max(d.TotalCrimes, function(d) {
return +d.amount;
});
}),
0
]);
var groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.classed("highlight", function(d) {
if (d.country == "TOTALFELONYOFFENSES" ||
d.country == "TOTALARRESTS") {
return true;
} else {
return false;
}
});
groups.append("title")
.text(function(d) {
return d.country;
});
groups.selectAll("path")
.data(function(d) {
return [ d.TotalCrimes ];
})
.enter()
.append("path")
.attr("class", "line")
.attr("d", line)
.attr("fill", "none")
.attr("stroke-width", 2);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding[3]) + ",0)")
.call(yAxis);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment