1
我在尝试将我的可视化脚本更改为更像Modifying a Force Layout Example。由于我没有像a, b and c
这样的固定节点来添加我读json file
来填充nodes
和links
阵列。无法在d3布局中显示节点
d3.json("mock.json", function(error, json) {
if (error)
throw error;
nodes = nodes.concat(json.nodes);
links = links.concat(json.links);
start();
});
nodes
和links
有合适的尺寸,这意味着节点包含26 nodes
和链接37 links
。现在我想简单地使用line
和circle
元素将它们可视化。
function start() {
link = link.data(force.links(), function(d) { return d.source.id + "-" + d.target.id; });
link.enter().append("line").attr("class", "link");
link.exit().remove();
node = node.data(force.nodes(), function(d) { return d.id;});
node.enter().append("circle").attr("class", "node").attr("r", 8);
node.exit().remove();
force.start();
}
这是非常类似于这个例子,我不明白为什么这不起作用。我提供a demo与模拟。有没有问题,因为我使用concat()
而不是push()
还是有什么问题吗?
Jup,现在工作,但为什么我必须使用'force.nodes(节点);'?在这个例子中,它没有它,尽管我想知道他是如何让部队知道节点和链接的。 –
是的,因为'nodes.concat(json.nodes)'会返回一个新的对象,而'force.nodes()'将持有旧的对象..所以当你在你的代码中执行force.nodes()时,它会返回一个空阵列,这就是你没有看到任何东西的原因。 – Cyril
谢谢! :)现在对我来说很有意义。在这种情况下'push()'不适用于我,因为它只会将数组推到另一个数组中。 –