简体   繁体   English

从显示其关系的csv文件中获取连接的节点数

[英]Get the number of connected nodes from a csv file that shows their relations

I have a large csv file that is in the format: 我有一个格式较大的csv文件:

ServerID|AppID
   01   |  01
   01   |  02
   02   |  02
   02   |  03
   02   |  04
   03   |  04

I am using this data in a d3 force layout as shown in this plunker . 我在d3强制布局中使用此数据,如本插件所示。 The key bit of code to get the relations between the servers and apps (IE what apps are on what servers) is: 获取服务器和应用程序之间的关系(即什么应用程序在什么服务器上)的关键代码是:

links.forEach(function (link) {
    link.ApplicationName= nodeByName(link.ApplicationName, "A");
    link.Servername = nodeByName(link.Servername, "S");

    edges.push({ source: link.ApplicationName, target: link.Servername })
});

function nodeByName(name, SorA) {
    var groupNo = 0;
    switch (SorA) {
        case "A":
            groupNo = 1;
            break;
        case "S":
            groupNo = 2;
            break;
        default:
        groupNo = 0;
    }
    return nodesByNames[name] || (nodesByNames[name] = { Name: name, group: groupNo });
}

which generates a unique list of servers and apps to create the nodes, and a separate list ( edges ) which has the relations between the servers and apps and is used to create the lines linking the nodes. 它会生成一个用于创建节点的服务器和应用程序的唯一列表,以及一个单独的列表( edges ),该列表具有服务器和应用程序之间的关系,并用于创建链接节点的线。

I want to be able to set the radius of the server nodes based off the number of apps running on them. 我希望能够根据服务器节点上运行的应用程序的数量来设置其半径。 I am struggling to think of an elegant way to get and store this information in the current system. 我正在努力寻找一种优雅的方式来在当前系统中获取和存储此信息。 Is there anything in d3 already that can help here, or can someone see a way to do this given the current code? d3已经有什么可以帮助您解决此问题的,还是有人可以在当前代码下看到实现此目的的方法?

Updated plnkr : http://plnkr.co/edit/gtAcJinltdjkgu1MEPmY?p=preview 更新的plnkr: http ://plnkr.co/edit/gtAcJinltdjkgu1MEPmY?p=preview

var circles = node.append("circle")
  .each(function(d) {
    d.amountOfNeighbours = 0;
    link.each(function(e) {
      console.log(e)
      if (e.source.Name == d.Name) { //if the source of the link is this node, amountOfNeighbours++
        d.amountOfNeighbours++
      }
      if (e.target.Name == d.Name) { //if the target of the link is this node, amountOfNeighbours++
        d.amountOfNeighbours++
      }
    })
  })
  .attr("r", function(d) {
    return d.amountOfNeighbours * 2;
  })

Basically go through all the links for and check how many link to the selected node like so : 基本上遍历所有链接,然后检查到所选节点的链接数,如下所示:

.each(function(d) {
  d.amountOfNeighbours = 0; //set an attribute
  link.each(function(e) {
    console.log(e)
    if (e.source.Name == d.Name) { //if the source of the link is this node, amountOfNeighbours++
      d.amountOfNeighbours++
    }
    if (e.target.Name == d.Name) { //if the target of the link is this node, amountOfNeighbours++
      d.amountOfNeighbours++
    }
  })
})

And then use this value : d.amountOfNeighbours as the radius. 然后使用此值: d.amountOfNeighbours作为半径。 I multiplied it by 2 as the value was fairly small :) 我将其乘以2,因为该值很小:)

 .attr("r", function(d) {
   return d.amountOfNeighbours * 2;
 })

Hope that helps 希望能有所帮助

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

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