Skip to content

Instantly share code, notes, and snippets.

@d3noob
Last active November 28, 2019 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d3noob/c4b31a539304c29767a56c2373eeed79 to your computer and use it in GitHub Desktop.
Save d3noob/c4b31a539304c29767a56c2373eeed79 to your computer and use it in GitHub Desktop.
Range input with v5
license: mit

This is a demonstration of the use of the html 'range' input using d3.js v5.

This graph is part of the code samples for the update to the book D3 Tips and Tricks to version 5 of d3.js.

<!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="150" id="nRadius">
</p>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script>
var width = 600;
var height = 300;
var holder = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height);
// draw the circle
holder.append("circle")
.attr("cx", 300)
.attr("cy", 150)
.style("fill", "none")
.style("stroke", "blue")
.attr("r", 120);
// when the input range changes update the circle
d3.select("#nRadius").on("input", function() {
update(+this.value);
});
// Initial starting radius of the circle
update(120);
// 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 rircle radius
holder.selectAll("circle")
.attr("r", nRadius);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment