Skip to content

Instantly share code, notes, and snippets.

Forked from mbostock/.block
Created October 1, 2012 03:20
Show Gist options
  • Save christophermanning/3809302 to your computer and use it in GitHub Desktop.
Save christophermanning/3809302 to your computer and use it in GitHub Desktop.
General Update Pattern, III

By adding transitions, we can more easily follow the elements as they are entered, updated and exited. Separate transitions are defined for each of the three states.

Note that no transition is applied to the merged enter + update selection; this is because it would supersede the transition already scheduled on entering and updating elements. It's possible to schedule concurrent elements by using transition.transition or by setting, but it's simpler here to only transition the x-position on update; for entering elements, the x-position is assigned statically.

Want to read more? Try these tutorials:

See the D3 wiki for even more resources.

Previous: Key Functions

<!DOCTYPE html>
<meta charset="utf-8">
text {
font: bold 48px monospace;
.enter {
fill: green;
.update {
fill: #333;
.exit {
fill: brown;
<script src=""></script>
var d3_committer_index = 0;
// git shortlog -s | sort -rn
var d3_committers = [
'Mike Bostock',
'Jason Davies',
'Michael Bostock',
'Kristofer Monisit',
'Lars Kotthoff',
'Jeffrey Heer',
'Nelson Minar',
'Trevor Norris',
'Nathan Vander Wilt',
'Peter Woodman',
'Mitsutoshi Aoe',
'Steven Noble',
'David Poncelow',
'Xavier Shay',
'Tim Branyen',
'Michael Meisel',
'Jon Seymour',
'Johan Sundström',
'Ian Malpass',
'chris viau',
'Andrew Sutherland',
'Zee Agency',
'Tommy Montgomery',
'Stephen Bannasch',
'Mike Bostock + Erica Kwan',
'Michael Jackson',
'Lachèze Alexandre',
'Kevin J. Lynagh',
'Jim Radford',
'Jan Alonzo',
'Ger Hobbelt',
'Gerard Hundman',
'George Adams',
'Dirk Bergstrom',
'Carlos Scheidegger',
'Brandon Liu',
'Andy Chong'
var width = 960,
height = 500;
var svg ="body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(32," + (height / 2) + ")");
function update(data) {
// Join new data with old elements, if any.
var text = svg.selectAll("text")
.data(data, function(d) { return d; });
// Update old elements as needed.
text.attr("class", "update")
.attr("x", function(d, i) { return i * 32; });
// Create new elements as needed.
.attr("class", "enter")
.attr("dy", ".35em")
.attr("y", -60)
.attr("x", function(d, i) { return i * 32; })
.style("fill-opacity", 1e-6)
.text(function(d) { return d; })
.attr("y", 0)
.style("fill-opacity", 1);
// Remove old elements as needed.
.attr("class", "exit")
.attr("y", 60)
.style("fill-opacity", 1e-6)
// The initial display.
// Grab a random sample of letters from the alphabet, in alphabetical order.
setInterval(function() {
update(d3_committers[++d3_committer_index % d3_committers.length].split(""));
}, 1500);
// Shuffles the input array.
function shuffle(array) {
var m = array.length, t, i;
while (m) {
i = Math.floor(Math.random() * m--);
t = array[m], array[m] = array[i], array[i] = t;
return array;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment