Skip to content

Instantly share code, notes, and snippets.

@CoreyBurkhart
Last active March 26, 2017 18:40
Show Gist options
  • Save CoreyBurkhart/d42bfa0466ad6a7eb9e0095737a11952 to your computer and use it in GitHub Desktop.
Save CoreyBurkhart/d42bfa0466ad6a7eb9e0095737a11952 to your computer and use it in GitHub Desktop.
Click Transition
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.circle, .updated {
fill: red;
}
</style>
</head>
<body>
<script>
// Feel free to change or delete any of the code you see in this editor!
var width = 900,
height = 500,
data = [50, 100, 150, 200, 250, 300, 350, 400];
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.on('click', triggerTransition.bind(this))
var t = d3.transition()
.duration(200)
.ease(d3.easeLinear)
var g = svg.append('g')
.attr('class', 'group')
.selectAll('.circle')
.data(data)
.enter()
.append('circle')
.attr('class', 'circle')
.attr('r', 20)
.attr('cx', width / 8)
.attr('cy', function(d) {return d})
function triggerTransition() {
data = [50, 100, 150, 200];
d3.selectAll('.circle')
.data(data) // update
.attr('class', 'updated')
.exit() //exit and remove w/ transition
.transition(t)
.delay(function(d, i){return i * 400})
.attr('cx', 600)
.remove()
d3.selectAll('.updated')
.transition(t)
.delay(function(d, i){return i * 200})
.style('fill', 'blue') //update data that will stay
// .attr('cy', function(d){return d* 2})
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment