|
<!DOCTYPE html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="utf-8"> |
|
<title>Jersey City . Public Housing</title> |
|
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script> |
|
<style type="text/css"> |
|
|
|
body { |
|
font-family: Helvetica, Arial, sans-serif; |
|
background-color: white; |
|
} |
|
svg { |
|
background-color: white; |
|
} |
|
h1 { |
|
font-size: 24px; |
|
margin: 0; |
|
} |
|
p { |
|
font-size: 14px; |
|
margin: 10px 0 0 0; |
|
} |
|
.Barchart rect:hover { |
|
fill: orange !important; |
|
fill-opacity: 1; |
|
} |
|
.axis path, |
|
.axis line { |
|
fill: none; |
|
stroke: black; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.axis text { |
|
font-family: sans-serif; |
|
font-size: 11px; |
|
} |
|
.y.axis path, |
|
.y.axis line { |
|
opacity: 0; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
|
|
<h1>Vacancy Rates by Property</h1> |
|
|
|
<p>Monthly average vacancy rates Jan 2014-May 2015. Source: <a href="http://www.jerseycitynj.gov/data.aspx?id=14832">Jersey City Housing Authority</a>.</p> |
|
|
|
<p> |
|
<button id="vacancy">Vacancy Rate</button> |
|
<button id="vacantUnits">Vacant Units</button> |
|
<button id="totalUnits">Total Units</button> |
|
</p> |
|
|
|
<script type="text/javascript"> |
|
var rects; |
|
var dataset; |
|
var padding = [ 20, 10, 30, 170 ]; //Top, right, bottom, left |
|
var percent = d3.format(".0%"); |
|
|
|
var color = d3.scale.ordinal() |
|
var w = 700; |
|
var h = 380; |
|
var widthScale = d3.scale.linear() |
|
.range([ 0, w - padding[1] - padding[3] ]); |
|
|
|
var heightScale = d3.scale.ordinal() |
|
.rangeRoundBands([ padding[0], h - padding[2] ], 0.1); |
|
var xAxis = d3.svg.axis() |
|
.scale(widthScale) |
|
.orient("bottom"); |
|
var yAxis = d3.svg.axis() |
|
.scale(heightScale) |
|
.orient("left"); |
|
var svg = d3.select("body") |
|
.append("svg") |
|
.attr("width", w ) |
|
.attr("height", h ) |
|
.attr("class","Barchart"); |
|
d3.csv("PAvg.csv", function(data) { |
|
data.sort(function(a, b) { |
|
return d3.descending(+a.vacancyRate, +b.vacancyRate); |
|
}); |
|
dataset = data; |
|
widthScale.domain([ 0, d3.max(data, function(d) { |
|
return +d.vacancyRate; |
|
}) ]); |
|
heightScale.domain(data.map(function(d) { return d.propertyName; } )); |
|
rects = svg.selectAll("rect") |
|
.data(data) |
|
.enter() |
|
.append("rect"); |
|
rects.attr("x", padding[3]) |
|
.attr("y", function(d) { |
|
return heightScale(d.propertyName); |
|
}) |
|
.attr("width", function(d) { |
|
return widthScale(d.vacancyRate); |
|
}) |
|
.attr("height", heightScale.rangeBand()) |
|
.attr("fill","steelblue") |
|
.append("title") |
|
.text(function(d) { |
|
return d.propertyName + "'s vacancy rate is " + percent(+d.vacancyRate); |
|
}); |
|
svg.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") |
|
.call(xAxis); |
|
svg.append("g") |
|
.attr("class", "y axis") |
|
.attr("transform", "translate(" + padding[3] + ",0)") |
|
.call(yAxis); |
|
}); |
|
d3.select("#totalUnits") |
|
.on("click", function() { |
|
//Update scale domain |
|
widthScale.domain([ 0, d3.max(dataset, function(d) { |
|
return +d.totalUnits; |
|
}) ]); |
|
//Update rect widths |
|
rects.transition() |
|
.duration(1000) |
|
.attr("width", function(d) { |
|
return widthScale(d.totalUnits); |
|
}) |
|
.select("title") |
|
.text(function(d) { |
|
return d.propertyName + " has " + d.totalUnits + " units"; |
|
}); |
|
//Update axis |
|
d3.select(".x.axis") |
|
.transition() |
|
.duration(1000) |
|
.call(xAxis); |
|
}); |
|
d3.select("#vacancy") |
|
.on("click", function() { |
|
//Update scale domain |
|
widthScale.domain([ 0, d3.max(dataset, function(d) { |
|
return +d.vacancyRate; |
|
}) ]); |
|
//Update rect widths |
|
rects.transition() |
|
.duration(1000) |
|
.attr("width", function(d) { |
|
return widthScale(d.vacancyRate); |
|
}) |
|
.select("title") |
|
.text(function(d) { |
|
return d.propertyName + "'s vacancy rate is " + d.vacancyRate * 100.00 + "%"; |
|
}); |
|
//Update axis |
|
d3.select(".x.axis") |
|
.transition() |
|
.duration(1000) |
|
.call(xAxis); |
|
}); |
|
d3.select("#vacantUnits") |
|
.on("click", function() { |
|
//Update scale domain |
|
widthScale.domain([ 0, d3.max(dataset, function(d) { |
|
return +d.vacantUnits; |
|
}) ]); |
|
//Update rect widths |
|
rects.transition() |
|
.duration(1000) |
|
.attr("width", function(d) { |
|
return widthScale( +d.vacantUnits ); |
|
}) |
|
.select("title") |
|
.text(function(d) { |
|
return d.propertyName + " has " + d.vacantUnits + " vacant units"; |
|
}); |
|
//Update axis |
|
d3.select(".x.axis") |
|
.transition() |
|
.duration(1000) |
|
.call(xAxis); |
|
}); |
|
</script> |
|
|
|
</body> |
|
</html> |