Selection modification using functions.
From D3 in Depth book by Peter Cook.
license: gpl-3.0 | |
height: 220 | |
border: no |
Selection modification using functions.
From D3 in Depth book by Peter Cook.
<!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> |