2013-07-17 29 views
1

我正在玩D3並製作強制指令圖。我想清除並重新加載圖表以響應點擊按鈕。但是,當我重新加載已經加載的圖形時,圖形格式不正確。這是我使用的代碼:圖形將無法正確重新加載到D3強制佈局

<!DOCTYPE html> 
<meta charset="utf-8"> 
<script src="http://d3js.org/d3.v2.js?2.9.1"></script> 
<style> 

.link { 
    fill: none; 
    stroke: #666; 
    stroke-width: 1.5px; 
} 

.node circle { 
    fill: #ccc; 
    stroke: #fff; 
    stroke-width: 1.5px; 
} 

text { 
    font: 10px sans-serif; 
    pointer-events: none; 
} 

</style> 
<body> 
<script> 

var all = [ 
{source: "Tockwotton", target: "List", type: "woodworking"}, 
{source: "Tockwotton", target: "Prince Lab", type: "woodworking"}, 
{source: "Prince Lab", target: "Prince Lab", type: "metalworking"}, 
{source: "Tockwotton", target: "List", type: "cnc"}, 
{source: "Granoff", target: "Prince Lab", type: "3d-printing"}, 
{source: "Tockwotton", target: "List", type: "drafting"}, 
{source: "List", target: "Prince Lab", type: "welding"}, 
{source: "Prince Lab", target: "Prince Lab", type: "sand-blaster"}, 
{source: "Tockwotton", target: "List", type: "finishing"}, 
{source: "Tockwotton", target: "Prince Lab", type: "finishing"}, 
{source: "Tockwotton", target: "Tockwotton", type: "laser-cutter"} 
]; 

var wood = [ 
{source: "Tockwotton", target: "List", type: "woodworking"}, 
{source: "Tockwotton", target: "Prince Lab", type: "woodworking"} 
]; 

var metal = [ 
{source: "Prince Lab", target: "Prince Lab", type: "metalworking"} 
]; 

var cnc = [ 
{source: "Tockwotton", target: "List", type: "cnc"} 
]; 

var print = [ 
{source: "Granoff", target: "Prince Lab", type: "3d-printing"} 
]; 

var draft = [ 
{source: "Tockwotton", target: "List", type: "drafting"} 
]; 

var weld = [ 
{source: "List", target: "Prince Lab", type: "welding"} 
]; 

var sand = [ 
{source: "Prince Lab", target: "Prince Lab", type: "sand-blaster"} 
]; 

var finishing = [ 
{source: "Tockwotton", target: "List", type: "finishing"}, 
{source: "Tockwotton", target: "Prince Lab", type: "finishing"} 
]; 

var laser = [ 
{source: "Tockwotton", target: "Tockwotton", type: "laser-cutter"} 
]; 

function render(linkdata){ 

d3.selectAll("svg").remove(); 

links = linkdata; 

nodes = {}; 

// Compute the distinct nodes from the links. 
links.forEach(function(link) { 
    link.source = nodes[link.source] || (nodes[link.source] = {name: link.source, type: link.type}); 
    link.target = nodes[link.target] || (nodes[link.target] = {name: link.target, type: link.type}); 
}); 

width = 960, 
    height = 500; 

force = d3.layout.force() 
    .nodes(d3.values(nodes)) 
    .links(links) 
    .size([width, height]) 
    .linkDistance(60) 
    .charge(-300) 
    .on("tick", tick) 
    .start(); 

svg = d3.select("body").append("svg") 
    .attr("width", width) 
    .attr("height", height); 

link = svg.selectAll(".link") 

    .data(force.links()) 
    .enter().append("line") 
    .attr("class", "link"); 

node = svg.selectAll(".node") 
    .data(force.nodes()) 
    .enter().append("g") 
    .attr("class", "node") 
    .on("mouseover", mouseover) 
    .on("mouseout", mouseout) 
    .call(force.drag); 

node.append("circle") 
    .attr("r", 8); 

node.append("text") 
    .attr("x", 12) 
    .attr("dy", ".35em") 
    .text(function(d) { return d.name; }); 

function tick() { 
    link 
     .attr("x1", function(d) { return d.source.x; }) 
     .attr("y1", function(d) { return d.source.y; }) 
     .attr("x2", function(d) { return d.target.x; }) 
     .attr("y2", function(d) { return d.target.y; }); 

    node 
     .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }); 
} 

function mouseover() { 
    d3.select(this).select("circle").transition() 
     .duration(750) 
     .attr("r", 16); 
} 

function mouseout() { 
    d3.select(this).select("circle").transition() 
     .duration(750) 
     .attr("r", 8); 
} 
} 
</script> 

<div id="nav"> 
    <div id="wood"> 
     <a onclick="render(wood)">Woodworking</a> 
    </div> 
    <div id="metal"> 
     <a onclick="render(metal)">Metalworking</a> 
    </div> 
    <div id="cnc"> 
     <a onclick="render(cnc)">CNC</a> 
    </div> 
    <div id="print"> 
     <a onclick="render(print)">3D Printing</a> 
    </div> 
    <div id="draft"> 
     <a onclick="render(draft)">Drafting</a> 
    </div> 
    <div id="weld"> 
     <a onclick="render(weld)">Welding</a> 
    </div> 
    <div id="sand"> 
     <a onclick="render(sand)">Sand Blast</a> 
    </div> 
    <div id="finishing"> 
     <a onclick="render(finishing)">Finishing</a> 
    </div> 
    <div id="laser"> 
     <a onclick="render(laser)">Laser Cutting</a> 
    </div> 
    <div id="all"> 
     <a onclick="render(all)">All</a> 
    </div> 
</div> 

</body> 

Try it,請注意,您第一次單擊一個項目,它工作正常,但如果你再次點擊它,你只能叫[object Object]一個節點。爲什麼是這個,我該如何解決這個問題?

回答

1

當您使用links = linkdata時,它可能不符合您的期望。像其他一些語言一樣,這不會複製數據;相反,它只是使links指向linkdata所指向的相同位置。在循環訪問links時,將它們修改爲指向對方,即可修改數組中的原始對象。當你再次循環它們時,它們已經被修改,導致你正在經歷的不良行爲。

爲了避免這種命運,您需要在修改數組之前深度複製數組。有quite a few ways to do that,但是你要確保你使用的任何解決方案都將數組作爲一個數組克隆,而不是將它變成一個普通的Object

+0

我使用links = JSON.parse(JSON.stringify(linkdata))深度複製了JSON數組;它的工作原理。不是最好的解決方案,但至少它讓我進入下一步。謝謝您的幫助! –