鑑於以下佈局:D3 V4更新,模式的組
<g>
... // many nodes
<g>
<circle></circle>
<text></text>
</g>
...
</g>
如何將正確的更新模式看起來像D3 V4? (?) 我有什麼在合併作爲參數使用,多久我得打電話合併
我創建了小提琴的工作示例(僅在節點節點+圓圈+文?):https://jsfiddle.net/cvvfsg97/6/
代碼:
function update(items) {
node = nodeLayer.selectAll(".node")
.data(items, function(d) { return d.id; })
node = node.enter() // insert
.append("g")
.attr("class", "node");
node.append("circle") // insert
.attr("r", 2.5)
.attr('class', 'circle')
.merge(nodeLayer.selectAll('.node > circle')) // is this correct?! // merge
.attr('fill', 'red') // just for testing purposes
.exit().remove(); // exit
node.append("text") // insert
.attr("dy", 3)
.text(function(d) { return d.name; })
.merge(nodeLayer.selectAll('.node > text')) // is this correct?! // merge
.attr('fill', 'green') // just for testing purposes
.exit().remove();
node.merge(nodeLayer.selectAll('.node')) // is this correct?! // merge
.attr('class', 'anotherClass')
.exit().remove(); // does not work // exit
}
有人能帶來一些清晰度如何使用輸入(條款),合併(),在組退出()?
我很可能喜歡在每個階段對每個元素進行更改。
更新:我簡單的例子,我不需要鏈接或力佈局。我的問題只是關於更新模式,而不是關於力量。更新後的jsfiddle沒有強制佈局。
你見過https://bl.ocks.org/mbostock/3808218 ?它已更新合併 – softwarenewbie7331
@ Frame91,我很高興你意識到你有兩個不同的問題在這裏:一個關於更新模式本身和其他如何更新部隊。他們是完全不同的問題。現在的問題是,解決您的一般問題(更新模式)的任何答案都無法正確解決部隊問題,因爲您現在有兩個答案。因此,我建議您刪除此問題併發布另一個問題,關於如何爲包含文本和圈子的組創建更新模式(並且將很容易回答)。 –
我對力量佈局不感興趣。我只關心更新模式。我只是在jsfiddle上使用了一個現有的例子,它恰好有一個強制佈局。對於混淆抱歉;) – Frame91