在多焦点 d3 力布局中重新定位节点

Reposition nodes in a multi-foci d3 force layout

我在多焦点力布局中有三组节点。每个节点都已在 HTML.

中呈现

这是我的强制布局代码的样子:

var node = this.svg.selectAll('path')
    .data(data);

// foci is a dictionary that assigns the x and y value based
// on what group a node belongs to.
var foci = {
    "Blue" : {
         "x" : xScale(0),
         "y": height / 2
    },
    "Red": {
         "x" : xScale(1),
         "y": height / 2
    },
    "Purple": {
         "x" : xScale(2),
         "y": height / 2
    },
};

// This helped me position the nodes to their assigned clusters.
var forceX = d3.forceX((d) => foci[d.group].x);
var forceY = d3.forceY((d) => foci[d.group].y);

var force = d3.forceSimulation(data)
    .force('x', forceX)
    .force('y', forceY)
    .force("collide", d3.forceCollide(8))
    .on('tick', function() {
         node
            .attr('transform', (d) => {
                return 'translate(' + (d.x - 100) + ',' + (-d.y + 25) + ')';
            });
        });

到目前为止,我能够完成的是根据下拉列表中的更改重新绘制布局,这会重新初始化 d3.forceSimulation() 并使群集快速回到页面上,如您在 gif 中所见下面。

那不是我想要的。我正在努力使重新排列尽可能无缝。

更新:通过不重新初始化 d3.forceSimulation(),我可以将新数据绑定到节点并更改它们的颜色。

您可以简单地重新加热模拟,而不是重新初始化d3.forceSimulation(),使用restart():

Restarts the simulation’s internal timer and returns the simulation. In conjunction with simulation.alphaTarget or simulation.alpha, this method can be used to “reheat” the simulation during interaction, such as when dragging a node, or to resume the simulation after temporarily pausing it with simulation.stop.

我使用您的部分代码创建了一个演示来向您展示。在此演示中,按钮随机化每个数据点的颜色。之后,我们重新加热模拟:

force.alpha(0.8).restart();

检查一下,点击"Randomize":

var width = 500, height = 200;

var svg = d3.select("#svgdiv")
 .append("svg")
 .attr("width", width)
 .attr("height", height);
 
var data = d3.range(100).map(function(d, i){
 return {
 group: Math.random()*2 > 1 ? "blue" : "red",
 id: i
 }
});

var xScale = d3.scaleOrdinal()
 .domain([0, 1])
 .range([100, width-100]);

var foci = {
    "blue" : {
         "x" : xScale(0),
         "y": height / 2
    },
    "red": {
         "x" : xScale(1),
         "y": height / 2
    }
};

var forceX = d3.forceX((d) => foci[d.group].x);
var forceY = d3.forceY((d) => foci[d.group].y);

var node = svg.append("g")
            .attr("class", "nodes")
            .selectAll("circle")
            .data(data)
            .enter().append("circle")
            .attr("r", 5)
      .attr("fill", (d)=>d.group);


var force = d3.forceSimulation(data)
    .velocityDecay(0.65)
    .force('x', forceX)
    .force('y', forceY)
    .force("collide", d3.forceCollide(8));
  
force.nodes(data)
    .on('tick', function() {
         node
            .attr('transform', (d) => {
                return 'translate(' + (d.x) + ',' + (d.y) + ')';
            });
        });
    
d3.select("#btn").on("click", function(){
    data.forEach(function(d){
  d.group = Math.random()*2 > 1 ? "blue" : "red"
  })
  node.transition().duration(500).attr("fill", (d)=>d.group);
  setTimeout(function(){
  force.nodes(data);
  force.alpha(0.8).restart();
  }, 1500)
})
<script src="https://d3js.org/d3.v4.min.js"></script>
<button id="btn">Randomize</button>
<div id="svgdiv"><div>

PS:我把 reheat 放在 setTimeout 里面,所以你可以先看到圆圈改变颜色,然后移动到焦点职位。