[ Launch: Ejemplo Jacathon Streamgraph ] 0c9daf6053778d6ee0be by bluezio
-
-
Save agarciadom/0c9daf6053778d6ee0be to your computer and use it in GitHub Desktop.
Ejemplo Jacathon Streamgraph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"description":"Ejemplo Jacathon Streamgraph","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":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"thumbnail":"http://i.imgur.com/0uVfYrK.png"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var n = 20, // number of layers | |
m = 200, // number of samples per layer | |
stack = d3.layout.stack().offset("wiggle"), | |
layers0 = stack(d3.range(n).map(function() { return bumpLayer(m); })), | |
layers1 = stack(d3.range(n).map(function() { return bumpLayer(m); })); | |
var width = 400, | |
height = 300; | |
var x = d3.scale.linear() | |
.domain([0, m - 1]) | |
.range([0, width]); | |
var y = d3.scale.linear() | |
.domain([0, d3.max(layers0.concat(layers1), function(layer) { return d3.max(layer, function(d) { return d.y0 + d.y; }); })]) | |
.range([height, 0]); | |
var color = d3.scale.linear() | |
.range(["red", "blue"]); | |
var area = d3.svg.area() | |
.x(function(d) { return x(d.x); }) | |
.y0(function(d) { return y(d.y0); }) | |
.y1(function(d) { return y(d.y0 + d.y); }); | |
var svg = d3.select("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.on("click", transition); | |
svg.selectAll("path") | |
.data(layers0) | |
.enter().append("path") | |
.attr("d", area) | |
.style("fill", function() { return color(Math.random()); }); | |
function transition() { | |
d3.selectAll("path") | |
.data(function() { | |
var d = layers1; | |
layers1 = layers0; | |
layers0 = d; | |
return layers0; | |
}) | |
.transition() | |
.duration(2500) | |
.attr("d", area); | |
} | |
// Inspired by Lee Byron's test data generator. | |
function bumpLayer(n) { | |
function bump(a) { | |
var x = 1 / (0.1 + Math.random()), | |
y = 2 * Math.random() - 0.5, | |
z = 10 / (0.1 + Math.random()); | |
for (var i = 0; i < n; i++) { | |
var w = (i / n - y) * z; | |
a[i] += x * Math.exp(-w * w); | |
} | |
} | |
var a = [], i; | |
for (i = 0; i < n; ++i) a[i] = 0; | |
for (i = 0; i < 5; ++i) bump(a); | |
return a.map(function(d, i) { return {x: i, y: Math.max(0, d)}; }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment