Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active September 25, 2020 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save d3indepth/3236a4658005ab78d60816b74830694b to your computer and use it in GitHub Desktop.
Save d3indepth/3236a4658005ab78d60816b74830694b to your computer and use it in GitHub Desktop.
Modifying a selection
license: gpl-3.0
height: 180
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Selecting circles</title>
</head>
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 14px;
}
circle {
fill: #ddd;
}
circle.selected {
stroke: #555;
stroke-width: 2px;
}
</style>
<body>
<div class="title"></div>
<svg width="760" height="140">
<g transform="translate(70, 70)">
<circle r="40" />
<circle r="40" cx="120" />
<circle r="40" cx="240" />
<circle r="40" cx="360" />
<circle r="40" cx="480" />
</g>
</svg>
<form>
<input class="robot-checkbox" type="checkbox"> A checkbox</input>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
// Change colour of first circle
d3.select('circle')
.style('fill', 'orange')
// Change radius of all circles
d3.selectAll('circle')
.attr('r', 20);
// Add .selected class to 3rd circle
d3.select('circle:nth-child(3)')
.classed('selected', true);
// Set checked property of checkbox
d3.select('input.robot-checkbox')
.property('checked', true);
// Set text on .title element
d3.select('.title')
.text('D3 in Depth selection example')
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment