我正在尝试编写自己的函数来权衡d3 v4 Force有向图的链接。
此处提供了默认的强度函数,根据自述文件,可以通过传递新函数作为参数来更改它。(请注意,自述文件中的功能与实际代码中的功能略有不同)。
这是我要工作的部分代码:
var simulation = d3.forceSimulation()
.force("link", d3.forceLink()
.distance(60)
.id(function (d) { return d.name;})
.strength( function (link) {/* my code here */}))
我尝试用默认值替换注释:
function (link) { return 1 / Math.min(count[link.source.index],
count[link.target.index]); }
没有成功。
从您提供的示例中很难看出来,但是您需要将哪些链接传递给模拟forceLink函数。如果您的示例实际上成功地构建了链接,则可以考虑将计算外部化,然后从内部链接中调用该函数:这是一个对我有用的示例。
var weightScale = d3.scaleLinear()
.domain(d3.extent(edges, function (d) { return d.weight }))
.range([.1, 1])
graph = d3.forceSimulation()
.nodes(nodes)
.force("charge", d3.forceManyBody()
.strength(-75) // -1000
.distanceMax([250]))
.force("link", d3.forceLink()
.links(edges)
.id(function(d) { return d.index })
.strength (function (d) {return weightScale(d.weight)})
.distance(55))
.force("center", d3.forceCenter(250, 250)) // width / 2, height / 2
.on("tick", forceTick);
我weightScale
在链接权重上使用了一个标度,称为scale .strenght
。您可以像这样传递链接:
.force("link", d3.forceLink(*YOURLINKS*)
或这个:
.force("link", d3.forceLink()
.links(*YOURLINKS*)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句