Skip to content

Instantly share code, notes, and snippets.

@d3noob d3noob/index.html
Last active Aug 29, 2015

What would you like to do?
Interactive text rotation with d3.js

This is a simple example of implimenting an HTML input using a <range> input tag and using that to adjust a d3.js drawn svg element (rotate 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 the text
.style("fill", "black")
.style("font-size", "56px")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.attr("transform", "translate(300,150) rotate(0)")
// when the input range changes update the angle"#nAngle").on("input", function() {
// Initial starting angle of the text
// update the element
function update(nAngle) {
// adjust the text on the range slider"#nAngle-value").text(nAngle);"#nAngle").property("value", nAngle);
// rotate the text"text")
.attr("transform", "translate(300,150) rotate("+nAngle+")");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.