Skip to content

Instantly share code, notes, and snippets.

@GerardoFurtado
Last active December 27, 2015 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GerardoFurtado/f4bff7b5442981d70c6c to your computer and use it in GitHub Desktop.
Save GerardoFurtado/f4bff7b5442981d70c6c to your computer and use it in GitHub Desktop.
Australian energy: line chart
Source 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
Coal 2146 2200 2229 2286 2637 2798 2988 3143 3573 3947 4360 4035 4426 4685 4880 5177 5245 5261 5665 5746 6139 6617 6663 7046 7549 7952 8019 8262 8792 8887 9360 9384 9761 10519 9970 10632 11438
OilLPG 977 1016 1005 960 934 909 894 1076 1248 1281 1270 1261 1145 1284 1276 1254 1236 1171 1250 1216 1249 1374 1136 1502 1540 1458 1422 1262 1135 1027 1146 1055 1083 1057 1059 993 882
Gas 256 283 315 363 416 462 466 490 523 571 588 611 628 797 840 932 978 1065 1175 1204 1226 1276 1333 1317 1375 1389 1462 1463 1638 1701 1801 1875 1961 2084 2225 2134 2439
Renewables 200 202 200 194 207 212 205 203 215 217 217 221 231 235 239 225 247 255 262 272 285 283 283 267 266 257 277 279 280 284 288 286 261 297 299 295 329
Uranium 231 234 323 386 1066 2363 2177 2082 2055 2092 2117 1971 2140 1922 2063 2044 1271 1293 1237 2399 2818 2725 3002 3902 4535 3782 4311 4497 5153 4688 4507 4758 4846 3341 3322 3599 4229
<!DOCTYPE html>
<html lang="en">
<head>
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=PT+Serif' rel='stylesheet' type='text/css'>
<meta charset="utf-8">
<title>Australian energy: line chart</title>
<style type="text/css">
body {
background-color: white;
font-family: 'PT Serif', serif;
}
#container {
width: 1000px;
margin-left: auto;
margin-right: auto;
margin-top: 5px;
margin-bottom: 0px;
padding-bottom: 0px;
padding: 1px 10px 10px 10px;
background-color: white;
box-shadow: 1px 1px 1px 1px #fff;
}
h1 {
font-weight: 300;
color: #3e4a54;
font-size: 48px;
font-family: "Roboto";
margin-bottom: 20px;
margin-top: 10px;
}
h2 {
font-weight: 300;
color: #3e4a54;
font-size: 32px;
margin-bottom: 0px;
padding-bottom: 0px;
margin-top: 15px;
font-family: "Roboto";
}
p {
font-size: 16px;
width: 900px;
}
a {
color: steelblue;
}
a:hover {
color: darkslategray;
}
.footer {
font-size: 14px;
margin-top: 0px;
}
.sourcetext:hover {
cursor: default;
}
div.tooltip {
position: absolute;
text-align: center;
white-space: normal;
padding: 2px;
font-size: 14px;
background: whitesmoke;
border: 1px solid gray;
border-radius: 4px;
pointer-events: none;
cursor: none;
}
svg {
background-color: white;
}
.axis path, .axis line {
opacity: 1;
fill: none;
stroke: #4e5a64;
shape-rendering: crispEdges;
}
.axis text {
fill: #4e5a64;
font-size: 10px;
font-family: sans-serif;
font-size: 11px;
}
</style>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
</head>
<body>
<div id="container">
<h1>Australian energy by fuel type</h1>
<img src="http://proecogroup.eu/sites/default/files/images/proecogroupPropjekty.jpg">
<h2>A line chart</h2>
<p>The following bar chart was created using d3.js. The data shows the amount of energy produced from five sources - coal, oil, uranium, gas and renewable sources - in Australia, from 1977 to 2013, in Petajoules (10<sup>15</sup> Joules). Each line represents an energy source.</p>
<p>Hover over the labels at the right to highlight the respective line. Hover over the lines to see more details.</p>
<div id="svganchor"></div>
<br>
<p>Line charts are good for comparing values among different groups, in this case among different energy sources, in a time series.</p>
<p>Source of the data: <a href="http://www.industry.gov.au/Office-of-the-Chief-Economist/Publications/Pages/Australian-energy-statistics.aspx#">Department of Industry and Science</a></p>
<p class="footer">Created by Gerardo Furtado.</p>
<br>
</div>
<script type="text/javascript">
var w = 900
var h = 400
var padding = [ 5, 75, 20, 45 ]; //Top, right, bottom, left
var dateFormat = d3.time.format("%Y");
var xScale = d3.time.scale()
.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(15)
.outerTickSize(0)
.tickFormat(function(d) {
return dateFormat(d);
});
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.outerTickSize(0)
.ticks(5);
var line = d3.svg.line()
.x(function(d) {
return xScale(dateFormat.parse(d.year));
})
.y(function(d) {
return yScale(+d.amount);
});
var svg = d3.select("#svganchor")
.append("svg")
.attr("width", w)
.attr("height", h);
var color = d3.scale.category10().domain(d3.range(0,10));
var datlabel = [11438, 882, 2439, 329, 4229];
var tt = d3.select("#svganchor").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var yline = svg.append("line")
.attr("stroke", "black")
.attr("stroke-dasharray", "1,2");
var xline = svg.append("line")
.attr("stroke", "black")
.attr("stroke-dasharray", "1,2");
d3.csv("australianenergy2.csv", function(data) {
var years = ["1977", "1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013"];
var dataset = [];
//Loop once for each row in data
for (var i = 0; i < data.length; i++) {
//Create new object with this country's name and empty array
dataset[i] = {
source: data[i].Source,
energy: []
};
//Loop through all the years
for (var j = 0; j < years.length; j++) {
// If value is not empty
if (data[i][years[j]]) {
dataset[i].energy.push({
year: years[j],
amount: data[i][years[j]]
});
}
}
}
//Set scale domains
xScale.domain([
d3.min(years, function(d) {
return dateFormat.parse(d);
}),
d3.max(years, function(d) {
return dateFormat.parse(d);
})
]);
yScale.domain([
d3.max(dataset, function(d) {
return d3.max(d.energy, function(d) {
return +d.amount;
});
}),
0]);
//Make a group for each source
var groups = svg.selectAll("g")
.data(dataset)
.enter()
.append("g")
.attr("stroke", function(d) { return color(d.source); })
.attr("class", "datagroup");
//Avoid text being colored as the lines
groups.selectAll("text")
.data(dataset)
.enter();
//Within each group, create a new line/path
groups.selectAll("path")
.data(function(d) {
return [ d.energy ];
})
.enter()
.append("path")
.attr("class", "line")
.attr("d", line)
.attr("fill", "none")
.attr("stroke-width", 2);
//Append the label for the sources
groups.append("text")
.attr("x", w - 70)
.attr("y", function(d, i) {
return yScale(datlabel[i]) + 5;
})
.attr("font-family", "Helvetica")
.attr("font-size", 12)
.attr("class", "sourcetext")
.attr("stroke", "none")
.attr("fill", "darkslategray")
.text(function(d) {
return d.source;
});
d3.selectAll(".datagroup")
.on("mouseover", function() {
//Style *all* paths first
d3.selectAll(".datagroup")
.attr("opacity", 0.1);
//Style *this* path specially
d3.select(this)
.attr("opacity", 1.0)
.select(".line")
.attr("stroke-width", 5)
.on("mousemove", function(d) {
var mousex = d3.mouse(this);
mousex = mousex[0] + 12;
var invertedx = xScale.invert(mousex);
invertedx = invertedx.getFullYear();
var mousedate = years.indexOf(String(invertedx));
var pon = d.energy[mousedate].amount;
var posx = invertedx.toString();
tt.html("&nbspThe total energy produced from " + d.source + " <br>in " + invertedx + " was <strong>" + pon + "</strong> Petajoules")
.style('top', d3.event.pageY - 12 + 'px')
.style('left', d3.event.pageX + 15 + 'px')
.style("opacity", .95);
xline.attr("x1", xScale(dateFormat.parse(posx)))
.attr("y1", yScale(pon))
.attr("y2", (h - padding[2]))
.attr("x2", xScale(dateFormat.parse(posx)))
.attr("opacity", 1);
yline.attr("x1", xScale(dateFormat.parse(posx)))
.attr("y1", yScale(pon))
.attr("y2", yScale(pon))
.attr("x2", padding[3])
.attr("opacity", 1);
});
})
.on("mouseout", function() {
//Restore defaults to all paths
d3.selectAll(".datagroup")
.attr("opacity", 1.0)
d3.selectAll("path")
.attr("stroke-width", 2);
tt.style("opacity", 0);
xline.attr("opacity", 0);
yline.attr("opacity", 0)
});
svg.append("text")
.attr("x", -padding[0])
.attr("y", padding[3] + 15)
.attr("transform", "rotate(-90)")
.attr("font-family", "Helvetica")
.attr("font-size", 12)
.attr("fill", "darkslategray")
.attr("text-anchor", "end")
.text("Petajoules");
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);
d3.selectAll(".tick")
.filter(function (d) { return d === 0; })
.remove();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment