d3.js linkStrength influence on linkDistance in a force graph

后端 未结 2 1126
鱼传尺愫
鱼传尺愫 2021-01-15 06:00

I\'m working on a graph to show relations between different nodes. The closer related the nodes are (according to business logic), the closer together the nodes should be. <

2条回答
  •  醉梦人生
    2021-01-15 06:41

    The link strength sets the rigidity of the links, not the distance between the nodes (force layout doc). The distance of the nodes from each other is controlled by their charge. You can make the charge of a node dynamic like so:

    var force = d3.layout.force()
            .nodes(nodes)
            .theta(0.1)
            .charge(function (d) {
                return -(d.radius * d.radius * 0.125);
            })
            .gravity(0.1)
            .size([width, height])
            .on("tick", tick)
            .start();
    

    A working example that uses this technique: vizz.ly

提交回复
热议问题