2013-07-17 3 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

JSON 배열을 링크가 JSON.parse (JSON.stringify (linkdata))로 깊게 복사했습니다. 작동합니다. 최선의 해결책은 아니지만 적어도 다음 단계로 넘어갑니다. 도와 주셔서 감사합니다! –