var data = [
[{y:21},{y:10},{y:10},{y:38},{y:20}],
[{y:14},{y:25},{y:21},{y:10},{y:10}],
[{y:14},{y:35},{y:21},{y:10},{y:4}]
];
var height = 600;
var width = 500;
var ch = 100;
var yScale = d3.scale.linear()
.domain([0, 38])
.range([0, ch]);
var colors = ["#ff0000", "#00ff00", "#0000ff"];
var color = d3.scale.category20();
var svg = d3.select("body").append("svg")
.attr({
"width": width,
"height": height
});
var stack = d3.layout.stack();
stack(data);
var group = svg.append("g")
.attr("transform", "translate(80, 120)");
var layers = group.selectAll("g")
.data(data)
.enter()
.append("g")
.style("fill", function(d, i) {return color(i);});
var stacks = layers.selectAll("rect")
.data(function(d){return d;})
.enter()
.append("rect")
.attr({
"width": 30,
"height": function(d, i) {return yScale(d.y);},
"x": function(d, i) {return i * 50;},
"y": function(d, i) { return ch - yScale(d.y + d.y0); }
});
console.log(data);
//make a color scale, stackify the data, then draw the rects
//on a group you can translate around
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.