Skip to content

Instantly share code, notes, and snippets.

@mbostock
Created November 14, 2011 21:45
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 mbostock/1365279 to your computer and use it in GitHub Desktop.
Save mbostock/1365279 to your computer and use it in GitHub Desktop.
d3.create + selection.adopt
diff --git a/src/core/create.js b/src/core/create.js
new file mode 100644
index 0000000..a284a67
--- /dev/null
+++ b/src/core/create.js
@@ -0,0 +1,5 @@
+d3.create = function(name) {
+ return d3.select((name = d3.ns.qualify(name)).local
+ ? document.createElementNS(name.space, name.local)
+ : document.createElement(name));
+};
diff --git a/src/core/selection-adopt.js b/src/core/selection-adopt.js
new file mode 100644
index 0000000..d7eb884
--- /dev/null
+++ b/src/core/selection-adopt.js
@@ -0,0 +1,5 @@
+d3_selectionPrototype.adopt = function(selection) {
+ return this.select(function(d, i, j) {
+ return (d = selection[i]) && (d = d[j]) && this.appendChild(d);
+ });
+};
diff --git a/src/core/selection-select.js b/src/core/selection-select.js
index fea22b5..d7a65df 100644
--- a/src/core/selection-select.js
+++ b/src/core/selection-select.js
@@ -12,7 +12,7 @@ d3_selectionPrototype.select = function(selector) {
subgroup.parentNode = (group = this[j]).parentNode;
for (var i = -1, n = group.length; ++i < n;) {
if (node = group[i]) {
- subgroup.push(subnode = selector.call(node, node.__data__, i));
+ subgroup.push(subnode = selector.call(node, node.__data__, i, j));
if (subnode && "__data__" in node) subnode.__data__ = node.__data__;
} else {
subgroup.push(null);
diff --git a/src/core/selection-selectAll.js b/src/core/selection-selectAll.js
index c133c63..fb58ce0 100644
--- a/src/core/selection-selectAll.js
+++ b/src/core/selection-selectAll.js
@@ -8,7 +8,7 @@ d3_selectionPrototype.selectAll = function(selector) {
for (var j = -1, m = this.length; ++j < m;) {
for (var group = this[j], i = -1, n = group.length; ++i < n;) {
if (node = group[i]) {
- subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i)));
+ subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i, j)));
subgroup.parentNode = node;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment