Skip to content

Instantly share code, notes, and snippets.

@mbostock mbostock/adopt.diff
Created Nov 14, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.