简体   繁体   English

如何在D3.js Force Directed Graph中设置焦点节点?

[英]How do I set the focal node in a D3.js Force Directed Graph?

I have a data set that defines a number of nodes to use in a Force Directed Graph. 我有一个数据集,定义了一个强制定向图中使用的节点数。 It looks like... 看起来像...

  var nodeSet = [
    {id: "N1", name: "Node 1", type: "Type 1", hlink: "http://www.if4it.com"},
    {id: "N2", name: "Node 2", type: "Type 3", hlink: "http://www.if4it.com/glossary.html"},
    {id: "N3", name: "Node 3", type: "Type 4", hlink: "http://www.if4it.com/resources.html"},
    {id: "N4", name: "Node 4", type: "Type 5", hlink: "http://www.if4it.com/taxonomy.html"},
    {id: "N5", name: "Node 5", type: "Type 1", hlink: "http://www.if4it.com/disciplines.html"}
  ];

How do I specifically tell the force.layout in the d3.js library to use "Node 1" of id = "N1" as the primary root or focal node? 我如何具体告诉d3.js库中的force.layout使用id =“N1”的“节点1”作为主要根或焦点节点?

If you only want a root node you can have a root property in your object and set it to true, than treat that node separately. 如果只需要根节点,则可以在对象中具有root属性并将其设置为true,而不是单独处理该节点。 You can also set this root to center. 您也可以将此根设置为居中。 Here is how we did it (d3 + Prototype - at the time - now switching to d3+jQuery+underscore): 以下是我们如何做到的(d3 + Prototype - 当时 - 现在切换到d3 + jQuery +下划线):

getCenter: function() {
    var center = {
        x : this.width / 2,
        y : this.height / 2
    };
    return center;
}

//later do something like this in your init method:
var first = {
                id : id,
                name : name,
                x : this.getCenter().x,
                y : this.getCenter().y,
                root : true,
                //other properties
            };

//later in your redraw() or other methods you might employ...
//try to find the root node later in the code using something like:
var rootNode = this.nodes.detect(function(node) {
    return node.root;
});

//do something to the root node after you have detected it...
if (rootNode) {
    rootNode.x = rootNode.px = this.getCenter().x;
    rootNode.y = rootNode.py = this.getCenter().y;
    //some other stuff...
}

This is how we have done it. 这就是我们如何做到的。 However it's not clear to me what are the links in your example... A little bit puzzled. 然而,我不清楚你的例子中的链接是什么......有点困惑。 As you will notice, for a force directed layout or more complicated animations you almost always need to use D3+something else (Prototype, jQuery, underscore) for the simple methods like detect or include or other similar Ruby style methods. 正如您将注意到的,对于强制定向布局或更复杂的动画,您几乎总是需要使用D3 +其他东西(Prototype,jQuery,下划线)来处理诸如detect或include等简单方法或其他类似的Ruby样式方法。

I'm not sure what you mean by focal or root node. 我不确定你的焦点或根节点是什么意思。 If you want to fix a certain node in a specific place (eg the centre) you can set its 'fixed' attribute to true. 如果要在特定位置(例如中心)修复某个节点,可以将其“固定”属性设置为true。 For more info see Fix Node Position in D3 Force-Directed Layout , and also Moving fixed nodes in D3 . 有关详细信息,请参阅D3强制定向布局中的修复节点位置 ,以及D3中的移动固定节点

I dont think your force directed radial graph example does show that d3 implicitly uses the first node in the node list as the 'root' node. 我不认为你的力导向径向图示例确实显示d3隐式使用节点列表中的第一个节点作为“根”节点。 Node 1 in your example always gravitates towards the centre as a consequence of the network structure. 由于网络结构的原因,示例中的节点1总是倾向于中心。 If you place Node 1 later in the array of nodes its behaviour is the same. 如果稍后将节点1放在节点数组中,则其行为是相同的。

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

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