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()'不適用於我,因爲它只會將數組推到另一個數組中。 –