xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Loading CSV Data with D3</title>
<link href='https://fonts.googleapis.com/css?family=Roboto+Slab:400,700,300' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:400,700' rel='stylesheet' type='text/css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: 'Yanone Kaffeesatz',Helvetica, sans-serif;
}
h1 {
font-size: 36px;
margin: 0;
font-weight: bold;
text-transform: uppercase;
padding-left: 20px;
padding-bottom: 2px;
border-top: solid;
border-top-width: 3px;
border-top-color: #000000;
padding-top: 5px;
}
p {
font-size: 14px;
margin: 10px 0 0 0;
}
.chatterClass{
font-size: 20px;
line-height: 22px;
padding-left: 20px;
padding-right: 20px;
}
.sourceClass{
font-size: 14px;
line-height: 16px;
padding-left: 20px;
padding-right: 20px;
}
svg {
background-color: white;
width: 100%;
}
rect:hover {
fill: orange;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: 'Roboto Slab', Times, serif;
font-size: 9px;
}
.y.axis path,
.y.axis line {
opacity: 0;
</style>
</head>
<body>
<div id="myIntroText">
<h1>Living in poverty</h1>
<p class="chatterClass">In two Chicago communities over 50% of its residents live below the poverty line. City-wide the figure is about 19%.</p>
</div>
<script type="text/javascript">
var myWindow=$( window ).width();
// console.log(myWindow);
var w = myWindow;
var h = 1000;
var padding = [ 20, 20, 30, 120 ]; //Top, right, bottom, left
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);
//Load in contents of CSV file
d3.csv("ChicagoSocEconIndicators2008_12.csv", function(data) {
data.sort(function(a, b) {
//return d3.descending(a.PercentHouseholdsBelowPov, b.PercentHouseholdsBelowPov);
return d3.descending(+a.PercentHouseholdsBelowPov, +b.PercentHouseholdsBelowPov)
});
widthScale.domain([ 0, d3.max(data, function(d) {
return +d.PercentHouseholdsBelowPov;
}) ]);
heightScale.domain(data.map(function(d) { return d.community; } ));
var rects = svg.selectAll("rect")
.data(data)
.enter()
.append("rect");
rects.attr("x", padding[3])
.attr("y", function(d) {
return heightScale(d.community);
})
.attr("width", function(d) {
return widthScale(d.PercentHouseholdsBelowPov);
})
.attr("height", heightScale.rangeBand())
.attr("fill", "#43B649")
.append("title")
.text(function(d) {
return d.community + " has " + d.PercentHouseholdsBelowPov + " below the poverty line ";
});
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);
});
</script>
<p class="sourceClass">Source:City of Chicago, data.gov</p>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
https://d3js.org/d3.v3.js