xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Module 5 Exercise -- Life Expectancy for all the countries.</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Helvetica, Arial, sans-serif;
}
h1 {
font-size: 24px;
margin: 0;
}
p {
font-size: 14px;
margin: 10px 0 0 0;
}
svg {
background-color: white;
}
circle:hover {
fill: red;
}
.axis path,
.axis line {
fill: none;
stroke: blue;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<h1>Life expectancy at birth in years</h1>
<p>Life expectancy at birth indicates the number of years a newborn infant would live if prevailing patterns of mortality at the time of its birth were to stay the same throughout its life.</p>
<p>Source: <a href="https://data.worldbank.org/indicator/SP.DYN.LE00.IN">World Bank</a>, 2014</p>
<div id="chart1960" style="float: left"></div>
<div id="chart2012" style="float: left"></div>
<script type="text/javascript">
var w = 600;
var h = 600;
var padding = [ 50, 10, 50, 50 ]; //Top, right, bottom, left
var xScale = d3.scale.linear()
.range([ padding[3], w - padding[1] - padding[3] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.tickFormat(function(d) {
return d;
});
function setupSVG(divelem, year) {
var svg = d3.select(divelem)
.append("svg")
.attr("width", w)
.attr("height", h);
svg.append("text")
.attr("x", (w / 2))
.attr("y", 25)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Life expectancy at birth in " + year);
return svg;
}
function setupChart(svg, data, year, yearForSorting) {
data.sort(function(a, b) {
return d3.descending(+a[yearForSorting], +b[yearForSorting]);
});
yScale.domain([ 90, 20
/*d3.max(data, function(d) {
return +d[year];
}),
d3.min(data, function(d) {
return +d[year];
})*/
]);
xScale.domain([data.length, 0]);
var circles = svg.selectAll("circle")
.data(data)
.enter()
.append("circle");
circles.attr("cx", function(d, i) {
return xScale(i);
})
.attr("cy", function(d) {
return yScale(d[year]);
})
.attr("r", 0.1)
.attr("fill", "orange")
.append("title")
.text(function(d) {
return d.CountryName + "'s life expectancy at birth is " + d[year];
});
circles.sort(function(a, b) {
return d3.ascending(+a[year], +b[year]);
})
.transition()
.ease('quad')
.delay(function(d, i) {
return i * 50;
})
.duration(1000)
.attr("r", 3);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding[3] - 10) + ",0)")
.call(yAxis);
}
var svg = setupSVG("#chart1960", "1960");
var svg2012 = setupSVG("#chart2012", "2012");
d3.csv("LifeExpectancyAtBirth.csv", function(data) {
console.table(data, ["CountryName", "1960", "2012"]);
setupChart(svg, data, "1960", "2012");
setupChart(svg2012, data, "2012", "1960");
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js