2016-05-06 53 views
1

我正在试验this d3.js的示例(粘贴在下面的全部内容)来绘制基于JSON树结构的树。如何获得d3.js树的布局按字母顺序排列节点?

此代码绘制了一棵很好的树,但它不以任何顺序垂直排序兄弟节点。数据恰好已经在underlying JSON object中排序。

我想知道如何我可以按字母顺序(顶部A,底部Z,理想情况下不区分大小写)垂直排列这些节点,如果底层数据尚未排序。我需要在下面的代码中更改什么?

START编辑:

我想做到这一点,而无需修改底层的JSON对象。 我试过的一件事是在d3.layout.tree()行后面加上tree.sort(d3.ascending);。但是这没有任何效果。我不知道为什么。

编辑完

<!DOCTYPE html> 
    <meta charset="utf-8"> 
    <style> 

    .node { 
     cursor: pointer; 
    } 

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

    .node text { 
     font: 10px sans-serif; 
    } 

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

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

    var margin = {top: 20, right: 120, bottom: 20, left: 120}, 
     width = 960 - margin.right - margin.left, 
     height = 800 - margin.top - margin.bottom; 

    var i = 0, 
     duration = 750, 
     root; 

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

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

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

    d3.json("/mbostock/raw/4063550/flare.json", function(error, flare) { 
     if (error) throw error; 

     root = flare; 
     root.x0 = height/2; 
     root.y0 = 0; 

     function collapse(d) { 
     if (d.children) { 
      d._children = d.children; 
      d._children.forEach(collapse); 
      d.children = null; 
     } 
     } 

     root.children.forEach(collapse); 
     update(root); 
    }); 

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

    function update(source) { 

     // Compute the new tree layout. 
     var nodes = tree.nodes(root).reverse(), 
      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") 
      .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") 
      .attr("class", "node") 
      .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; }) 
      .on("click", click); 

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

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

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

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

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

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

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

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

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

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

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

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

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

    // Toggle children on click. 
    function click(d) { 
     if (d.children) { 
     d._children = d.children; 
     d.children = null; 
     } else { 
     d.children = d._children; 
     d._children = null; 
     } 
     update(d); 
    } 

    </script> 

回答

2

取而代之的是:

tree.sort(d3.ascending) 

做这样的:

var tree = d3.layout.tree() 
    .size([height, width]).sort(function(a,b){ 
     return a.name.toLowerCase().localeCompare(b.name.toLowerCase()); 
    });; 

工作代码here

+0

谢谢!如果我不关心区分大小写,我可以使用'd3.ascending'吗?你能告诉我如何?我找不到任何正在使用的示例。 –

+1

对于对象排序,你需要给一个排序函数'd3.ascending'不起作用。如果你不关心不区分大小写的话:'return a.name.localeCompare(b.name);' – Cyril

1

如果flare.json与例如一个类似的,你可以使用JS排序函数来处理这个。

if (error) throw error; 

flare.children.sort(function(a, b){ 
    if(a.name < b.name) return -1; 
    if(a.name > b.name) return 1; 
    return 0; 
}); 
+0

我宁愿不修改底层的JSON结构。我可以在树上设置一个标志吗? –

+0

@SaqibAli根据文档https://github.com/mbostock/d3/wiki/Tree-Layout树中有一个排序功能,可以通过升序和降序功能执行此操作。所以我猜测没有你想要的旗子。但是,如果您还想保留原始的JSON,为什么不将这个有序的绑定到一个新的对象? – echonax

+1

请参阅我的编辑以回答您的评论。谢谢。 –