2014-04-19 1 views
1

D3 레이아웃은 버튼 클릭에 따라 동적으로 업데이트됩니다. SVG를 업데이트하기 위해 버튼을 클릭 할 때, 항상 오래된 SVG 아래에 새로운 SVG가 생성됩니다.현재 svg를 새 svg로 바꿉니다. 이전 DOM 요소도 삭제합니다.

버튼을 클릭하면 이전 SVG 아래에 새 SVG를 계속 추가합니다.

내가 원하는 것은 현재 SVG를 대체하고 같은 장소에서 업데이트 된 차트를 만들어야한다는 것입니다.

// JQuery function for two buttons. Based on button click, I am selecting 
input file name 

$(document).ready(function(){ 
    $("#link1").click(function(){ 
    d3.select("#SVG_name").remove(); 
getData("readme-flare-imports.json"); 
    }); 
$("#link2").click(function(){ 
    d3.select("#SVG_name").remove(); 
    getData("readme-flare-imports_1.json"); 
    } 
)}); 


var getData = function(fileName) 
{ 

var diameter = 500, 
    radius = diameter/2, 
    innerRadius = radius - 120; 

var cluster = d3.layout.cluster() 
    .size([360, innerRadius]) 
    .sort(null) 
    .value(function(d) { return d.size; }); 

var bundle = d3.layout.bundle(); 

var line = d3.svg.line.radial() 
.interpolate("bundle") 
.tension(.85) 
.radius(function(d) { return d.y; }) 
.angle(function(d) { return d.x/180 * Math.PI; }); 

var svg = d3.select("body").append("svg") 
.attr("width", diameter) 
.attr("height", diameter) 
.append("g") 
.attr("id","SVG_name") 
.attr("transform", "translate(" + radius + "," + radius + ")"); 

var link = svg.append("g").selectAll(".link"), 
node = svg.append("g").selectAll(".node"); 
var tip = d3.tip() 
    .attr('class', 'd3-tip') 
    .offset([-10, 0]) 

svg.call(tip); 
d3.select(self.frameElement).style("height", diameter + "px"); 

d3.json(fileName, function(error, classes) { 
    var nodes = cluster.nodes(packageHierarchy(classes)), 
    links = packageImports(nodes); 

    link = link 
    .data(bundle(links)) 
    .enter().append("path") 
    .each(function(d) { d.source = d[0], d.target = d[d.length - 1]; }) 
    .attr("class", "link") 
    .attr("d", line); 

    node = node 
    .data(nodes.filter(function(n) { return !n.children; })) 
.enter().append("text") 
    .attr("class", "node") 
    .attr("dx", function(d) { return d.x < 180 ? 8 : -8; }) 
    .attr("dy", ".31em") 
    .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + d.y + ")" + (d.x < 180 ? "" : "rotate(180)"); }) 
    .style("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; }) 
    .text(function(d) { return d.key; }) 
    .on("mouseover", mouseovered) 
    .on("mouseout", mouseouted); 
}); 

    function mouseovered(d) { 
    tip.html("<span style='color:red'>" + d.key + "</span>"); 
    node 
     .each(function(n) { n.target = n.source = false; }); 

    link 
    .classed("link--target", function(l) { if (l.target === d) return l.source.source = true; }) 
    .classed("link--source", function(l) { if (l.source === d) return l.target.target = true; }) 
    .filter(function(l) { return l.target === d || l.source === d; }) 
    .each(function() { this.parentNode.appendChild(this); }); 

    node 
    .classed("node--target", function(n) { return n.target; }) 
    .classed("node--source", function(n) { return n.source; }) 
    .classed("mouseover", tip.show); 
} 

function mouseouted(d) { 
link 
    .classed("link--target", false) 
    .classed("link--source", false); 

node 
    .classed("node--target", false) 
    .classed("node--source", false) 
    .classed("mouseout",tip.hide); 
} 



// Lazily construct the package hierarchy from class names. 
function packageHierarchy(classes) { 
var map = {}; 

function find(name, data) { 
    var node = map[name], i; 
    if (!node) { 
    node = map[name] = data || {name: name, children: []}; 
    if (name.length) { 
    node.parent = find(name.substring(0, i = name.lastIndexOf("."))); 
    node.parent.children.push(node); 
    node.key = name.substring(i + 1); 
    } 
} 
return node; 
} 

classes.forEach(function(d) { 
    find(d.name, d); 
    }); 

return map[""]; 
} 

// Return a list of imports for the given array of nodes. 
function packageImports(nodes) { 
    var map = {}, 
    imports = []; 

    // Compute a map from name to node. 
    nodes.forEach(function(d) { 
    map[d.name] = d; 
    }); 

    // For each import, construct a link from the source to target node. 
    nodes.forEach(function(d) { 
    if (d.imports) d.imports.forEach(function(i) { 
    imports.push({source: map[d.name], target: map[i]}); 
    }); 
}); 

return imports; 
} 
} 

<body> 
    <div> 
     <button class="btn btn-default ok-button" id="link1"> Link1 </button> 
     <button class="btn btn-default ok-button" id="link2"> Link2 </button> 
    </div> 
     <script type="text/javascript" src="d3/d3.v3.min.js"></script> 
     <script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script> 
     <script type="text/javascript" src="js/jquery-2.1.0.min.js"></script> 
     <script type="text/javascript" src="js/main.js"></script> 
</body> 
+0

모든 HTML 컨텍스트를? – Lugia101101

+0

@ Lugia101101 - – Neil

+0

추가 페이지에 SVG를 추가 할 때 요소가 생성됩니까? – Lugia101101

답변

1

내가하는 D3 전문가가 아니에요 업데이트 아래 내 코드는하지만,이 코드는 나에게 잘못 같습니다

var svg = d3.select("body").append("svg") 
      .attr("width", diameter) 
      .attr("height", diameter) 
      .append("g") 
      .attr("id","SVG_name") 

는 D3 문서를 인용하면 append() :

지정한 이름의 새 요소를 현재 선택의 각 요소의 마지막 자식 에 추가하고 추가 된 요소가 포함 된 을 반환합니다.

실제로 실제로는 해당 ID를 <svg>이 아닌 <g>에 할당합니다. 그래서

d3.select("#SVG_name").remove(); 

<g> 아닌 <svg>을 제거한다.

대신이 시도

:

var svg = d3.select("body").append("svg") 
      .attr("width", diameter) 
      .attr("height", diameter) 
      .attr("id","SVG_name") 
      .append("g") 
+0

고마워요 @BigBadaboom. 효과가있다. – Neil

관련 문제