Skip to content

Instantly share code, notes, and snippets.

@Fil
Last active October 17, 2016 14:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Fil/4f937d85cbc10705bd1d7e7f45f4fcf2 to your computer and use it in GitHub Desktop.
Save Fil/4f937d85cbc10705bd1d7e7f45f4fcf2 to your computer and use it in GitHub Desktop.
.call(generalpattern)
license: mit

Ease up the pain with the general update pattern in d3v4.

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
textarea {
font-size: 14px;
font-family: monospace;
width: 100%;
height: 90%;
border: none;
margin-left: 2em;
padding-left: 2em;
border-left: #fee solid 4px;
}
</style>
</head>
<body>
<textarea>
function generalpattern(selection, cb) {
var enter = selection.enter();
if (cb.before) cb.before(selection);
if (cb.enter) enter = cb.enter(enter);
if (cb.exit) cb.exit(selection.exit());
if (cb.update) cb.update(selection.merge(enter));
}
// USAGE
svg.selectAll('path').data([…])
.call(generalupdate, {
enter: function (g) {
return g.append('path')
},
exit: function (g) {
g
.transition()
.remove()
},
update: function (g) {
g
.transition()
.attr('d', function (d) {})
},
// before: function(g) {…},
}
});</textarea>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment