Skip to content

Instantly share code, notes, and snippets.

@tarvaina
Created August 1, 2013 13:39
Show Gist options
  • Save tarvaina/6131413 to your computer and use it in GitHub Desktop.
Save tarvaina/6131413 to your computer and use it in GitHub Desktop.
Stacked bars practice
{"description":"Stacked bars practice","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/zDIQoOA.png"}
var graphHeight = 594;
var data = [
[{y: 3}, {y: 3}, {y: 3}, {y: 13}, {y: 3}],
[{y: 11}, {y: 3}, {y: 3}, {y: 3}, {y: 6}],
[{y: 3}, {y: 7}, {y: 7}, {y: 3}, {y: 3}]
];
var stack = d3.layout.stack();
stack(data);
var maxY = d3.max(data, function(d) {
return d3.max(d, function(v) {
return v.y + v.y0;
});
});
console.log(maxY);
var scaleY = d3.scale.linear().domain([0, maxY]).range([0, graphHeight])
var svg = d3.select("svg");
var colors = d3.scale.category20();
var layers = svg.selectAll("g").data(data).enter().append("g")
.style("fill", function(d, i) { return colors(i); });
var bars = layers.selectAll("rect")
.data(function(d) { return d; })
.enter()
.append("rect")
.attr({
x: function(d, i) { return 30 + 13 * i; },
y: function(d, i) { return 30 + graphHeight - scaleY(d.y0 + d.y); },
width: 9,
height: function(d, i) { return scaleY(d.y); }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment