Skip to content

Instantly share code, notes, and snippets.

Created January 21, 2016 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8ad986db8b3f5cc82e69 to your computer and use it in GitHub Desktop.
Save anonymous/8ad986db8b3f5cc82e69 to your computer and use it in GitHub Desktop.
selection.order
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</head>
<body>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.6.0"></script>
<script type="text/javascript">
d3.selection.prototype.order = function() {
for (var j = 0, m = this.length; j < m; j++) {
for (var group = this[j], i = 1, n = group.length, prev = group[0]; i < n; i++) {
var node = group[i];
if (node) {
if (prev) prev.parentNode.insertBefore(node, prev.nextSibling);
prev = node;
}
}
}
return this;
};
var div = d3.select("body").selectAll("div")
.data([0, 10, 11, 4, 6, 12])
.enter().append("div")
.text(String);
var div = d3.select("body").selectAll("div")
.data([0, 1, 2, 10, 11, 12], String);
div.enter().append("div")
.text(String);
div.order().exit().remove();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment