Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active August 2, 2017 10: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 d3indepth/9c59fb1fe6e914538341308340296885 to your computer and use it in GitHub Desktop.
Save d3indepth/9c59fb1fe6e914538341308340296885 to your computer and use it in GitHub Desktop.
Selection modification using functions
license: gpl-3.0
height: 220
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Using the i argument</title>
</head>
<style>
rect {
fill: #ddd;
}
</style>
<body>
<svg width="760" height="190">
<g transform="translate(70, 20)">
<rect width="30" height="30" y="0" />
<rect width="30" height="30" y="40" />
<rect width="30" height="30" y="80" />
<rect width="30" height="30" y="120" />
</g>
</svg>
<div>
<button onClick="update();">Click to update rect elements using function(d, i)</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
function update() {
d3.selectAll('rect')
.attr('x', function(d, i) {
return i * 40;
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment