Skip to content

Instantly share code, notes, and snippets.

@GerardoFurtado
Last active January 1, 2016 03:54
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/1c1c56ea3e42825cea35 to your computer and use it in GitHub Desktop.
Save GerardoFurtado/1c1c56ea3e42825cea35 to your computer and use it in GitHub Desktop.
Australian energy: bar chart
year coal oillpg gas renewables uranium
1977 2146 977 256 200 231
1978 2200 1016 283 202 234
1979 2229 1005 315 200 323
1980 2286 960 363 194 386
1981 2637 934 416 207 1066
1982 2798 909 462 212 2363
1983 2988 894 466 205 2177
1984 3143 1076 490 203 2082
1985 3573 1248 523 215 2055
1986 3947 1281 571 217 2092
1987 4360 1270 588 217 2117
1988 4035 1261 611 221 1971
1989 4426 1145 628 231 2140
1990 4685 1284 797 235 1922
1991 4880 1276 840 239 2063
1992 5177 1254 932 225 2044
1993 5245 1236 978 247 1271
1994 5261 1171 1065 255 1293
1995 5665 1250 1175 262 1237
1996 5746 1216 1204 272 2399
1997 6139 1249 1226 285 2818
1998 6617 1374 1276 283 2725
1999 6663 1136 1333 283 3002
2000 7046 1502 1317 267 3902
2001 7549 1540 1375 266 4535
2002 7952 1458 1389 257 3782
2003 8019 1422 1462 277 4311
2004 8262 1262 1463 279 4497
2005 8792 1135 1638 280 5153
2006 8887 1027 1701 284 4688
2007 9360 1146 1801 288 4507
2008 9384 1055 1875 286 4758
2009 9761 1083 1961 261 4846
2010 10519 1057 2084 297 3341
2011 9970 1059 2225 299 3322
2012 10632 993 2134 295 3599
2013 11438 882 2439 329 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: bar 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;
}
.button {
border-top: 1px solid #ffffff;
background: #ffffff;
background: -webkit-gradient(linear, left top, left bottom, from(#d4d4d4), to(#ffffff));
background: -webkit-linear-gradient(top, #d4d4d4, #ffffff);
background: -moz-linear-gradient(top, #d4d4d4, #ffffff);
background: -ms-linear-gradient(top, #d4d4d4, #ffffff);
background: -o-linear-gradient(top, #d4d4d4, #ffffff);
padding: 4px 10px;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
color: #262426;
font-size: 14px;
font-family: "Roboto";
text-decoration: none;
vertical-align: middle;
margin-right: 10px;
margin-bottom: 5px;
margin-top: 5px;
}
.button:hover {
border-top-color: #bfc4c7;
background: #cfd4d7;
color: #000000;
cursor: pointer;
}
.button:active {
border-top-color: #ffffff;
background: #eeeeee;
}
.button:focus {
outline: 0;
}
.btn-group {
padding-left: 0px;
}
.bars:hover {
fill: maroon;
}
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;
}
.x.axis path {
opacity: 1;
}
.y.axis path,
.y.axis line {
stroke: #fff;
stroke-dasharray: 1,2;
opacity: 1;
}
.axis text {
fill: #4e5a64;
font-size: 10px;
}
</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 bar 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 vertical bar represents a year.</p>
<p>Click on the buttons to change the energy source. Hover over the bars to see more details.</p>
<div class="btn-group" data-toggle="buttons-radio">
<button type="button" id="sourcecoal" class="button">Coal</button>
<button type="button" id="sourceoillpg" class="button">Oil and LPG</button>
<button type="button" id="sourcegas" class="button">Gas</button>
<button type="button" id="sourcerenewables" class="button">Renewables</button>
<button type="button" id="sourceuranium" class="button">Uranium</button>
</div>
<div id="svganchor"></div>
<br>
<p>Bar charts are good for comparing values within a given energy source, but not so much for comparing two or more energy sources. Among other reasons, this happens because the y-axis domain changes every time you change the energy source.</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 = [ 0, 50, 20, 0 ]; //Top, right, bottom, left
var xScale = d3.scale.ordinal()
.rangeBands([ padding[3], (w - padding[1]) ], .2);
var yScale = d3.scale.linear()
.range([ h - padding[2], padding[0] ]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.outerTickSize(0)
.tickValues(["1977", "1980", "1985", "1990", "1995", "2000", "2005", "2010", "2013"]);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("right")
.ticks(10)
.innerTickSize(-(w))
.outerTickSize(0);
var svg = d3.select("#svganchor")
.append("svg")
.attr("width", w)
.attr("height", h);
var tt = d3.select("#svganchor").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var colors = ['#1b9e77','#d95f02','#7570b3','#e7298a','#66a61e'];
var ci = 3;
var esource = "coal";
var label = "Coal";
d3.csv("australianenergy.csv", function(data) {
//Set scale domains
xScale.domain(data.map(function(d) {
return d.year
}));
var bars = svg.selectAll("rect")
.data(data)
.enter()
.append("rect");
bars.attr("x", function(d) {
return xScale(d.year)
})
.attr("width", xScale.rangeBand())
.attr("y", h - padding[2])
.attr("height", 0)
.attr("fill", colors[ci])
.attr("class", "bars");
d3.select("#sourcecoal").on("click", function(){
esource = "coal";
ci = 3;
label = "Coal";
draw();
});
d3.select("#sourceoillpg").on("click", function(){
esource = "oillpg";
ci = 0;
label = "Oil and LPG";
draw();
});
d3.select("#sourcegas").on("click", function(){
esource = "gas";
ci = 1;
label = "Gas";
draw();
});
d3.select("#sourcerenewables").on("click", function(){
esource = "renewables";
ci = 2
label = "Renewables";
draw();
});
d3.select("#sourceuranium").on("click", function(){
esource = "uranium";
ci = 4;
label = "Uranium";
draw();
});
draw();
function draw() {
yScale.domain([
0, d3.max(data, function(d) {
return +d[esource]
})
]);
bars.transition()
.delay(function(d, i) {
return i * 50
})
.duration(1000)
.attr("y", function(d) {
return yScale(d[esource])
})
.attr("height", function(d) {
return h - padding[2] - yScale(d[esource])
})
.attr("fill", colors[ci]);
bars.on("mousemove", function(d) {
tt.html("&nbspIn " + d.year + ", Australia produced <br><strong>&nbsp" + d[esource] + "</strong> Petajoules from " + label + "&nbsp")
.style('top', d3.event.pageY - 12 + 'px')
.style('left', d3.event.pageX + 25 + 'px')
.style("opacity", 0.95);
});
bars.on("mouseout", function() {
tt.style("opacity", 0)
});
d3.transition(svg).select(".y.axis")
.transition()
.duration(2000)
.call(yAxis)
d3.selectAll(".tick")
.filter(function (d) { return d === 0; })
.remove();
}
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(" + (w - (padding[1])) + ",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