d3 を使用して、ページ上の円の位置と数を動的に更新したいとします。.data()、.enter()、.exit() パターンを使用してこれを行うことができます。これが実際の例です。 http://jsfiddle.net/csaid/MFBye/6/
function updatePositions(data) {
var circles = svg.selectAll("circle").data(data);
circles.enter().append("circle");
circles.exit().remove();
circles.attr("r", 6)
.attr("cx", 50)
.attr("cy", function (d) {
return 20 * d
});
}
ただし、円の代わりに外部 SVG で同じことをしようとすると、最初の更新後の新しいデータ ポイントの多くがページに表示されません。例: http://jsfiddle.net/csaid/bmdQz/8/
function updatePositions(data) {
var gs = svg.selectAll("g")
.data(data);
gs.enter().append("g");
gs.exit().remove();
gs.attr("transform", function (d, i) {
return "translate(50," + d * 20 + ")";
})
.each(function (d, i) {
var car = this.appendChild(importedNode.cloneNode(true));
d3.select(car).select("path")
});
}
これは、外部 SVG オブジェクトを追加するために使用される .each() と関係があると思われますが、これを回避する方法がわかりません。また、「cx」および「cy」属性は円に固有のものであるため、外部 SVG にどのように使用できるかはわかりません。
前もって感謝します!