Javascript d3:仅在力定向图中将类应用于新节点
我有一个带有力定向图的动态模拟 我希望实现所有新节点都以某种颜色显示,而现有节点都具有相同的其他颜色(由id标识的节点) 在每次迭代中(我想是这样),我会将一个类应用于现有节点,并在“enter”选项中将新节点仅设置为Javascript d3:仅在力定向图中将类应用于新节点,javascript,d3.js,d3-force-directed,Javascript,D3.js,D3 Force Directed,我有一个带有力定向图的动态模拟 我希望实现所有新节点都以某种颜色显示,而现有节点都具有相同的其他颜色(由id标识的节点) 在每次迭代中(我想是这样),我会将一个类应用于现有节点,并在“enter”选项中将新节点仅设置为new node类: restart() { // Update and restart the simulation. node = node.data(nodes); // Remove all old nodes node.
new node
类:
restart() {
// Update and restart the simulation.
node = node.data(nodes);
// Remove all old nodes
node.exit().remove();
// Apply class "existing-node" to all existing nodes
node.attr("class","existing");
// Apply to all new nodes (enter selection)
node = node
.enter()
.append("circle")
.attr("r", rad)
.attr("class", "new-node")
对于输入和更新选择,应该使用不同的变量。现在,您正在覆盖
节点
。使用nodeEnter=node.enter().append(“circle”).etc.
.hmmm.。这听起来是一个查看@GerardoFurtado的好方法。我会看看我是否能重构它。谢谢事实上……记录在案:这段代码很好。我的代码中有一个问题,它阻止了预期的行为。谢谢@GerardoFurtado