Skip to content

Instantly share code, notes, and snippets.

@lindep
Last active May 17, 2016 22:22
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 lindep/dac7cf301847b22758d8b9807eaecc6f to your computer and use it in GitHub Desktop.
Save lindep/dac7cf301847b22758d8b9807eaecc6f to your computer and use it in GitHub Desktop.
Add_elements_by_function
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
svg { width:100%; height: 100% }
</style>
</head>
<body>
<script>
var x = 10, y = 30, width = 600, height = 480;
function drawCircles(svg, data) {
var circles = svg.selectAll('circle').data(data);
circles.enter().append('circle')
.attr('r', 3);
circles.exit().remove();
circles
.attr('cx', function(d) { return d.x })
.attr('cy', function(d) { return d.y });
}
numPoints =272;
var data = [];
for (var i = 0; i < numPoints; i++) {
data.push({
x: Math.random() * (width-3) + (x),
y: Math.random() * (height-1) + (y)
});
}
var svg = d3.selectAll('svg').data([0]);
svg.enter().append("svg");
svg.append("rect")
.attr({x: x, y: y, width: width, height: height})
.style({ fill: "#a72d1a"})
.transition().duration(3000).ease("bounce")
.style({ fill: "#5db9e3"})
drawCircles(svg, data);
console.log(data);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment