-
-
Save djodjoni/1922c1117fe0727b74076c72c9100332 to your computer and use it in GitHub Desktop.
D3 bubbles
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
<script src="https://raw.github.com/mbostock/d3/master/d3.js"></script> | |
<script src="https://raw.github.com/mbostock/d3/master/d3.layout.js"></script> | |
<div id="area"></div> | |
<style> | |
#area { background: #eee; width: 600px; height: 400px; } | |
circle { fill: #933; stroke-width: 2px; stroke: #fff; } | |
</style> | |
<script> | |
var canvas = d3.select("#area") | |
.append("svg:svg") | |
.attr("width", 600) | |
.attr("height", 400); | |
function getData() { | |
return d3.range(30).map(function() { | |
return { | |
x: Math.random()*600, | |
y: Math.random()*400, | |
r: Math.random()*60 | |
}; | |
}); | |
} | |
setInterval(function() { | |
var data = getData(); | |
var nodes = canvas.selectAll('g.node').data(data); | |
console.log(data); | |
// On change | |
nodes.select('circle') | |
.transition() | |
.duration(400) | |
.ease('bounce-in') | |
.attr('r', function(d) { return d.r; }) | |
.attr('cx', function(d) { return d.x; }) | |
.attr('cy', function(d) { return d.y; }); | |
// On enter | |
nodes.enter() | |
.append('svg:g') | |
.attr('class', 'node') | |
.append('svg:circle') | |
.attr('r', function(d) { return d.r; }) | |
.attr('cx', function(d) { return d.x; }) | |
.attr('cy', function(d) { return d.y; }); | |
nodes.exit() | |
.remove(); | |
}, 1000); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment