简体   繁体   English

d3.js强制有向图:如何使节点大小取决于链接的值?

[英]d3.js force directed graph: How to make node size depends on the value of the links?

I have this kind of data: 我有这种数据:

var nodes = [
{
    "id": "A",
    "label": "Data A",
    "group": "0",
    "level": "0"
}, {
    "id": "B",
    "label": "Data B",
    "group": "1",
    "level": "1"
},
// etc //
]

var links = [
{
    "source": "A",
    "target": "B",
"strength": "0.5",
    "value": "276"
}, {
    "source": "A",
    "target": "C",
"strength": "0.5",
"value": "866"
},
// etc //
]

I've been trying to set the nodes radius size according to the value from the links that have said node as its target . 我一直在尝试根据以该节点为target的链接的value设置节点半径大小。

So for example, node B should have its size according to the value of 276 (the link that have node B as its target). 因此,例如,节点B的大小应根据276的值(以节点B为目标的链接)。

This what I use: 这是我用的:

var nodeElements = g.append("g") 
    .attr("class", "nodes")
    .selectAll("circle")
    .data(nodes)
    .enter().append("circle")
    .attr("r", function(node,link){
          var radius = 12;
          if (node.level == 0) radius = radius * 2; // Setting up the main node as bigger than others

          node.weight = link.filter(function(link) {
             if (link.target.index == node.index){
               var linkValue = link.value;
               if (linkValue > 500) ? (radius = 12) : (radius = 6);
             }
          });

          return radius;
      })
    .attr("fill", getNodeColor)
    .attr("stroke", "#fff")
    .attr('stroke-width', 2)
    .on('mouseover', selectNode)

That doesn't seem to work though. 但这似乎不起作用。 Says it doesn't recognize the link.filter , something I've taken from here . 说它无法识别link.filter ,这是我从这里提取的东西。 I'm using d3.js v4. 我正在使用d3.js v4。

Tried looking up for clues but still got no hints. 试图寻找线索,但仍然没有任何提示。 Any ideas? 有任何想法吗?

There are several things that need to change, but first you should understand that you are not automatically adding links to the node's data. 有几件事需要更改,但是首先您应该了解,您不会自动将links添加到节点的数据。

So the purpose of the filter statement seems to be to find the corresponding link in that array, based on index. 因此,filter语句的目的似乎是根据索引在该数组中找到相应的链接。 There is no link variable passed into the outer function, but instead you should be searching through the links array defined above for one link that has target with the node's id. 没有link变量传递到外部函数中,但是您应该在上面定义的links数组中搜索一个目标为带有节点ID的链接。

If you only need one link, and not all of them, use Array.prototype.find . 如果只需要一个链接,而不是全部,请使用Array.prototype.find But filter and find work the same way - the anonymous function defined is called on each item in the array. 但是filterfind工作的方式相同-定义的匿名函数在数组中的每个项目上调用。 find stops at the first found object, while filter returns all matches. find停在第一个找到的对象上,而filter返回所有匹配项。

.attr("r", function(dat, index, n) {
    var linkItem = links.find(function(link) {
        return link.target == dat.id;
    });
    var radius = 12;
    // linkItem would be undefined if the item was not found in the links
    if (linkItem && linkItem.value > 500) {
        radius = 12;
    } else {
        radius = 6;
    }

    // you should double the radius towards the end,
    // so that radius doesn't get overwritten
    if (dat.level === 0) {
        radius = 2 * radius;
    }

    return radius;
})

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

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