由于半径较大,D3Js垂直可折叠图节点重叠

时间:2016-03-04 13:09:33

标签: html css d3.js

我通过从site获取代码创建了一个D3Js垂直可折叠树。我想增加节点的半径。目前,它在r = 10并且希望将其增加到30.这导致邻居节点彼此重叠,如下所示。Image。如何增加每个节点之间的距离,使它们彼此不重叠? 谢谢

function buildVerticalTree(treeData,treeContainerDom){

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

  var i = 0, duration = 750;
  var tree = d3.layout.tree()
    .size([height,width]);

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

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

  var root = treeData;
  update(root);

  function update(source){
    var nodes = tree.nodes(root).reverse();
    var links = tree.links(nodes);

    nodes.forEach(function(d){
      d.y = d.depth * 100;
    });

    var node = svg.selectAll("g.node")
      .data(nodes,function(d){
        return d.id || (d.id=++i);
      });

    var nodeEnter = node.enter().append("g")
      .attr("class","node")
      .attr("transform",function(d){
        if(!source.x0 && !source.y0)
          return "";
        return "translate("+source.x0+","+source.y0+")";
      })
      .on("click",nodeclick)

    nodeEnter.append("circle")
      .attr("r",10)
      .attr("stroke",function(d){
        return d.children || d._children ? "steelblue" : "#00c13f";
      })
      .style("fill",function(d){
        return d.children || d._children ? "lightsteelblue" : "#fff";
      });

    nodeEnter.append("text")
      .attr("y",function(d){
        return d.children || d._children ? -18 : 18;
        //return 0;
      })
      .attr("dy",".35em")
      .attr("text-anchor","middle")
      .text(function(d){
        return d.name;
      })
      .style("fill","black")
      .style("fill-opacity",1e-6)

    var nodeUpdate = node.transition()
      .duration(duration)
      .attr("transform",function(d){
        return "translate("+ d.x+","+ d.y+")";
      });

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

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

    var nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform",function(d){
        return "translate("+source.x+","+source.y+")";
      })
      .remove();

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

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

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

    link.enter().insert("path","g")
      .attr("class","link")
      .attr("d",function(d){
        if(!source.x0 && !source.y0)
          return "";
        var o = {x:source.x0,y:source.y0};
        return diagonal({source:o,target:o});
      });
    link.transition()
      .duration(duration)
      .attr("d",diagonal);

    link.exit().transition()
      .duration(duration)
      .attr("d",function(d){
        var o = {x:source.x,y:source.y};
        return diagonal({source:o,target:o});
      })
      .remove();

    nodes.forEach(function(d){
      d.x0 = d.x;
      d.y0 = d.y;
    });
  }

  function nodeclick(d){

    if(d.children){
      d._children = d.children;
      d.children = null;
    }else{
      d.children = d._children;
      d._children = null;
    }
    update(d);
  }

}//end of buildVerticalTree

buildVerticalTree(that.objectList, "#tree");

0 个答案:

没有答案