Skip to content

Instantly share code, notes, and snippets.

@rand0m86
Last active December 27, 2015 20:59
Show Gist options
  • Save rand0m86/7388029 to your computer and use it in GitHub Desktop.
Save rand0m86/7388029 to your computer and use it in GitHub Desktop.
General update pattern D3
function update(data) {
// DATA JOIN
// Join new data with old elements, if any.
var text = svg.selectAll("text")
.data(data);
// UPDATE
// Update old elements as needed.
text.attr("class", "update");
// ENTER
// Create new elements as needed.
text.enter().append("text")
.attr("class", "enter")
.attr("x", function(d, i) { return i * 32; })
.attr("dy", ".35em");
// ENTER + UPDATE
// Appending to the enter selection expands the update selection to include
// entering elements; so, operations on the update selection after appending to
// the enter selection will apply to both entering and updating nodes.
text.text(function(d) { return d; });
// EXIT
// Remove old elements as needed.
text.exit().remove();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment