Last active
September 20, 2015 01:12
-
-
Save jalapic/5aeb42278a089a036d3c to your computer and use it in GitHub Desktop.
Horizontal Bar with scale and axes
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> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
<title>horizontal bar</title> | |
<style type="text/css"> | |
h1{ | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 26px; | |
font-style: bold; | |
} | |
body { | |
background-color: #F9FFFF; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 16px; | |
font-style: bold; | |
} | |
svg { | |
background-color: #F9FFFF; | |
} | |
.myrect { | |
fill: #191970; | |
} | |
.myrect:hover { | |
fill: #af1e23; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: black; | |
shape-rendering: crispEdges; | |
} | |
.axis text { | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-style: bold; | |
font-size: 11px; | |
} | |
.y.axis path, | |
.y.axis line { | |
opacity: 0; | |
} | |
</style> | |
<h1>Stop and Frisk Totals by Race for NYC in 2014 </h1> | |
</head> | |
<body> | |
<p>Original data available at <a href="http://www.nyc.gov/html/nypd/html/analysis_and_planning/stop_question_and_frisk_report.shtml">NYC.gov</a>.</p> | |
<script type="text/javascript"> | |
var w = 800; | |
var h = 200; | |
var padding = [ 20, 20, 30, 210 ]; //Top, right, bottom, left | |
// scale x data linearly | |
var widthScale = d3.scale.linear() | |
.range([ 0, w - padding[1] - padding[3] ]); | |
// scale y data ordinally | |
var heightScale = d3.scale.ordinal() | |
.rangeRoundBands([ padding[0], h - padding[2] ], 0.1); | |
// add x-axis | |
var xAxis = d3.svg.axis() | |
.scale(widthScale) | |
.orient("bottom") | |
// .tickFormat(d3.format("d")); //remove comma | |
; | |
// add y-axis | |
var yAxis = d3.svg.axis() | |
.scale(heightScale) | |
.orient("left"); | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
d3.csv("stopfrisksum.txt", function(data) { | |
data.sort(function(a, b) { | |
return d3.descending(+a.total, +b.total); | |
}); | |
// inform domain to widthScale - x data | |
widthScale.domain([ 0, d3.max(data, function(d) { | |
return +d.total; | |
}) ]) | |
.nice(); | |
// inform domain to heightScale - y axis | |
heightScale.domain(data.map(function(d) { return d.race; } )); | |
var rects = svg.selectAll("rect") | |
.data(data) | |
.enter() | |
.append("rect"); | |
rects.attr("class", "myrect") | |
.attr("x", padding[3]) | |
.attr("y", function(d) { | |
return heightScale(d.race); | |
}) | |
.attr("width", function(d) { | |
return widthScale(d.total); | |
}) | |
.attr("height", heightScale.rangeBand()) | |
//.attr("height", 15) | |
.append("title") | |
.text(function(d) { | |
return d.race + " individuals were stopped and frisked " + d.total + " times in NYC in 2014"; | |
}); | |
// add x-axis | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") | |
.call(xAxis); | |
// add y-axis | |
svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + padding[3] + ",0)") | |
.call(yAxis); | |
}); | |
</script> | |
</body> | |
</html> |
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
"race","Bronx","Brooklyn","Manhattan","Queens","Staten_Island","total" | |
"AMERICAN INDIAN/ALASKAN NATIVE",16,23,14,115,17,185 | |
"ASIAN/PACIFIC ISLANDER",39,332,138,1594,70,2173 | |
"BLACK",3182,8696,3434,5688,2468,23468 | |
"BLACK-HISPANIC",839,547,661,478,158,2683 | |
"OTHER",143,160,239,107,57,706 | |
"WHITE",199,1243,711,1715,1259,5127 | |
"WHITE-HISPANIC",2115,1978,1563,2919,735,9310 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment