[英]D3 javascript network: How to parametrize node size with the number of links?
我想用d3.js创建一个网络。 我已经创建了一个,如下所示,但是我希望节点的大小取决于每个节点具有的链接数(节点具有的链接越多,节点的大小就越大,反之亦然)。 以下是我为节点编写的JS。
如何通过链接数更改节点大小?
var nodes = graph.nodes.slice(),
links = [],
bilinks = [];
graph.links.forEach(function(link) {
var s = nodes[link.source],
t = nodes[link.target],
i = {}; // intermediate node
nodes.push(i);
links.push({source: s, target: i}, {source: i, target: t});
bilinks.push([s, i, t]);
});
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", 3)
.style("fill", function(d) { return color(d.group); })
对于具体的答案,您的问题有点不完整(您应该显示足够的代码来再现您的工作),因此这是基于Bostock的经典示例的概括性问题。 本质上,只需循环链接并计算节点链接到的次数。 然后使用此计数来设置半径:
graph.links.forEach(function(link){
// initialize a new property on the node
if (!link.source["linkCount"]) link.source["linkCount"] = 0;
if (!link.target["linkCount"]) link.target["linkCount"] = 0;
// count it up
link.source["linkCount"]++;
link.target["linkCount"]++;
});
// use it to set radius
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("circle")
.attr("class", "node")
.attr("r", function(d){
return d.linkCount ? (d.linkCount * 2) : 2; //<-- some function to determine radius
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.