Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created July 26, 2013 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/6092282 to your computer and use it in GitHub Desktop.
Save enjalot/6092282 to your computer and use it in GitHub Desktop.
kiera stream data
{"description":"kiera stream data","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"streamdata.csv":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"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/6FL5f6U.png"}
var data = tributary.streamdata
var svg = d3.select("svg");
var margin = {top: -8, right: -2, bottom: -42, left: 22};
var width = 1351 - margin.left - margin.right;
var height = 179 - margin.top - margin.bottom;
var streamOpacity = 0.72;
var barOpacity = .0;
var x = d3.scale.ordinal()
.rangeRoundBands([-10, width], 1.08);
var y = d3.scale.linear()
.rangeRound([height, 49]);
var color = d3.scale.ordinal()
.range(["#021B7C", "#AA104E", "#850385", "#099781", "#A0B314", "#F1390B"]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("top");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(d3.format(".2s"));
var names = d3.keys(data[5]).filter(function(key) { return key !== "year"; })
color.domain(names);
var stack = d3.layout.stack().offset("wiggle")
processed = data.map(function(d) {
var y0 = 6;
var ret = {
name: d.name,
year: d.year,
values: d
}
ret.months = names.map(function(name) { return { year: d.year, name: name, y0: y0, y1: y0 += +d[name]}; });
ret.total = ret.months[ret.months.length - 1].y1;
return ret
});
//processed.sort(function(a, b) { return b.total - a.total; });
x.domain(processed.map(function(d) { return d.year; }));
y.domain([0, d3.max(processed, function(d) { return d.total; })]);
var layers = names.map(function(name) {
var layer = [];
for(var i = 0; i < data.length; i++) {
var d = null;
processed[i].months.forEach(function(p) { if(p.name == name) d = p;});
layer.push(d);
}
console.log("name",name)
return {
months: layer,
name: name
};
})
var area = d3.svg.area()
.x(function(d,i) { return x(i); })
.y0(function(d) { return y(d.y0); })
.y1(function(d) { return y(d.y1) ; })
.interpolate("basis")
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.attr("transform", "translate(20, 20)rotate(45)")
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
var state = svg.selectAll(".state")
.data(processed)
.enter().append("g")
.attr("class", "g")
.attr("transform", function(d) { return "translate(" + x(d.year) + ",0)"; });
state.selectAll("rect")
.data(function(d) { return d.months; })
.enter().append("rect")
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.y1); })
.attr("height", function(d) { return y(d.y0) - y(d.y1); })
.style("fill", function(d) { return color(d.name); })
.style("opacity", barOpacity)
svg.selectAll("path.stream")
.data(layers)
.enter().append("path").classed("stream", true)
.attr("d", function(d) { return area(d.months) })
.style("fill", function(d,i) { return color(d.name); })
.style("stroke", function(d,i) { return color(d.name); })
.style("opacity", streamOpacity)
var legend = svg.selectAll(".legend")
.data(color.domain().slice().reverse())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 19 + ")"; });
legend.append("rect")
.attr("x", width - 1)
.attr("width", 33)
.attr("height", 5)
.style("fill", color);
legend.append("text")
.attr("x", width - 9)
.attr("y", 3)
.attr("dy", ".27em")
.style("text-anchor", "end")
.text(function(d) { return d; });
year education exploration interactive print art catestrophic
1997 9 0 3 0 0 0
1998 6 0 0 7 0 0
1999 2 1 6 10 0 1
2000 10 1 0 0 0 0
2001 1 0 11 7 0 0
2002 0 0 6 11 0 0
2003 9 0 0 3 7 0
2004 0 0 0 6 5 0
2005 0 0 0 12 0 0
2006 11 0 0 6 0 0
2007 0 0 11 5 0 1
2008 0 2 6 6 0 0
2009 0 8 1 2 0 0
2010 0 0 5 0 5 0
2011 0 0 6 0 6 0
2012 3 0 8 0 0 0
2013 1 0 4 0 1 0
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.x.axis path {
display: none;
}
.hover {
opacity: 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment