Skip to content

Instantly share code, notes, and snippets.

Last active Nov 30, 2019
What would you like to do?
HTML input and multiple d3.js objects
license: mit

This is an example of implimenting an HTML input using a <range> input tag and using that to adjust multiple d3.js svg elements (spinning text!).

It is used as an example and described in the book D3 Tips and Tricks.

<!DOCTYPE html>
<meta charset="utf-8">
<title>Input test</title>
<label for="nAngle"
style="display: inline-block; width: 240px; text-align: right">
angle = <span id="nAngle-value">…</span>
<input type="range" min="0" max="360" id="nAngle">
<script src=""></script>
var width = 600;
var height = 300;
var holder ="body")
.attr("width", width)
.attr("height", height);
// draw d3.js text
.attr("class", "d3js")
.style("fill", "black")
.style("font-size", "56px")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("transform", "translate(300,55) rotate(0)")
// draw text
.attr("class", "d3noob")
.style("fill", "black")
.style("font-size", "56px")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("transform", "translate(300,130) rotate(0)")
// when the input range changes update the rectangle"#nAngle").on("input", function() {
// Initial starting height of the rectangle
// update the elements
function update(nAngle) {
// adjust the range text"#nAngle-value").text(nAngle);"#nAngle").property("value", nAngle);
// adjust d3.js text"text.d3js")
.attr("transform", "translate(300,55) rotate("+nAngle+")");
// adjust text"text.d3noob")
.attr("transform", "translate(300,130) rotate("+(360 - nAngle)+")");
Copy link

Keyzol commented Aug 2, 2017


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment