Data-driven modification of selection.
From D3 in Depth book by Peter Cook.
license: gpl-3.0 | |
height: 170 | |
border: no |
Data-driven modification of selection.
From D3 in Depth book by Peter Cook.
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<head> | |
<title>Data-driven modification of selection</title> | |
</head> | |
<style> | |
circle { | |
fill: #ddd; | |
} | |
circle.high { | |
fill: orange; | |
} | |
</style> | |
<body> | |
<svg width="760" height="140"> | |
<g transform="translate(70, 70)"> | |
<circle /> | |
<circle /> | |
<circle /> | |
<circle /> | |
<circle /> | |
</g> | |
</svg> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script> | |
<script> | |
var myData = [ 10, 40, 20, 30, 50 ]; | |
var s = d3.selectAll('circle'); | |
s.data(myData); | |
s.attr('r', function(d) { | |
return d; | |
}) | |
.classed('high', function(d) { | |
return d >= 40; | |
}) | |
.attr('cx', function(d, i) { | |
return i * 120; | |
}); | |
</script> | |
</body> | |
</html> |