xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Module 3 exercice</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: #EBEFEB;
}
h {
font-size: 30px;
font-family: arial;
margin: 2;
}
p {
font-size: 14px;
font-family: arial;
margin: 15px 0 0 0;
}
svg {
background-color: #EBEFEB;
}
circle:hover {
fill: #635C5C;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: arial;
font-size: 12px;
}
</style>
</head>
<body>
<h1>GHG emissions in the world in 2012<h1/>
<p>Countries are ranked by total greenhouse gases (GHG) emissions (tonnes of CO2 equivalent), and per capita (thousand kgs). Based on <a href="https://stats.oecd.org/">OECD data.<a/></p>
<p>Overall, we can see that the United States are far away from the others, and Australia has a high level of greenhouse gas emissions per person, but a low total emissions. </p>
<script type="text/javascript">
var w = 900;
var h = 600;
var padding = [7, 10, 30, 95 ]; //top - 0, right - 1, bottom - 2, left - 3
var xScale = d3.scale.linear()
.range([ padding[3], w - padding[1] ]);
var yScale = d3.scale.linear()
.range ( [ padding[0], h - padding[2] ]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(10)
.tickFormat(function(d)
{ return d + " kg ";
})
;
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(7)
.tickFormat(function(d)
{ return d + " t. ";
})
;
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("melaniejuliendata.csv", function(data) {
data.sort(function(a, b) {
return d3.descending(+a.GHG2012,
+b.GHG2012);
});
xScale.domain( [
d3.min(data, function(d) {
return +d.GHGin2012;
}),
d3.max(data, function(d) {
return +d.GHGin2012;
})
]);
yScale.domain([
d3.max(data, function(d) {
return +d.totalGHG;
}),
d3.min(data, function(d) {
return +d.totalGHG;
})
]);
var circles = svg.selectAll("circle")
.data(data)
.enter()
.append("circle");
circles.attr("cx", -100)
.attr("cy", function(d, i) {
return yScale(d.totalGHG);
})
.attr("r", 0,1)
.attr("fill", "#D11919")
.append("title")
.text(function(d) {
return d.Country + "'s total GHG emissions in 2012 are " + d.totalGHG +" tonnes of CO2 equivalent, and emissions per capita are "+ d.GHGin2012 +" thousand kg";
});
circles.transition()
.duration(2000)
.attr("cx", function(d) {
return xScale(d.GHGin2012);
})
.attr ("fill", "#006600")
.attr ("r", 4);
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] - 5) + ",0)")
.call(yAxis);
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js