xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>USA 2010 Population by State - bar chart</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.min.js"></script>
<style type="text/css">
body {
background-color: #ddddff;
}
svg {
background-color: white;
}
.chart rect {
fill: steelblue;
}
</style>
</head>
<body>
<script type="text/javascript">
var svgWidth = 400;
var svgHeight = 600;
var svg = d3.select("body")
.append("svg")
.attr("width", svgWidth)
.attr("height", svgHeight)
.attr("class","chart");
var thousandsFormat = d3.format("0,000");
d3.csv("usa_2010_population_by_state.csv", function(data) {
var totalPopulation = d3.sum(data, function(d) { return +d.census2010Population; });
// use max population to calculate the width of the bars
var maxPopulation = d3.max(data, function(d) { return +d.census2010Population; });
console.log("totalPopulation: " + totalPopulation);
console.log("maxPopulation: " + maxPopulation);
console.log("# of rows: " + data.length);
data.sort(function(a, b) {
return d3.descending(+a.census2010Population, +b.census2010Population);
});
var rects = svg.selectAll("rect")
.data(data)
.enter()
.append("rect");
rects.attr("x", 0)
.attr("y", function(d, i) {
return i * svgHeight / data.length;
})
.attr("width", function(d) {
return d.census2010Population / maxPopulation * svgWidth;
})
.attr("height", svgHeight / data.length * .8)
.append("title")
.text(function(d, i) {
return i + 1 + ") " + d.state + ": " + thousandsFormat(d.census2010Population);
});
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.min.js to a secure url
https://d3js.org/d3.v3.min.js