【发布时间】:2017-01-12 23:08:17
【问题描述】:
这可能是有史以来最简单的 D3 力布局:
const svg = main.append("svg")
.attr("width",500)
.attr("height",500)
.classed("SVG_frame",true)
.append("g")
const nodes = [{id:1},{id:2}];
const simulation = d3.forceSimulation(nodes)
.force("centering",d3.forceCenter([200,200]))
.force("collision",d3.forceCollide(20))
const node = svg
.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr("r",20)
simulation.on("tick",function() {
console.log(nodes[0].x)
node
.attr("cx",d => d.x)
.attr("cy",d => d.y)
})
然而,我在第一帧得到<circle> attribute cx: Expected length, "NaN".。 (cy 在模拟放弃移动的框架上移动了一点点)
我知道这个问题已经被问过好几次了,但似乎没有一个解决版本 4,其中力模拟可能已经改变了它的内部运作。事实上,文档现在甚至声明当位置为 NaN 时,the position is automatically arranged in a "phyllotaxis arrangement" or whatever,所以也许这不应该发生,但它确实发生了。
有人知道吗?
【问题讨论】:
标签: javascript d3.js