Skip to content

Instantly share code, notes, and snippets.

@poezn
Created January 24, 2014 01:21
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 poezn/8590346 to your computer and use it in GitHub Desktop.
Save poezn/8590346 to your computer and use it in GitHub Desktop.
Stacks o' coins
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
{"description":"Stacks o' coins","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},"assets.svg":{"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,"thumbnail":"http://i.imgur.com/obRSSpU.png"}
var num = 50,
h = 20,
w = 50,
width = 400,
padding = 100
var g = d3.select("#vis");
var data = [
{
"data": d3.range(num),
"label": "Mo' Money",
"color-side": "#5E6850",
"color-front": "#818B73"
},
{
"data": d3.range(num * Math.random()),
"label": "Less interest",
"color-side": "#75A730",
"color-front": "#9CC464"
}
];
var scale = d3.scale.ordinal()
.domain(data[0].data)
.rangeBands([padding, width]);
var stacks = g.selectAll("g")
.data(data)
.enter().append("g")
.attr({
"transform": function(d, i) {
return "translate(" + [0, i * 100] + ")";
}
});
stacks
.append("text")
.attr({
"transform": "translate(80, 37)",
"text-anchor": "end"
})
.style({
"font-size": 12
})
.text(function(d, i) {
return d.label;
});
var coins = stacks.selectAll("g.coin")
.data(function(d, i) {
return d.data;
});
var newCoins = coins.enter().append("g")
.attr({
"transform": function(d, i, p) {
var tx = 1000,
ty = p === 0 ? -h : 100;
return "translate(" + [tx, ty] + ")"
},
"class": "coin"
});
newCoins.append("use")
.attr({
"xlink:href": "#side"
})
.style({
"fill": function(d, i, p) {
return data[p]["color-side"];
}
})
;
newCoins.append("use")
.attr({
"xlink:href": "#front"
})
.style({
"fill": function(d, i, p) {
return data[p]["color-front"];
}
})
;
coins
.transition()
.delay(function(d, i) {
return i * 12
})
.attr({
"transform": function(d, i) {
var tx = scale(i),
ty = h + Math.random() * 2;
return "translate(" + [tx, ty] + ")"
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment