Skip to content

Instantly share code, notes, and snippets.

@mukhtyar
Forked from Fil/.block
Created October 7, 2016 16:32
Show Gist options
  • Save mukhtyar/de76aca14925fbad2f03b297e96e7e0a to your computer and use it in GitHub Desktop.
Save mukhtyar/de76aca14925fbad2f03b297e96e7e0a 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