Skip to content

Instantly share code, notes, and snippets.

@tgk
Last active December 18, 2015 07:09
Show Gist options
  • Save tgk/5744921 to your computer and use it in GitHub Desktop.
Save tgk/5744921 to your computer and use it in GitHub Desktop.
Circular buttons

The circular buttons use callback to update which of the two is selected.

<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
function update(selected)
{
// BUTTONS
var button = svg.selectAll("circle").data(["A", "B"]);
// enter
button.enter().append("circle")
.attr("cx", function(d, i) { return (i + 1) * 128; })
.attr("cy", 128)
.attr("r", 60)
.style("stroke", "black")
.style("stroke-width", 3);
// update
button
.style("fill",
function(d) {
if (d == selected)
return "rgb(250,0,0)";
else
return "rgb(128,128,128)";
})
// clicks
button
.on("mouseup", update);
}
update("A");
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment