d3.js: “Cannot read property 'weight' of undefined” when manually defining both nodes and links for force layout

前端 未结 6 687
旧时难觅i
旧时难觅i 2020-12-09 16:31

I tried setting both nodes and links at the same time this way:

var force = d3.layout.force()
    .size([w, h])
    .nodes(nodes)
    .links(connections)
            


        
6条回答
  •  被撕碎了的回忆
    2020-12-09 17:21

    The force-directed layout uses edge weights to calculate the layout. Try adding a dummy "weight":1 to all of your connections.

    The code that initializes the links looks like this:

    links.forEach(function(d) {
        if (typeof d.source == "number") { d.source = nodes[d.source]; }
        if (typeof d.target == "number") { d.target = nodes[d.target]; }
    });
    

    Presumably you could tweak that (in the d3 source) to use any property/type.

提交回复
热议问题