Skip to content

Instantly share code, notes, and snippets.

@jjlumagbas
Last active June 14, 2018 05:34
Show Gist options
  • Save jjlumagbas/2254768983992fc3e85895b5633f358c to your computer and use it in GitHub Desktop.
Save jjlumagbas/2254768983992fc3e85895b5633f358c to your computer and use it in GitHub Desktop.
Circle radius transition on change of slider
<!DOCTYPE html>
<meta charset="utf-8">
<title>Input test (circle)</title>
<p>
<label for="nRadius"
style="display: inline-block; width: 240px; text-align: right">
radius = <span id="nRadius-value"></span>
</label>
<input type="range" min="1" max="2" id="nRadius">
</p>
<script src="http://d3js.org/d3.v5.min.js"></script>
<script>
var data = [[50, 80], [80, 40]];
var width = 600;
var height = 300;
var holder = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
holder.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", function(d, i) { return 300 + (i * 150); })
.attr("cy", 150)
.style("fill", "none")
.style("stroke", "blue")
.attr("r", function(d) { return d[0]; });
// when the input range changes update the circle
d3.select("#nRadius").on("input", function() {
update(+this.value);
});
// Initial starting radius of the circle
update(1);
// update the elements
function update(nRadius) {
// adjust the text on the range slider
d3.select("#nRadius-value").text(nRadius);
d3.select("#nRadius").property("value", nRadius);
// update the circle radius
holder.selectAll("circle")
.data(data)
.transition()
.duration(1000)
.attr("r", function(d) { return d[nRadius - 1]; });
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment