xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Excercise module 4</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body{
margin-left : 4em;
margin-right : 4em;
}
svg {
background-color: white;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: Arial, sans-serif;
font-size: 12px;
}
</style>
</head>
<body>
<h2>Cash for Care recipients</h2>
<p>
The largest number of recipients are in the age group 30 - 34 for both sexes.
</p>
<h3>Number of cash care recipients in 2014 by age group</h3>
<script type="text/javascript">
function drawYear(data,year){
var width = 500;
var height = 300;
var svg = d3.select("body").append("svg");
svg.attr("width",width).attr("height",height)
var padding = {
top: 5,
right: 10,
bottom: 20,
left: 150
};
// Sort the data
//data.sort(function(a, b) {
// return d3.ascending(parseInt(a["2005"]),parseInt(b["2005"]));
//});
var widthScale = d3.scale.linear()
.range([ 0, width - padding.right - padding.left ])
.domain([0, d3.max(data, function(d) {
return +d[year];
})]);
var heightScale = d3.scale.ordinal()
.rangeRoundBands([ padding.top, height - padding.bottom],0.1)
.domain(data.map(function(d) { return d["Category"]; } ));
var xAxis = d3.svg.axis()
.scale(widthScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(heightScale)
.orient("left");
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", padding.left )
.attr("y", function(d,i){
return heightScale(d["Category"]);
})
.attr("width",function(d){
return widthScale(+d[year]);
})
.attr("fill", function(d){
// Different colors for each age group
if(d["Category"].indexOf("34") > -1) return "#feb24c";
else return "#fd8d3c";
})
.attr("height",heightScale.rangeBand())
.append("title")
.text(function(d){
return +d[year];
});
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + padding.left + "," + (height - padding.bottom + ")"))
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding.left - 5) + ",0)")
.call(yAxis);
}
//Load in contents of CSV file
d3.csv("Number of Cash for Care recipients by year and sex2.csv", function(data) {
drawYear(data,"2014");
})
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js