Create a gist now

Instantly share code, notes, and snippets.

@mbostock /.block forked from mbostock/.block
Last active Nov 12, 2016

What would you like to do?
General Update Pattern, III
license: gpl-3.0

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. To avoid repeating transition timing parameters for the enter, update, and exit selections, a top-level transition t sets the duration, and then subsequent transitions use selection.transition, passing in t to inherit timing:

var t = d3.transition()

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;
<svg width="960" height="500"></svg>
<script src=""></script>
var alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
var svg ="svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g").attr("transform", "translate(32," + (height / 2) + ")");
function update(data) {
var t = d3.transition()
// JOIN new data with old elements.
var text = g.selectAll("text")
.data(data, function(d) { return d; });
// EXIT old elements not present in new data.
.attr("class", "exit")
.attr("y", 60)
.style("fill-opacity", 1e-6)
// UPDATE old elements present in new data.
text.attr("class", "update")
.attr("y", 0)
.style("fill-opacity", 1)
.attr("x", function(d, i) { return i * 32; });
// ENTER new elements present in new data.
.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);
// The initial display.
// Grab a random sample of letters from the alphabet, in alphabetical order.
d3.interval(function() {
.slice(0, Math.floor(Math.random() * 26))
}, 1500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment