Skip to content

Instantly share code, notes, and snippets.

Forked from mbostock/.block
Last active June 5, 2023 14:25
Show Gist options
  • Save mbostock/3808221 to your computer and use it in GitHub Desktop.
Save mbostock/3808221 to your computer and use it in GitHub Desktop.
General Update Pattern, II
license: gpl-3.0

By adding a key to the data-join, letters that are already displayed are put in the update selection. Now updates can occur anywhere in the array, depending on the overlap between the old letters and the new letters. The text content only needs to be set on enter because the mapping from letter to element never changes; however, the x-position of the text element must now be recomputed on update as well as enter.

It'll be easier to see what's going on when we add animated transitions next!

Next: Update Transitions

Previous: General Update Pattern

<!DOCTYPE html>
<meta charset="utf-8">
text {
font: bold 48px monospace;
.enter {
fill: green;
.update {
fill: #333;
<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) {
// Join new data with old elements, if any.
var text = g.selectAll("text")
.data(data, function(d) { return d; });
// Update old elements as needed.
text.attr("class", "update");
// Create new elements as needed.
// After merging the entered elements with the update selection,
// apply operations to both.
.attr("class", "enter")
.attr("dy", ".35em")
.text(function(d) { return d; })
.attr("x", function(d, i) { return i * 32; });
// Remove old elements as needed.
// 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