[ Launch: Stacked bars practice ] 6131413 by tarvaina
-
-
Save tarvaina/6131413 to your computer and use it in GitHub Desktop.
Stacked bars practice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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