私はV4の一般的な更新パターンをうまく利用することに取り組んでいますが、私はmerge()
を使って新しい動作を理解していると思います。私は奇妙な結果を経験しています。ここに私の更新機能があります:D3.enter()。merge()not working
function update(data) {
//I create and empty selection, and use the key function with an ID string
//in my dsv data.
var bubs = dataLr.selectAll(".data-bub")
.data(data, function(d) {
return d.id || (d.id = ++i);
});
//remove any that dont need to be there.
//The first time around there wont be any nodes in the exit, but no matter.
bubs.exit()
.transition(tIntro)
.style("opacity", 0)
.each(function(d) {
//Log out the removed elements.
console.log(d.id);
})
.remove();
bubs.enter() //add any new ones - a group and a circle within it.
.append("g")
.classed("data-bub", true)
.on("click", function(d) {
console.log(d);
})
.append("circle")
.attr("r", 20)
.classed("bub", true)
.merge(bubs);//here I merge the enter with the update.
//Now I should be able to run this transition that
//moves each group to a random location on my svg.
//But nothing happens. Why is the update selection empty after merge?
bubs.transition(tIntro)
.attr("transform", function(d, i) {
///console.log(i);
var y = Math.random() * gHeight;
var x = Math.random() * gWidth;
return "translate(" + x + "," + y + ")";
});
}
さらに、別の奇妙なことが起こっています。現在、データを変更せずにこの機能を実行しています。したがって、exit()
またはenter()
で削除される要素はありません。しかし、2つのランダムなものが削除され、毎回再追加されますか?悪魔は何ですか?
を私はあなたの最初の提案を試みたが、変換は、円と大きくないに適用されていますグループ、狂気の結果。試してみてくださいv2。ありがとうございました。 – dgfx
はい、申し訳ありませんが、マージを使用していた選択が 'g'要素ではなく' circle'sの選択であることを忘れました。 v2は日付がついていて、ドキュメンテーションに欠けています。可能であれば、それを回避しようとします。 –
これは動作しています。各選択を変数に追加すると、それぞれの選択が不変であることがわかります。しかし私の第二の問題はまだ残っています。入力データが変更されていないにもかかわらず、この更新機能が実行されるたびに、2つの要素が追加および削除されています。起こっているべきことは、既存の要素の新しい場所への移行です。 – dgfx