Last active
December 27, 2015 20:59
-
-
Save rand0m86/7388029 to your computer and use it in GitHub Desktop.
General update pattern D3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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