Skip to content

Instantly share code, notes, and snippets.

@sethfam
Forked from mbostock/.block
Last active July 13, 2017 15:54
Show Gist options
  • Save sethfam/d89662d78c8d16a5461b763bcc80b1fa to your computer and use it in GitHub Desktop.
Save sethfam/d89662d78c8d16a5461b763bcc80b1fa to your computer and use it in GitHub Desktop.
Stacked-to-Multiples
license: gpl-3.0
group date value
VSS 2009-01 39123
VSS 2009-02 39044
VSS 2009-03 39064
VSS 2009-04 38276
VSS 2009-05 37440
VSS 2009-06 36321
VSS 2009-07 37352
VSS 2009-08 36577
VSS 2009-09 37262
VSS 2009-10 37362
VSS 2009-11 37684
VSS 2009-12 38861
VSS 2010-01 38558
VSS 2010-02 37653
VSS 2010-03 39064
VSS 2010-04 35930
VSS 2010-05 35880
VSS 2010-06 36119
VSS 2010-07 35017
VSS 2010-08 35024
VSS 2010-09 33930
VSS 2010-10 32837
VSS 2010-11 33427
VSS 2010-12 32852
VSS 2011-01 31994
VSS 2011-02 32375
VSS 2011-03 32731
VSS 2011-04 31384
VSS 2011-05 30793
VSS 2011-06 31452
VSS 2011-07 31526
VSS 2011-08 30985
VSS 2011-09 31214
VSS 2011-10 32073
VSS 2011-11 32271
VSS 2011-12 32153
VSS 2012-01 32102
VSS 2012-02 32082
VSS 2012-03 32326
VSS 2012-04 33802
VSS 2012-05 33686
VSS 2012-06 32179
VSS 2012-07 32358
VSS 2012-08 31564
VSS 2012-09 30379
VSS 2012-10 32508
VSS 2012-11 33268
VSS 2012-12 33886
VSS 2013-01 33559
VSS 2013-02 34202
VSS 2013-03 35075
VSS 2013-04 33993
VSS 2013-05 34841
VSS 2013-06 34207
VSS 2013-07 34840
VSS 2013-08 37028
VSS 2013-09 36992
VSS 2013-10 36675
VSS 2013-11 35230
VSS 2013-12 35218
VSS 2014-01 36163
VSS 2014-02 37850
VSS 2014-03 38548
VSS 2014-04 38503
VSS 2014-05 39439
VSS 2014-06 40043
VSS 2014-07 41581
VSS 2014-08 40641
VSS 2014-09 40680
VSS 2014-10 40721
VSS 2014-11 40594
VSS 2014-12 39928
VSS 2015-01 40075
VSS 2015-02 40924
VSS 2015-03 41486
VSS 2015-04 42588
SVS 2009-01 27310
SVS 2009-02 27041
SVS 2009-03 26453
SVS 2009-04 25979
SVS 2009-05 25660
SVS 2009-06 25019
SVS 2009-07 24156
SVS 2009-08 23625
SVS 2009-09 22558
SVS 2009-10 21844
SVS 2009-11 23603
SVS 2009-12 22949
SVS 2010-01 22449
SVS 2010-02 21654
SVS 2010-03 26453
SVS 2010-04 21086
SVS 2010-05 20416
SVS 2010-06 20157
SVS 2010-07 19196
SVS 2010-08 19193
SVS 2010-09 18716
SVS 2010-10 18269
SVS 2010-11 18586
SVS 2010-12 18552
SVS 2011-01 18233
SVS 2011-02 18603
SVS 2011-03 18234
SVS 2011-04 17938
SVS 2011-05 16481
SVS 2011-06 16674
SVS 2011-07 17174
SVS 2011-08 16777
SVS 2011-09 18297
SVS 2011-10 20062
SVS 2011-11 20577
SVS 2011-12 20223
SVS 2012-01 20856
SVS 2012-02 20841
SVS 2012-03 21522
SVS 2012-04 21541
SVS 2012-05 21656
SVS 2012-06 21104
SVS 2012-07 23007
SVS 2012-08 23207
SVS 2012-09 25547
SVS 2012-10 28161
SVS 2012-11 28450
SVS 2012-12 29096
SVS 2013-01 28875
SVS 2013-02 28958
SVS 2013-03 29733
SVS 2013-04 31031
SVS 2013-05 31267
SVS 2013-06 31589
SVS 2013-07 31348
SVS 2013-08 31426
SVS 2013-09 30500
SVS 2013-10 29736
SVS 2013-11 29076
SVS 2013-12 28451
SVS 2014-01 27884
SVS 2014-02 27377
SVS 2014-03 27415
SVS 2014-04 27763
SVS 2014-05 27325
SVS 2014-06 27021
SVS 2014-07 26162
SVS 2014-08 28341
SVS 2014-09 31105
SVS 2014-10 36571
SVS 2014-11 36027
SVS 2014-12 36111
SVS 2015-01 35703
SVS 2015-02 35293
SVS 2015-03 35411
SVS 2015-04 35716
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: auto;
position: relative;
width: 100%;
overflow: visible;
scroll: auto;
}
text {
font: 10px sans-serif;
}
.axis path {
display: none;
}
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.group-label {
font-weight: bold;
text-anchor: end;
}
form {
position: absolute;
right: 10px;
top: 10px;
}
</style>
<form>
<label><input type="radio" name="mode" value="multiples" checked> Multiples</label>
<label><input type="radio" name="mode" value="stacked"> Stacked</label>
</form>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var parseDate = d3.time.format("%Y-%m").parse,
formatYear = d3.format("02d"),
formatDate = function(d) { return "Q" + ((d.getMonth() / 3 | 0) + 1) + formatYear(d.getFullYear() % 100); };
var margin = {top: 10, right: 20, bottom: 20, left: 60},
width = 1000 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var y0 = d3.scale.ordinal()
.rangeRoundBands([height, 0], .2);
var y1 = d3.scale.linear();
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1, 0);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickFormat(formatDate)
.ticks(4);
var nest = d3.nest()
.key(function(d) { return d.group; });
var stack = d3.layout.stack()
.values(function(d) { return d.values; })
.x(function(d) { return d.date; })
.y(function(d) { return d.value; })
.out(function(d, y0) { d.valueOffset = y0; });
var color = d3.scale.category10();
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.tsv("data.tsv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.value = +d.value;
});
var dataByGroup = nest.entries(data);
stack(dataByGroup);
x.domain(dataByGroup[0].values.map(function(d) { return d.date; }));
y0.domain(dataByGroup.map(function(d) { return d.key; }));
y1.domain([0, d3.max(data, function(d) { return d.value; })]).range([y0.rangeBand(), 0]);
var group = svg.selectAll(".group")
.data(dataByGroup)
.enter().append("g")
.attr("class", "group")
.attr("transform", function(d) { return "translate(0," + y0(d.key) + ")"; });
group.append("text")
.attr("class", "group-label")
.attr("x", -6)
.attr("y", function(d) { return y1(d.values[0].value / 2); })
.attr("dy", ".35em")
.text(function(d) { return " " + d.key; }); // prefix series label
group.selectAll("rect")
.data(function(d) { return d.values; })
.enter().append("rect")
.style("fill", function(d) { return color(d.group); })
.attr("x", function(d) { return x(d.date); })
.attr("y", function(d) { return y1(d.value); })
.attr("width", x.rangeBand())
.attr("height", function(d) { return y0.rangeBand() - y1(d.value); });
group.filter(function(d, i) { return !i; }).append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + y0.rangeBand() + ")")
.call(xAxis);
d3.selectAll("input").on("change", change);
var timeout = setTimeout(function() {
d3.select("input[value=\"stacked\"]").property("checked", true).each(change);
}, 2000);
function change() {
clearTimeout(timeout);
if (this.value === "multiples") transitionMultiples();
else transitionStacked();
}
function transitionMultiples() {
var t = svg.transition().duration(750),
g = t.selectAll(".group").attr("transform", function(d) { return "translate(0," + y0(d.key) + ")"; });
g.selectAll("rect").attr("y", function(d) { return y1(d.value); });
g.select(".group-label").attr("y", function(d) { return y1(d.values[0].value / 2); })
}
function transitionStacked() {
var t = svg.transition().duration(750),
g = t.selectAll(".group").attr("transform", "translate(0," + y0(y0.domain()[0]) + ")");
g.selectAll("rect").attr("y", function(d) { return y1(d.value + d.valueOffset); });
g.select(".group-label").attr("y", function(d) { return y1(d.values[0].value / 2 + d.values[0].valueOffset); })
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment