Skip to content

Instantly share code, notes, and snippets.

Last active December 3, 2016 02:27
What would you like to do?
Input acting on two elements in v4

This is a demonstration of the use of the html 'range' input to rotate two text elements using d3.js v4.

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

<!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)+")");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment