xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Income per capita vs. Life expectancy</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: #FFF;
font-family: HelveticaNeue;
}
h1 {
font-size: 24px;
color: #0D5047;
margin: 0;
}
svg {
background-color: #FFF;
}
circle:hover {
fill: #0D5047;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-size: 9px;
}
.x.label,
.y.label {
font-size: 14px;
}
</style>
</head>
<body>
<h1>Income per capita vs. Life expectancy</h1>
<script type="text/javascript">
var w = 700;
var h = 600;
var padding = [20,10,50,50];
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 xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(10);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(10);
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("WHO_data.csv", function(data) {
data.sort(function(a, b) {
return d3.descending(+a.incomePerCapita, +b.incomePerCapita);
});
xScale.domain([
d3.min(data, function(d) {
return +d.incomePerCapita;
}),
d3.max(data, function(d) {
return +d.incomePerCapita;
})
]);
yScale.domain([
d3.max(data, function(d) {
return +d.lifeExpectancy;
}),
d3.min(data, function(d) {
return +d.lifeExpectancy;
})
]);
var circles = svg.selectAll("circle")
.data(data)
.enter()
.append("circle");
circles.attr("cx", function(d) {
return xScale(d.incomePerCapita);
})
.attr("cy", function(d) {
return yScale(d.lifeExpectancy);
})
.attr("r", 0.1)
.attr("fill", "#6BA099")
.append("title")
.text(function(d) {
return d.country + "'s income per capita is " + d.incomePerCapita + ", and life expectancy is " + d.lifeExpectancy + " years";
});
circles.sort(function(a, b) {
return d3.ascending(+a.incomePerCapita, +b.incomePerCapita);
})
.transition()
.delay(function(d, i) {
return i * 5;
})
.duration(500)
.attr("r", 4);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2] + 10) + ")")
.call(xAxis);
svg.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", w - 50)
.attr("y", h - 6)
.text("Income per capita");
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding[3] - 10) + ",0)")
.call(yAxis);
svg.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", 3)
.attr("x", -20)
.attr("dy", ".75em")
.attr("transform", "rotate(-90)")
.text("Life expectancy");
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js