Skip to content

Instantly share code, notes, and snippets.

@veltman
Last active July 3, 2016 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veltman/ad5aaa518266ead2cc55 to your computer and use it in GitHub Desktop.
Save veltman/ad5aaa518266ead2cc55 to your computer and use it in GitHub Desktop.
styleTween for percentages
<!DOCTYPE html>
<meta charset="utf-8">
<style>
div {
height: 10px;
background-color: steelblue;
}
div div {
background-color: tomato;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var data = [
[70,30],
[30,70]
];
var bars = d3.selectAll("div")
.data(data)
.enter()
.append("div")
.append("div")
.style("width",function(d){
return d[0]+"%";
});
bars.transition()
.duration(1000)
.ease("exp-out")
.styleTween("width",function(d){
return function(t) { return (d[0] * (1 - t) + d[1] * t)+"%"; };
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment