xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<title>Worldwide nuclear energy use</title>
<script type="text/javascript" src="https://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: "Gotham Narrow", Helvetica, Arial, sans-serif;
}
h1 {
font-size: 24px;
font-family: "Gotham Narrow", Helvetica, Arial, sans-serif;
margin: 0;
}
p {
font-size: 14px;
font-family: "Gotham Narrow", Helvetica, Arial, sans-serif;
margin: 10px 0 0 0;
}
svg {
background-color: white;
}
circle:hover {
fill: orange;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: "Gotham Narrow", sans-serif;
font-size: 20px;
}
</style>
</head>
<body>
<h1>Change in nuclear energy consumption</h1>
<p>Nuclear energy use (TWh) in 2012 compared to 2002 for countries with non-zero historical production. Source: <a href="https://www.bp.com/en/global/corporate/about-bp/energy-economics/statistical-review-of-world-energy.html" target="_blank">BP Statistical Review of World Energy</a> (2014)</p>
<script type="text/javascript">
var w = 1000;
var h = 600;
var padding = [ 30, 100, 60, 130 ]; //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 xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
d3.csv("NuclearEnergyConsumption1965-2012clean.csv", function(data) {
xScale.domain([
d3.min(data, function(d) {
return +d.y2012;
}),
d3.max(data, function(d) {
return +d.y2012;
})
]);
yScale.domain([
d3.max(data, function(d) {
return +d.y2012-d.y2002;
}),
d3.min(data, function(d) {
return +d.y2012-d.y2002;
})
]);
var circles = svg.selectAll("circle")
.data(data)
.enter()
.append("circle");
circles.attr("cx", function(d, i) {
return xScale(d.y2012);
})
.attr("cy", function(d, i) {
return yScale(d.y2012-d.y2002);
})
.attr("r", 1)
.attr("fill", "darkred")
.append("title")
.text(function(d) {
return d.country +"'s nuclear energy consumption in 2012 was " +d.y2012 +" TWh, a change of " +(d.y2012-d.y2002) +" TWh relative to 2002";
})
circles.transition()
.duration(2000)
.attr("r", 6);
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);
svg.append("text")
.attr("text-anchor", "end")
.attr("x", (w - padding[1]) - 100)
.attr("y", (h - padding[2] + 50))
.text("Nuclear energy use 2012 (TWh)");
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("text-anchor", "start")
.attr("x", -300)
.attr("y", 65)
.text("Difference to 2002 (TWh)");
});
</script>
</body>
</html>
Modified http://d3js.org/d3.v3.js to a secure url
https://d3js.org/d3.v3.js