g
親ノード (SVG要素など)で発生したデータへの変更をその子ノード (SVG 要素など) に渡す最善の方法がわかりませんcircle
。
これは最小限の作業例です。svg
この例では、SVG 要素を含む d3 セレクションを参照するというオブジェクトがあることを前提としています。
data = [{"id":"A","name":"jim"},{"id":"B","name":"dave"},{"id":"C","name":"pete"}];
g = svg.selectAll("g").data(data, function(d) { return d.id; }).enter().append("g");
g.append("circle")
.attr("r", 3)
.attr("cx", 100)
.attr("cy", function(d,i) {return 100 + (i * 30);})
// The data gets passed down to the circles (I think):
console.log("circle data:");
d3.selectAll("g circle").each(function(d) { console.log(d.name); });
// Now change the data, and update the groups' data accordingly
data = [{"id":"A","name":"carol"},{"id":"B","name":"diane"},{"id":"C","name":"susan"}];
svg.selectAll("g").data(data, function(d) { return d.id;});
// These are the results of the change:
console.log("after change, the group has:");
d3.selectAll("g").each(function(d) { console.log(d.name); });
console.log("but the circles still have:");
d3.selectAll("g circle").each(function(d) { console.log(d.name); });
グループのすべての子要素に新しい名前を付ける簡潔な方法を見つけるのを手伝ってくれる人はいますか? 私の実際の例では、それぞれg
に多くcircle
の が含まれています。