【发布时间】:2018-02-22 18:34:49
【问题描述】:
我的气泡图有问题。
我之前将 forceSimulation() 与一组对象一起使用,并且它有效。现在我更改了数据源,但它没有,即使控制台显示 no errors。
我的数据是一个名为“lightWeight”的对象,结构如下:
我用它来附加圆圈:
// draw circles
var node = bubbleSvg.selectAll("circle")
.data(d3.entries(lightWeight))
.enter()
.append("circle")
.attr('r', function(d) { return scaleRadius(d.value.length)})
.attr("fill", function(d) { return colorCircles(d.key)})
.attr('transform', 'translate(' + [w/2, 150] + ')');
然后我创建模拟:
// simulate physics
var simulation = d3.forceSimulation()
.nodes(lightWeight)
.force("charge", d3.forceCollide(function(d) { return d.r + 10; }))
.force("x", d3.forceX())
.force("y", d3.forceY())
.on("tick", ticked); // updates the position of each circle (from function to DOM)
// call to check the position of each circle
function ticked(e) {
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
但圆圈仍然相互重叠,不会像以前那样变成气泡图。
如果这可能是一个愚蠢的问题,我深表歉意,我是 d3 的新手,对 forceSimulation() 的实际工作原理知之甚少。
例如,如果我用不同的数据多次调用它,生成的模拟会只影响指定的数据吗?
提前致谢!
【问题讨论】:
标签: javascript json d3.js force-layout