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

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

本文关键字:节点 连接 获取 csv 显示 关系 文件      更新时间:2024-06-21

我有一个大的csv文件,其格式为:

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

我在d3力布局中使用这些数据,如图所示。获取服务器和应用程序之间关系的关键代码是:

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 });
}

生成用于创建节点的服务器和应用程序的唯一列表的单独列表(edges)以及具有服务器和应用之间的关系并用于创建链接节点的线的单独列表。

我希望能够根据运行在服务器节点上的应用程序的数量来设置服务器节点的半径。我很难想出一种优雅的方式来在当前系统中获取和存储这些信息。d3中是否已经有什么可以在这里提供帮助,或者有人能在当前代码的情况下找到实现这一点的方法吗?

更新的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;
  })

基本上浏览所有的链接,并检查有多少链接到所选节点,如下所示:

.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++
    }
  })
})

然后使用这个值:d.amountOfNeighbours作为半径。我把它乘以2,因为这个值很小:)

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

希望对有所帮助