[英]Get the number of connected nodes from a csv file that shows their relations
我有一个格式较大的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;
})
希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.