Skip to content

Instantly share code, notes, and snippets.

@rubillionaire
Last active December 31, 2015 12:39
Show Gist options
  • Save rubillionaire/7987958 to your computer and use it in GitHub Desktop.
Save rubillionaire/7987958 to your computer and use it in GitHub Desktop.
d3.js interpolating numbers as text.
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel='stylesheet' type='text/css' href='' />
<meta http-equiv='content-type' content='text/html; charset=utf-8' />
<meta name='viewport' content='initial-scale=1.0 maximum-scale=1.0'>
<style>
.count {
position: relative;
margin: 80px auto;
font-size: 300px;
text-align: center;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
var data = [{
start: 1000,
end: 2000
}, {
start: 2000,
end: 1000
}];
var format = d3.format(',');
var count_sel = d3.select('body')
.selectAll('.count')
.data(data)
.enter()
.append('div')
.attr('class', 'count')
.text(function (d) {
return format(d.start);
})
count_sel
.transition()
.duration(2000)
.tween('text', function (d) {
var i = d3.interpolateRound(d.start, d.end);
return function (t) {
this.textContent = format(i(t));
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment