Skip to content

Instantly share code, notes, and snippets.

@trinary
Created February 14, 2013 19:59

Revisions

  1. trinary revised this gist Feb 14, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions _.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    [ <a href="http://tributary.io/inlet/4955866">Launch: Simple(?) Stacked Bar Charts</a> ] 4955866 by trinary<br>[ <a href="http://tributary.io/inlet/4945496">Launch: Simple(?) Stacked Bar Charts</a> ] 4945496 by gelicia<br>
  2. trinary created this gist Feb 14, 2013.
    1 change: 1 addition & 0 deletions config.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    {"description":"Simple(?) Stacked Bar Charts","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}
    80 changes: 80 additions & 0 deletions inlet.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    /*modified from Mike Bostock at http://bl.ocks.org/3943967 */

    var data = [
    {"key":"FL", "pop1":3000, "pop2":3000, "pop3":3000},
    {"key":"CA", "pop1":3000, "pop2":3000, "pop3":3000},
    {"key":"NY", "pop1":12000, "pop2":5000, "pop3":13000},
    {"key":"NC", "pop1":8000, "pop2":21000, "pop3":11000},
    {"key":"SC", "pop1":30000, "pop2":12000, "pop3":8000},
    {"key":"AZ", "pop1":31000, "pop2":1000, "pop3":28000},
    {"key":"TX", "pop1":8000, "pop2":42000, "pop3":20000}
    ];

    var n = 3, // number of layers
    m = data.map(function(i) {console.log(i);return i.key}), // number of samples per layer
    stack = d3.layout.stack(),

    //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 < data.length; ++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: 10},
    width = 677 - margin.left - margin.right,
    height = 414 - margin.top - margin.bottom;

    var x = d3.scale.ordinal()
    .domain(m)
    .rangeRoundBands([0, width], .08);

    var y = d3.scale.linear()
    .domain([0, yStackMax])
    .range([height, 0]);

    var color = d3.scale.linear()
    .domain([0, n - 1])
    .range(["#aad", "#556"]);

    var xAxis = d3.svg.axis()
    .scale(x)
    .tickSize(1)
    .tickPadding(6)
    .orient("bottom");

    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("x", function(d) { return x(d.x); })
    .attr("y", height)
    .attr("width", x.rangeBand())
    .attr("height", 0)
    .attr("y", function(d) { return y(d.y0 + d.y); })
    .attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); });

    svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);