Skip to content

Instantly share code, notes, and snippets.

@djodjoni
Forked from rstacruz/index.html
Created December 7, 2017 15:35
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 djodjoni/1922c1117fe0727b74076c72c9100332 to your computer and use it in GitHub Desktop.
Save djodjoni/1922c1117fe0727b74076c72c9100332 to your computer and use it in GitHub Desktop.
D3 bubbles
<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