简体   繁体   English

d3.js v4:尝试修复图节点的子集

[英]d3.js v4: Trying to fix a subset of graph nodes

I would like to set some nodes of a graph fixed. 我想设置固定的图节点。 For this I have an array with node names which should be fixed: 为此,我有一个数组,其节点名称应固定:

selectedNodes = ["id0","id4","ide7"];

Next, I have the complete set of nodes: 接下来,我有完整的节点集:

allNodes = graph.data;

Following is my attempt to change the state. 以下是我尝试更改状态的尝试。

for (var i = 0; i < allNodes.length; i++) {
    d3.select(allNodes[i]);
    if (nodeList.indexOf(allNodes[i].id > -1)) {
        allNodes[i].fixed = true;
    } else {
        allNodes[i].fixed = false;  
    };
}

This does not work. 这是行不通的。 And I have hit a dead end. 我已经死胡同了。 Does anybody know what is going on? 有人知道发生了什么吗? How can I set only fix a certain of number nodes? 如何设置仅修复特定数量的节点?

You should not change the data array (besides that, using d3.select to manipulate the data array doesn't make sense). 您不应该更改数据数组(除此之外,使用d3.select操作数据数组没有意义)。 Instead of that, change the bound datum . 取而代之的是更改绑定的基准

Since you didn't provide your force layout, I'll use this one from Bostock, which uses D3 v3, like yours. 由于您未提供您的部队布局,因此我将使用Bostock的这一布局,后者像您一样使用D3 v3。

I'll fix the positions of three Les Miserables' characters: 我将修复三个悲惨世界角色的位置:

var selectedNodes = ["Brujon", "Gervais", "Javert"]; 

To do that, we change the datum in an each : 为此,我们在each更改原点:

node.each(function(d) {
    if (selectedNodes.indexOf(d.name) > -1) {
        d.fixed = true;
    }
})

And here is the code with that change: https://bl.ocks.org/anonymous/7b02643ba27e5323d30447579a38f58a/2ef2fb7568021a1082aa959891a2d05fab6bad9e 这是发生更改的代码: https : //bl.ocks.org/anonymous/7b02643ba27e5323d30447579a38f58a/2ef2fb7568021a1082aa959891a2d05fab6bad9e

You can see that those three are fixed now. 您可以看到这三个现在已修复。

Here is the code. 这是代码。

Kind regards, Markus 亲切的问候,马库斯

<script src="//d3js.org/d3.v3.min.js"></script>
<script>

    var width = 960,
    height = 500;

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var force = d3.layout.force()
    .gravity(0.05)
    .distance(100)
    .charge(-100)
    .size([width, height]);

d3.json("graph.json", function(error, json) {
  if (error) throw error;

  force
      .nodes(json.nodes)
      .links(json.links)
      .start();

  var link = svg.selectAll(".link")
      .data(json.links)
    .enter().append("line")
      .attr("class", "link");

  var node = svg.selectAll(".node")
      .data(json.nodes)
    .enter().append("g")
      .attr("class", "node")
      .call(force.drag);

  node.append("image")
      .attr("xlink:href", "https://github.com/favicon.ico")
      .attr("x", -8)
      .attr("y", -8)
      .attr("width", 16)
      .attr("height", 16);

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

  svg.on("dblclick", function(d) {
     var selectedNodes = ["Brujon", "Gervais", "Javert"];

     node.each(function(d){
           if(selectedNodes.indexOf(d.name) > -1){
        d.fixed = true
        console.log(d)
        }

      force
      .nodes(json.nodes)
      .links(json.links)
      .start();


    createLayout(force, link, node);

  })


  });

  createLayout(force, link, node);

});

function createLayout(force, link, node) {


  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
  });


};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM