Skip to content

Instantly share code, notes, and snippets.

@vaibhavb
Last active December 28, 2015 01:49
Show Gist options
  • Save vaibhavb/7423791 to your computer and use it in GitHub Desktop.
Save vaibhavb/7423791 to your computer and use it in GitHub Desktop.
What's the world buying?

This table shows consumption of countries around the world. Its a list of costs! May be i'll break them in separate bars.

country recreation clothing household alcohol electronics healthcare
Canada 2238 937 2008 904 359 5257
USA 2883 1406 1495 673 1406 8233
Sweden 2642 1094 1213 780 368 4708
England 3621 1512 1648 1031 764 3495
Switzerland 2582 1159 1423 1120 395 7699
Norway 4242 2027 2027 1379 794 8039
Finland 2555 996 1277 1090 413 3955
Denmark 2940 1187 1680 968 548 6253
Germany 2071 1042 1042 778 365 4654
Brazil 82 130 82 136 27 990
Peru 53 128 82 46 7 258
Colombia 120 91 120 113 18 407
Nigeria 16 486 28 17 3 67
Egypt 29 83 56 29 7 125
Pakistan 1 35 21 15 1 28
China 26 72 43 24 9 219
Thailand 140 143 116 114 46 179
Indonesia 23 48 70 68 6 84
Australia 2734 773 1309 936 568 5174
India 10 25 18 12 3 51
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
svg {
width: 960px;
height: 500px;
border: solid 1px #ccc;
font: 10px sans-serif;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.3.9/d3.min.js"></script>
<script>
var w = 960,
h = 500,
p = [20, 50, 30, 20],
x = d3.scale.ordinal().rangeRoundBands([0, w - p[1] - p[3]]),
y = d3.scale.linear().range([0, h - p[0] - p[2]]),
z = d3.scale.ordinal().range(["lightpink", "darkgray", "lightblue", "lightgray", "lightgreen", "lightyellow"]);
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + p[3] + "," + (h - p[2]) + ")");
d3.csv("consumption.csv", function(error, data) {
// transpose the daa in to layers by expense-type.
var expenses = d3.layout.stack()(["recreation", "clothing", "household","alcohol", "electronics", "healthcare"].map(
function(expense) {
return data.map(function(d){
return {x: d.country, y: +d[expense]};
});
}));
// compute the x-domain (by-country) and y-domain (by top).
x.domain(expenses[0].map(function(d) { return d.x; }));
y.domain([0, d3.max(expenses[expenses.length - 1], function(d) { return d.y0 + d.y; })]);
// Add a group for each cause.
var expense = svg.selectAll("g.expense")
.data(expenses)
.enter().append("svg:g")
.attr("class", "expense")
.style("fill", function(d, i) { return z(i); })
.style("stroke", function(d, i) { return d3.rgb(z(i)).darker(); });
// Add a rect for each country.
var rect = expense.selectAll("rect")
.data(Object)
.enter().append("svg:rect")
.attr("x", function(d) { return x(d.x); })
.attr("y", function(d) { return -y(d.y0) - y(d.y); })
.attr("height", function(d) { return y(d.y); })
.attr("width", x.rangeBand());
// Add a label per country.
var label = svg.selectAll("text")
.data(x.domain())
.enter().append("svg:text")
.attr("x", function(d) { return x(d) + x.rangeBand() / 2; })
.attr("y", 6)
.attr("text-anchor", "middle")
.attr("dy", ".71em")
.text(function(d) {return d});
// Add y-axis rules.
var rule = svg.selectAll("g.rule")
.data(y.ticks(5))
.enter().append("svg:g")
.attr("class", "rule")
.attr("transform", function(d) { return "translate(0," + -y(d) + ")"; });
rule.append("svg:line")
.attr("x2", w - p[1] - p[3])
.style("stroke", function(d) { return d ? "#fff" : "#000"; })
.style("stroke-opacity", function(d) { return d ? .7 : null; });
rule.append("svg:text")
.attr("x", w - p[1] - p[3] + 6)
.attr("dy", ".35em")
.text(d3.format("$,d"));
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment