D3.js 如何更新D3中的特定节点

D3.js 如何更新D3中的特定节点,d3.js,D3.js,我正在更新节点和链接。我还想更改现有节点(圆)的半径。我如何更新一个特定的圆圈 当我在下面这样做时,它会更新所有圆圈 node.select("circle").attr("r", circleradius); 当我更新节点数组时,viz中没有任何变化 nodes[index].size = 2000; 这是函数更新代码: function update() { // Restart the force layout. force

我正在更新节点和链接。我还想更改现有节点(圆)的半径。我如何更新一个特定的圆圈

  • 当我在下面这样做时,它会更新所有圆圈

    node.select("circle").attr("r", circleradius); 
    
  • 当我更新节点数组时,viz中没有任何变化

    nodes[index].size = 2000;
    

这是函数更新代码:

    function update() {


        // Restart the force layout.
        force
            .nodes(nodes)
            .links(links)
            .start();


        // Update links.
        link = link.data(links, function(d) { return d.target.id; });

        link.exit().remove();

        link.enter().insert("line", ".node")
            .attr("class", "link");

        // Update nodes.
        node = node.data(nodes, function(d) { return d.id; });

        node.exit().remove();

        var nodeEnter = node.enter().append("g")
            .attr("class", "node")
            .on("click", click)
            .call(force.drag); 

        nodeEnter.append("circle")
                .attr("r", function(d) {   return Math.sqrt(d.size) / 2 || 24.5; });

        nodeEnter.append("text")
                      .attr("dy", ".35em")
                      .text(function(d) { return d.name; });

        node.select("circle")
            .style("fill", color);

    }

在使用d3.select之前,子元素不会自动更新。这是一件有点难的事,我不想在这里讨论

但出于实际目的,您需要做的是,在创建了强制导向布局和元素等之后,如果要更改用于大小元素0的数据值并看到其更新,则代码需要如下所示:

 nodes[0].size = 2000;
 d3.selectAll("g").select("circle")
  .attr("r", function(d) {   return Math.sqrt(d.size) / 2 || 24.5; });