Skip to content

Instantly share code, notes, and snippets.

@d3noob
Created September 15, 2020 06:08
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/6a00d052ef65f49cba7da2c1cc4c0796 to your computer and use it in GitHub Desktop.
Save d3noob/6a00d052ef65f49cba7da2c1cc4c0796 to your computer and use it in GitHub Desktop.
Number input in v6
license: mit

This is a demonstration of the use of the html 'number' input to rotate a text element using d3.js v6.

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

<!DOCTYPE html>
<meta charset="utf-8">
<title>Input (number) test</title>
<p>
<label for="nValue"
style="display: inline-block; width: 240px; text-align: right">
angle = <span id="nValue-value"></span>
</label>
<input type="number" min="0" max="360" step="5" value="0" id="nValue">
</p>
<script src="https://d3js.org/d3.v6.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 element
holder.append("text")
.style("fill", "black")
.style("font-size", "56px")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("transform", "translate(300,150) rotate(0)")
.text("d3noob.org");
// when the input range changes update value
d3.select("#nValue").on("input", function() {
update(+this.value);
});
// Initial update value
update(0);
// adjust the text
function update(nValue) {
// adjust the value
holder.select("text")
.attr("transform", "translate(300,150) rotate("+nValue+")");
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment