Skip to content

Instantly share code, notes, and snippets.

@adrianblanco
Created August 3, 2015 10:59
Show Gist options
  • Save adrianblanco/7a58059a728e27c44ecb to your computer and use it in GitHub Desktop.
Save adrianblanco/7a58059a728e27c44ecb to your computer and use it in GitHub Desktop.
Tributary inlet
{"description":"Tributary inlet","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"test.csv":{"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,"ajax-caching":true}
var data = [
{"key":"Wikpedia", "pop1":26614, "pop2":6944, "pop3":30778, "pop4":3000, "pop5":4000, "pop6":5000},
{"key":"De", "pop1":30000, "pop2":12000, "pop3":8000, "pop4":3000, "pop5":4000, "pop6":5000},
{"key":"小d郎", "pop1":8000, "pop2":21000, "pop3":11000, "pop4":3000, "pop5":4000, "pop6":5000},
];
var tag = ["Fútbol", "Deportes", "Entretenimiento", "経済政策", "雇用対策", "税制"];
var n = 6, // number of layers
m = data.length, // number of samples per layer
stack = d3.layout.stack(),
labels = data.map(function(d) {return d.key;}),
//go through each layer (pop1, pop2 etc, that's the range(n) part)
//then go through each object in data and pull out that objects's population data
//and put it into an array where x is the index and y is the number
layers = stack(d3.range(n).map(function(d) {
var a = [];
for (var i = 0; i < m; ++i) {
a[i] = {x: i, y: data[i]['pop' + (d+1)]};
}
return a;
})),
//the largest single layer
yGroupMax = d3.max(layers, function(layer) { return d3.max(layer, function(d) { return d.y; }); }),
//the largest stack
yStackMax = d3.max(layers, function(layer) { return d3.max(layer, function(d) { return d.y0 + d.y; }); });
var margin = {top: 40, right: 10, bottom: 20, left: 100},
width = 973 - margin.left - margin.right,
height = 86*data.length - margin.top - margin.bottom;
var y = d3.scale.ordinal()
.domain(d3.range(m))
.rangeRoundBands([0, height+10], 0.1);
var x = d3.scale.linear()
.domain([0, yStackMax])
.range([0, width-130]); //120px for legend
var color = d3.scale.category20();
var svg = d3.select("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var layer = svg.selectAll(".layer")
.data(layers)
.enter().append("g")
.attr("class", "layer")
.style("fill", function(d, i) { return color(i); });
layer.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("y", function(d) { return y(d.x); })
.attr("x", function(d) { return x(d.y0); })
.attr("height", y.rangeBand())
.attr("width", function(d) { return x(d.y); });
var yAxis = d3.svg.axis()
.scale(y)
.tickSize(1)
.tickPadding(6)
.tickValues(labels)
.orient("left");
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
var legend = svg.selectAll(".legend")
.data(color.domain().slice())
.style("position","fixed")
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 19)
.attr("width", 22)
.attr("height", 17)
.style("fill", color);
legend.append("text")
.attr("x", width - 22)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return tag[d]; });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment