Skip to content

Instantly share code, notes, and snippets.

@anaeliaovalle
Created February 18, 2017 00:25
Show Gist options
  • Save anaeliaovalle/66c3eba07c6d2c8f6b44fda14771e16e to your computer and use it in GitHub Desktop.
Save anaeliaovalle/66c3eba07c6d2c8f6b44fda14771e16e to your computer and use it in GitHub Desktop.
bar chart v3
license: mit
category count
suicide 10
liquor_laws 14
arson 22
kidnapping 22
financial_crime 25
sex_offenses 44
prostitution 49
disorderly_cond 84
weapon_laws 115
trespass 158
fraud 216
robbery 296
drug_narcotic 301
missing_person 306
warrants 422
suspicious_occ 506
burglary 517
vehicle_related 642
vandalism 822
assault 1129
non_criminal 1446
other_offenses 1654
larceny_theft 3818
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<style>
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 54, right: 60, bottom: 107, left: 88},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear().range([0, width]);
var y = d3.scale.ordinal().rangeRoundBands([0, height]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(10);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
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 + ")");
svg.append("text")
.attr("x", 0)
.attr("y", -35)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("SFPD Incidents in Dec 2016")
.style("font", "23px avenir")
.style("fill", "#000000");
svg.append("text")
.attr("x", -69)
.attr("y", -8)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("Category")
.style("font", "12px avenir")
.style("fill", "#000000")
.style("font-weight", "bold");
svg.append("text")
.attr("x", 0)
.attr("y", 400)
.attr("dy", "0em")
.style("font", "12px avenir")
.style("fill", "#000000")
.text("This visualization depicts the number of SFPD incidents that occurred in December 2016. Larceny was the most common indicident while liquor law violations ");
svg.append("text")
.attr("x", 0)
.attr("y", 400)
.attr("dy", "1em")
.style("font", "12px avenir")
.style("fill", "#000000")
.text("and suicide were observed less frequently.");
svg.append("text")
.attr("x", 0)
.attr("y", 400)
.attr("dy", "3em")
.style("font", "12px avenir")
.style("fill", "#000000")
.text("By Anaelia Ovalle")
.style("font-weight", "bold");
d3.csv("bar-data.csv", function(error, data) {
data.forEach(function(d) {
d.count = +d.count;
return d;
}, function(error, data) {
if (error) throw error;
});
data.sort(function(a,b){ return b.count- a.count});
x.domain([0, 4000]);
y.domain(data.map(function(d) {return d.category; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
// .text("Value ($)");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.style("fill", "steelblue")
.attr("y", function(d) { return y(d.category); })
.attr("width", function(d) { return x(d.count);} )
.attr("height", 13)
svg.append("text")
.attr("x", 350)
.attr("y", 370)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("Number of Records")
.style("font", "12px avenir")
.style("fill", "#000000")
.style("font-weight", "bold");
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment