Skip to content

Instantly share code, notes, and snippets.

@elsherbini
Created May 8, 2013 05:12
Show Gist options
  • Save elsherbini/5538352 to your computer and use it in GitHub Desktop.
Save elsherbini/5538352 to your computer and use it in GitHub Desktop.
copy-pastad barchart
{"description":"copy-pastad barchart","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},"data.csv":{"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}
State Response Predicted Difference
1 0.0526762956669499 0.0983417957543063 0.0456655000873564
2 0.116140051238258 0.109308795975831 0.00683125526242642
5 0.096722621902478 0.103514578497592 0.00679195659511383
4 0.0998322147651007 0.0942662765956997 0.00556593816940097
6 0.0888501742160279 0.0957795913389227 0.00692941712289478
8 0.102857142857143 0.0899265569700815 0.0129305858870613
9 0.114285714285714 0.0948882843180256 0.0193974299676887
11 0.166344294003868 0.139210777071404 0.0271335169324649
10 0.130327868852459 0.102019406504546 0.0283084623479134
12 0.0914479254868755 0.0967140837003516 0.00526615821347605
13 0.116455696202532 0.105320125277631 0.0111355709249005
15 0.12241653418124 0.0973781470936476 0.0250383870875924
19 0.0675569520816968 0.0874754963884159 0.0199185443067191
16 0.083739837398374 0.0876404912819252 0.00390065388355117
17 0.10310021629416 0.100537808890023 0.00256240740413684
18 0.117408906882591 0.0944305388334601 0.022978368049131
20 0.105723370429253 0.0883922268028023 0.0173311436264505
21 0.112757201646091 0.0952584898118371 0.0174987118342534
22 0.0944170771756979 0.113883404411713 0.0194663272360153
25 0.115602263540825 0.0924094822875007 0.0231927812533238
24 0.114985163204748 0.113757872022582 0.00122729118216568
23 0.0948678071539658 0.0908546138994951 0.00401319325447069
26 0.110655737704918 0.101263563653564 0.00939217405135387
27 0.0775646371976647 0.0869683401721915 0.00940370297452676
29 0.104453441295547 0.0940928215211176 0.010360619774429
28 0.0925 0.119318091923169 0.0268180919231688
30 0.0836038961038961 0.0921566944887218 0.0085527983848257
37 0.096551724137931 0.105857160497344 0.00930543635941303
38 0.0884070058381985 0.0817546546320644 0.00665235120613414
31 0.0789259560618389 0.0870241231197248 0.00809816705788589
33 0.106686701728024 0.0872847870404996 0.0194019146875245
34 0.104253544620517 0.0960349816380214 0.00821856298249567
35 0.121835443037975 0.0972718710441837 0.024563571993791
32 0.0916463909164639 0.0937160679317704 0.00206967701530644
36 0.112269446672013 0.103094198569698 0.00917524810231471
39 0.0959438377535101 0.0958805469620007 6.32907915094905e-05
40 0.138986013986014 0.102778640359023 0.036207373626991
41 0.0959016393442623 0.0924068074666989 0.00349483187756343
42 0.128606760098928 0.0980241169694057 0.0305826431295226
44 0.112540192926045 0.0927174215860511 0.0198227713399939
45 0.0982704402515723 0.106744274402152 0.00847383415057927
46 0.0770562770562771 0.0839645544144424 0.00690827735816535
47 0.117109634551495 0.0978943188522365 0.0192153156992586
48 0.0983125458547322 0.0938410677756411 0.0044714780790911
49 0.0714285714285714 0.0859483870582455 0.0145198156296741
51 0.0908333333333333 0.0953483972349073 0.00451506390157393
50 0.102817974105103 0.0921639206419876 0.0106540534631152
53 0.0711297071129707 0.0925596128701326 0.0214299057571619
55 0.0937996820349762 0.0942786704790301 0.000478988444053974
54 0.11340206185567 0.100501528206439 0.0129005336492309
56 0.0787728026533997 0.0905360696736087 0.011763267020209
var data = tributary.data
var n = 4, // number of layers
m = 58, // number of samples per layer
stack = d3.layout.stack(),
layers = stack(d3.range(n).map(function() { return bumpLayer(m, 0.1); })),
yGroupMax = d3.max(layers, function(layer) { return d3.max(layer, function(d) { return d.y; }); }),
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 = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal()
.domain(d3.range(m))
.rangeRoundBands([0, width], 0.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(0)
.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); });
var rect = 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);
rect.transition()
.delay(function(d, i) { return i * 10; })
.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);
d3.selectAll("input").on("change", change);
var timeout = setTimeout(function() {
d3.select("input[value=\"grouped\"]").property("checked", true).each(change);
}, 2000);
function change() {
clearTimeout(timeout);
if (this.value === "grouped") transitionGrouped();
else transitionStacked();
}
function transitionGrouped() {
y.domain([0, yGroupMax]);
rect.transition()
.duration(500)
.delay(function(d, i) { return i * 10; })
.attr("x", function(d, i, j) { return x(d.x) + x.rangeBand() / n * j; })
.attr("width", x.rangeBand() / n)
.transition()
.attr("y", function(d) { return y(d.y); })
.attr("height", function(d) { return height - y(d.y); });
}
function transitionStacked() {
y.domain([0, yStackMax]);
rect.transition()
.duration(500)
.delay(function(d, i) { return i * 10; })
.attr("y", function(d) { return y(d.y0 + d.y); })
.attr("height", function(d) { return y(d.y0) - y(d.y0 + d.y); })
.transition()
.attr("x", function(d) { return x(d.x); })
.attr("width", x.rangeBand());
}
// Inspired by Lee Byron's test data generator.
function bumpLayer(n, o) {
function bump(a) {
var x = 1 / (0.92 + Math.random()),
y = 2 * Math.random() - 0.5,
z = 10 / (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] = o + o * Math.random();
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