2015-04-22 60 views
1

我试图在D3中创建一个决策树。D3.js决策树 - 文本包装,边框,加载圆圈的数量

问题: - 本文发生在右侧,可以/如何设置边界框来停止该边界?

  • 圆圈中的文字未包裹。我愿意让圈子更大以适应文本,但是包装仍然是一个问题。

  • 我该如何获得只显示负载的前3个圆圈?

var treeData = [ 
 
    { 
 
    "name": "TK question here that's long", 
 
    "content":"Question here long", 
 
    "parent": "null", 
 
    "children": [ 
 
     { 
 
     "name": "Level 2: A", 
 
     "parent": "Top Level", 
 
     "children": [ 
 
      { 
 
      "name": "Son of A", 
 
      "parent": "Level 2: A" 
 
      }, 
 
      { 
 
      "name": "Daughter of A", 
 
      "parent": "Level 2: A" 
 
      } 
 
     ] 
 
     }, 
 
     { 
 
     "name": "Level 2: B", 
 
     "parent": "Top Level" 
 
     } 
 
    ] 
 
    } 
 
]; 
 

 

 
// ************** Generate the tree diagram \t ***************** 
 
var margin = {top: 20, right: 120, bottom: 20, left: 120}, 
 
\t width = 860 - margin.right - margin.left, 
 
\t height = 500 - margin.top - margin.bottom; 
 
\t 
 
var i = 0, 
 
\t duration = 750, 
 
\t root; 
 

 
var tree = d3.layout.tree() 
 
\t .size([height, width]); 
 

 
var diagonal = d3.svg.diagonal() 
 
\t .projection(function(d) { return [d.y, d.x]; }); 
 

 
var svg = d3.select("body").append("svg") 
 
\t .attr("width", width + margin.right + margin.left) 
 
\t .attr("height", height + margin.top + margin.bottom) 
 
    .append("g") 
 
\t .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); 
 

 
root = treeData[0]; 
 
root.x0 = height/2; 
 
root.y0 = 0; 
 
    
 
update(root); 
 

 
d3.select(self.frameElement).style("height", "500px"); 
 

 
function update(source) { 
 

 
    // Compute the new tree layout. 
 
    var nodes = tree.nodes(root).reverse(), 
 
\t links = tree.links(nodes); 
 
    
 
    
 

 
    // Normalize for fixed-depth. 
 
    nodes.forEach(function(d) { d.y = d.depth * 180; }); 
 

 
    // Update the nodes… 
 
    var node = svg.selectAll("g.node") 
 
\t .data(nodes, function(d) { return d.id || (d.id = ++i); }); 
 
    
 
    
 

 
    // Enter any new nodes at the parent's previous position. 
 
    var nodeEnter = node.enter().append("g") 
 
\t .attr("class", "node") 
 
\t .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; }) 
 
\t .on("click", click); 
 

 
    nodeEnter.append("circle") 
 
\t .attr("r", 1e-6) 
 
\t .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; }); 
 
    
 

 

 
    nodeEnter.append("text") 
 
\t .attr("x", function(d) { return d.children || d._children ? -33 : 13; }) 
 
\t .attr("dy", ".35em") 
 
\t .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; }) 
 
\t .text(function(d) { return d.name; }) 
 
\t .style("fill-opacity", 1e-6); 
 
    
 
     nodeEnter.append("text") 
 
    .attr("x", 0) 
 
    .attr("dy", ".35em") 
 
    .attr("text-anchor", "middle") 
 
    .text(function(d) { return d.content; }); 
 

 
    // Transition nodes to their new position. 
 
    var nodeUpdate = node.transition() 
 
\t .duration(duration) 
 
\t .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; }); 
 

 
    nodeUpdate.select("circle") 
 
\t .attr("r", 30) 
 
\t .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; }); 
 

 
    nodeUpdate.select("text") 
 
\t .style("fill-opacity", 1); 
 

 
    // Transition exiting nodes to the parent's new position. 
 
    var nodeExit = node.exit().transition() 
 
\t .duration(duration) 
 
\t .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; }) 
 
\t .remove(); 
 

 
    nodeExit.select("circle") 
 
\t .attr("r", 1e-6); 
 

 
    nodeExit.select("text") 
 
\t .style("fill-opacity", 1e-6); 
 

 
    // Update the links… 
 
    var link = svg.selectAll("path.link") 
 
\t .data(links, function(d) { return d.target.id; }); 
 

 
    // Enter any new links at the parent's previous position. 
 
    link.enter().insert("path", "g") 
 
\t .attr("class", "link") 
 
\t .attr("d", function(d) { 
 
\t \t var o = {x: source.x0, y: source.y0}; 
 
\t \t return diagonal({source: o, target: o}); 
 
\t }); 
 

 
    // Transition links to their new position. 
 
    link.transition() 
 
\t .duration(duration) 
 
\t .attr("d", diagonal); 
 

 
    // Transition exiting nodes to the parent's new position. 
 
    link.exit().transition() 
 
\t .duration(duration) 
 
\t .attr("d", function(d) { 
 
\t \t var o = {x: source.x, y: source.y}; 
 
\t \t return diagonal({source: o, target: o}); 
 
\t }) 
 
\t .remove(); 
 

 
    // Stash the old positions for transition. 
 
    nodes.forEach(function(d) { 
 
\t d.x0 = d.x; 
 
\t d.y0 = d.y; 
 
    }); 
 
} 
 

 
// Toggle children on click. 
 
function click(d) { 
 
    if (d.children) { 
 
\t d._children = d.children; 
 
\t d.children = null; 
 
    } else { 
 
\t d.children = d._children; 
 
\t d._children = null; 
 
    } 
 
    update(d); 
 
}
\t .node { 
 
\t \t cursor: pointer; 
 
\t } 
 

 
\t .node circle { 
 
\t fill: #fff; 
 
\t stroke: steelblue; 
 
\t stroke-width: 3px; 
 
\t } 
 

 
\t .node text { 
 
\t font: 12px sans-serif; 
 
\t } 
 

 
\t .link { 
 
\t fill: none; 
 
\t stroke: #ccc; 
 
\t stroke-width: 2px; 
 
\t }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> 
 
<!DOCTYPE html> 
 
<html lang="en"> 
 
    <head> 
 
    <meta charset="utf-8"> 
 

 
    <title>Tree Example</title> 
 

 
    </head> 
 

 
    <body> 
 
\t 
 
    </body> 
 
</html>

回答

0

的覆盖,在这个例子中,文本迈克·博斯托克的包装:text wrapping in d3并仅用于某些节点的表现,你应该包括一个过滤后的数据显示只有某些数据(即没有父母或父母和子女的节点(所以第一个和中间的节点)

+0

我觉得你的意思链接到此:http://bl.ocks.org/mbostock/7555321 我已经加入从功能换到我的代码,但它不工作。 – Jess

+0

是我的错误链接!该函数起作用,你确定你提供了正确的值并在创建文本后调用它? – tomtomtom

+0

我只需要将函数底部的“value”改为“content”对吧?我想不是,我似乎无法使其工作... http://jsfiddle.net/jarstin07/g8x4gpkt/ – Jess

0

你可以查看这个帖子.. Wrapping text labels也可以在foreignObjec您可以根据圆的半径添加x和y。

 
var nodeEnter = node.enter().append(\"g\") 
     .attr(\"class\", \"node\") 
     .attr(\"transform\", function(d) { 
      d.radius = (d.x - 90); // add some data to use in ForeignObject 
      return \"translate(\" + source.y0 + \",\" + source.x0 + \")\"; 
     }) 
     .on(\"click\", click);