2017-10-05 128 views
0

我目前有一個D3,它顯示一個強制定向網絡,並在點擊時摺疊它的子節點。我想也顯示懸停節點的名稱。像這樣:https://bl.ocks.org/mbostock/1212215d3節點標籤和摺疊

以下是我的代碼到目前爲止。

.node { 
    cursor: pointer; 
    stroke: #3182bd; 
    stroke-width: 1.5px; 
} 

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

</style> 
<body> 
<script src="http://d3js.org/d3.v3.min.js"></script> 
<script> 

var width = 960, 
    height = 500, 
    root; 

var force = d3.layout.force() 
    .size([width, height]) 
    .on("tick", tick); 

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

//Added markers to indicate that this is a directed graph 
svg.append("defs").selectAll("marker") 
    .data(["arrow"]) 
    .enter().append("marker") 
    .attr("id", function(d) { return d; }) 
    .attr("viewBox", "0 -5 10 10") 
    .attr("refX", 15) 
    .attr("refY", -1.5) 
    .attr("markerWidth", 4) 
    .attr("markerHeight", 4) 
    .attr("orient", "auto") 
    .append("path") 
    .attr("d", "M0,-5L10,0L0,5"); 

var link = svg.selectAll(".link"), 
    node = svg.selectAll(".node"); 

d3.json("test.json", function(json) { 
    root = json; 
    //Give nodes ids and initialize variables 
    for(var i=0; i<root.nodes.length; i++) { 
    var node = root.nodes[i]; 
    node.id = i; 
    node.collapsing = 0; 
    node.collapsed = false; 
    } 
    //Give links ids and initialize variables 
    for(var i=0; i<root.links.length; i++) { 
    var link = root.links[i]; 
    link.source = root.nodes[link.source]; 
    link.target = root.nodes[link.target]; 
    link.id = i; 
    } 
    // for (var i=0; i<root.nodes.length; i++){ 
    // var node = root.nodes[i]; 

    // } 
    update(); 
}); 

function update() { 
    //Keep only the visible nodes 
    var nodes = root.nodes.filter(function(d) { 
    return d.collapsing == 0; 
    }); 
    var links = root.links; 
    //Keep only the visible links 
    links = root.links.filter(function(d) { 
    return d.source.collapsing == 0 && d.target.collapsing == 0; 
    }); 

    force 
     .nodes(nodes) 
     .links(links) 
     .start(); 

    // Update the links… 
    link = link.data(links, function(d) { return d.id; }); 

    // Exit any old links. 
    link.exit().remove(); 

    // Enter any new links. 
    link.enter().insert("line", ".node") 
     .attr("class", "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; }) 


    // Update the nodes… 
    node = node.data(nodes, function(d){ return d.id; }).style("fill", color); 

    // Exit any old nodes. 
    node.exit().remove(); 

    // Enter any new nodes. 
    node.enter().append("circle") 
     .attr("class", "node") 
     .attr("cx", function(d) { return d.x; }) 
     .attr("cy", function(d) { return d.y; }) 
     .attr("r", function(d) { return Math.sqrt(d.size)/10 || 4.5; }) 
     .style("fill", color) 
     .on("click", click) 
     .call(force.drag); 
} 

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("cx", function(d) { return d.x; }) 
     .attr("cy", function(d) { return d.y; }); 
} 

// Color leaf nodes orange, and packages white or blue. 
function color(d) { 
    return d.collapsed ? "#3182bd" : d.children ? "#c6dbef" : "#fd8d3c"; 
} 

// Toggle children on click. 
function click(d) { 
    if (!d3.event.defaultPrevented) { 
    //check if link is from this node, and if so, collapse 
    root.links.forEach(function(l) { 
     if(l.source.id == d.id) { 
     if(d.collapsed){ 
      l.target.collapsing--; 
     } else { 
      l.target.collapsing++; 
     } 
     } 
    }); 
    d.collapsed = !d.collapsed; 
    } 
    update(); 
} 

</script> 

我的問題是與更新功能。我能夠讓它顯示,但是當我點擊摺疊時,它不會顯示標籤。我不知道我是否應該在嘀嘀嘀嗒或什麼。

謝謝!

+1

你也可以發佈test.json – Cyril

+0

從鏈接的json文件readme.json將與代碼 – Jakeeln

回答

2

使用SVG組元素對圓和文本標籤進行分組,如下所示。

var groupNodes = node.enter().append("g") 
    .attr("class", "node") 
    .attr("transform", function(d) { return "translate("+d.x+","+d.y+")"; }); 
    .on("click", click) 
    .on("mouseover", function(){ 
     d3.select(this).select("text").style("display","block"); 
    }) 
    .on("mouseout", function(){ 
     d3.select(this).select("text").style("display","none"); 
    }) 
    .call(force.drag); 

groupNodes.append("circle")     
     .attr("r", function(d) { return Math.sqrt(d.size)/10 || 4.5; }) 
     .style("fill", color); 

var label = groupNodes.append("text") 
     .attr("dy", ".35em") 
     .style("display", "none) 
     .text(function(d) { return d.name; //Use the key which holds the name value }); 

上述代碼替換更新函數中的以下代碼部分。

node.enter().append("circle") 
     .attr("class", "node") 
     .attr("cx", function(d) { return d.x; }) 
     .attr("cy", function(d) { return d.y; }) 
     .attr("r", function(d) { return Math.sqrt(d.size)/10 || 4.5; }) 
     .style("fill", color) 
     .on("click", click) 
     .call(force.drag); 

現在改變刻度功能如下

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+")"; 
    }); 
} 

現在的所有其它功能應該按預期工作。

+0

一起使用我有一些問題將其合併到我的代碼中。第一塊到哪裏去了?我正在使用節點和node.enter變量 – Jakeeln

+0

應該在更新函數中寫入第一個塊。此代碼將創建節點作爲組,其中將包含每個圓圈和相應的文本元素 – Gilsha

+0

感謝您的澄清!現在名稱將靜態顯示。有沒有簡單的方法來改變它顯示在懸停?或者我應該問一個新的線索。 – Jakeeln