xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Canadian population by immigrant status</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Georgia, serif;
}
h1 {
font-size: 24px;
margin: 0px 0 0px 0;
}
p {
font-size: 14px;
margin: 10px 0 0px 0;
}
svg {
background-color: white;
}
rect:hover {
fill: #cc0000;
}
.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: 1;
}
</style>
</head>
<body>
<h1>Immigrant vs non immigrant population in Canada</h1>
<p>2006 counts, for Canada, provinces and territories. <br>
Source: <a href="https://www12.statcan.ca/census-recensement/2006/dp-pd/hlt/97-557/T403-eng.cfm?Lang=E&T=403&GH=4&SC=1&S=99&O=A">Statistics Canada</a>, 2006</p>
<script type="text/javascript">
//Dimensions and padding
var w = 650;
var h = 500;
var padding = [ 50, 10, 50, 70 ]; //Top, right, bottom, left
//Set up scales
var xScale = d3.scale.linear()
.range([ padding[3], w - padding[1] - padding[3] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
//Set up formatter
var commasFormatter = d3.format(",.0f")
//Configure axis generators
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(7)
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(7)
.tickFormat(function(d) {
return commasFormatter(d);
});
//Create the empty SVG image
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//data
d3.csv("population-immigrant-status-and-period_2006_canada.csv", function(data) {
xScale.domain([
d3.min(data, function(d) {
return +d.NonimmigrantPopulation;
}),
d3.max(data, function(d) {
return +d.NonimmigrantPopulation;
})
]);
yScale.domain([
d3.max(data, function(d) {
return +d.NonimmigrantPopulation;
}),
d3.min(data, function(d) {
return +d.NonimmigrantPopulation;
})
]);
var circles = svg.selectAll("circle")
.data(data)
.enter()
.append("circle");
circles.attr("cx", function(d) {
return xScale(d.ImmigrantPopulation);
})
.attr("cy", function(d) {
return yScale(d.NonimmigrantPopulation);
})
.attr("r", 0.1)
.attr("fill", "#E67D23")
.append("title")
.text(function(d) {
return d.GeographicName + " non immigrant population is " + d.NonimmigrantPopulation + " and " + d.ImmigrantPopulation + " is immigrant";
});
circles.sort(function(a, b) {
return d3.ascending(+a.ImmigrantPopulation, +b.ImmigrantPopulation);
})
.transition()
.delay(function(d, i) {
return i * 50;
})
.duration(500)
.attr("r", 6);
//Axes
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding[3]) + ",0)")
.call(yAxis);
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js