Created
August 3, 2015 05:56
-
-
Save tuttinator/87f504ee25460b7f297b to your computer and use it in GitHub Desktop.
Convictions stacked area graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
end_date | Community Detention | Community work, Corrections | Conviction and discharge | Disqualification from driving | Fine | Home Detention | Imprisonment | Intensive Supervision | Life imprisonment | Other | Reparation/Restitution | Supervision by Community Corrections | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2003-06-30 | 1410 | 131 | 1990 | 870 | 1 | 108 | 2 | 65 | |||||
2004-06-30 | 1224 | 132 | 2 | 1668 | 862 | 95 | 2 | 65 | |||||
2005-06-30 | 2 | 1128 | 159 | 2 | 1620 | 1 | 755 | 115 | 2 | 66 | |||
2006-06-30 | 4 | 1167 | 166 | 1544 | 2 | 750 | 140 | 2 | 70 | ||||
2007-06-30 | 56 | 1205 | 157 | 1434 | 156 | 583 | 38 | 154 | 4 | 88 | |||
2008-06-30 | 142 | 1395 | 180 | 1516 | 314 | 560 | 67 | 3 | 156 | 8 | 139 | ||
2009-06-30 | 278 | 1617 | 165 | 1413 | 429 | 669 | 69 | 160 | 4 | 168 | |||
2010-06-30 | 285 | 1346 | 154 | 1 | 1084 | 442 | 667 | 67 | 172 | 3 | 197 | ||
2011-06-30 | 276 | 1210 | 138 | 928 | 390 | 561 | 66 | 133 | 3 | 175 | |||
2012-06-30 | 301 | 1122 | 109 | 768 | 457 | 569 | 71 | 98 | 2 | 185 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.conviction text { | |
/* text-anchor: end; */ | |
} | |
svg { | |
margin: auto; | |
} | |
</style> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> | |
<script> | |
var margin = { top: 20, right: 220, bottom: 30, left: 50 }, | |
width = 1260 - margin.left - margin.right, | |
height = 900 - margin.top - margin.bottom; | |
var parseDate = d3.time.format("%Y-%m-%d").parse, | |
formatPercent = d3.format(".0%"); | |
var x = d3.time.scale() | |
.range([0, width]); | |
var y = d3.scale.linear() | |
.range([height, 0]); | |
var color = d3.scale.category20(); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
.tickFormat(formatPercent); | |
var area = d3.svg.area() | |
.x(function(d) { return x(d.end_date); }) | |
.y0(function(d) { return y(d.y0); }) | |
.y1(function(d) { return y(d.y0 + d.y); }); | |
var stack = d3.layout.stack() | |
.values(function(d) { return d.values; }); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
var calculatePercentage = function(d, name) { | |
if(d[name] === '') { | |
return 0; | |
} | |
var keys = d3.keys(d).filter(function(key) { return key !== "end_date"; }) | |
total = 0; | |
for(key of keys) { | |
if(d[key] != '') { | |
total += Number(d[key]); | |
} | |
} | |
return Number(d[name]) / total; | |
} | |
d3.csv("drug_sentences.csv", function(error, data) { | |
if (error) { throw error; } | |
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "end_date"; })); | |
data.forEach(function(d) { | |
d.end_date = parseDate(d.end_date); | |
}); | |
var conviction = stack(color.domain().map(function(name) { | |
return { | |
name: name, | |
values: data.map(function(d) { | |
var val = calculatePercentage(d, name); | |
return { end_date: d.end_date, y: val }; | |
}) | |
}; | |
})); | |
x.domain(d3.extent(data, function(d) { return d.end_date; })); | |
var conviction = svg.selectAll(".conviction") | |
.data(conviction) | |
.enter().append("g") | |
.attr("class", "conviction"); | |
conviction.append("path") | |
.attr("class", "area") | |
.attr("d", function(d) { return area(d.values); }) | |
.style("fill", function(d) { return color(d.name); }); | |
conviction.append("text") | |
.datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; }) | |
.attr("transform", function(d) { return "translate(" + x(d.value.end_date) + "," + y(d.value.y0 + d.value.y / 2) + ")"; }) | |
.attr("x", 10 ) | |
.attr("dy", ".35em") | |
.text(function(d) { return d.name; }); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment