Skip to content

Instantly share code, notes, and snippets.

@jbelmont
Last active September 19, 2017 02:50
Show Gist options
  • Save jbelmont/03048c2bf48778b1d605345d7fdd6965 to your computer and use it in GitHub Desktop.
Save jbelmont/03048c2bf48778b1d605345d7fdd6965 to your computer and use it in GitHub Desktop.
Enter and Append Circles
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.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 myData = [1,2,3,4,5];
var svgViewport = d3.select("body")
.append("svg")
.attr("width","560")
.attr("height","900");
// Update
var circleSelection = svgViewport.selectAll("circle").data(myData);
// Enter and Append
var circleElements = circleSelection.enter().append("circle");
// Set properties for each circle
circleElements
.attr("cx",function(d,i) { return d * 50; })
.attr("cy",function(d,i) { return d * 50; })
.attr("r","25");
function colorCircles(d,i) {
return i % 2 === 0 ? 'green' : 'red';
}
// Style each circle
var circleStyle = circleElements.style("fill", colorCircles);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment