An extention of Mike Bostock's Text Transtion I that updates the data attribute instead of using regex on the formatted text.
Last active
February 24, 2017 18:39
-
-
Save easadler/0c77dac7f66f08d1c7842963921b0c32 to your computer and use it in GitHub Desktop.
Text Transition Using Datum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
h1 { | |
font: 400 120px/500px "Helvetica Neue"; | |
text-align: center; | |
width: 960px; | |
height: 500px; | |
margin: 0; | |
} | |
</style> | |
<h1>0</h1> | |
<script src="//d3js.org/d3.v4.0.0-alpha.23.min.js"></script> | |
<script> | |
var format = d3.format(",d"); | |
d3.select("h1").datum({value: 0}) | |
var values = d3.range(10).map(function() { | |
return Math.random() * 1e6; | |
}) | |
update(Math.random() * 1e6) | |
d3.interval(function(){ | |
var newValue = Math.random() * 1e6 | |
update(newValue) | |
}, 3000) | |
function update(newValue){ | |
d3.select("h1") | |
.transition() | |
.duration(2500) | |
.on("start", function repeat() { | |
d3.active(this) | |
.tween("text", function(d) { | |
var that = d3.select(this), | |
i = d3.interpolateNumber(d.value, newValue); | |
return function(t) { that.text(format(i(t))); }; | |
}) | |
}).on("end", function() { | |
d3.select(this).datum({value: newValue}) | |
}) | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment