本文介绍了D3js可折叠树使用深度第一风格进入转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改我的当前d3js垂直可折叠树,以在深度第一方式的节点扩展期间具有缓慢的过渡。目前,加载页面后,所有节点都将全部展开为1。我有兴趣以深度优先转换方式逐个扩展每个节点。
我的基本代码取自此。
是否可以使用d3js或其他库来执行这样的任务?

I would like to modify my current d3js vertical collapsible tree to have a slow transition during the expansion of the nodes in a depth first manner. Currently, as soon the page is loaded, all the nodes are expanded all at ones. I m interested to expand each node one by one in a depth first transition manner.My base code is taken from this SITE.Is it possible to perform such task using d3js or other library need to be used?

function buildVerticalTree(treeData,treeContainerDom){

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

  var i = 0, duration = 750;
  var tree = d3.layout.tree()
    //.nodeSize([70,40]);
    .size([width,height])
    .nodeSize([100, 100]);
  var diagonal = d3.svg.diagonal()
    .projection(function(d){
      return [d.x, d.y];
    });

  var zm;
  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+")")
    .call(zm = d3.behavior.zoom().scaleExtent([0.5,2]).on("zoom", redraw)).append("g")
    .attr("transform", "translate(" + 400 + "," + 20 + ")");

  zm.translate([400, 20]);

  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",40)
      .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 -10;
      })
      .attr("dy",".35em")
      .attr("text-anchor","middle")
      //.text(function(d){
      //  return d.name;
      //})
      .style("font-size",10)
      .style("fill","black")
      .style("fill-opacity",1e-6)
    .each(function (d) {
      var arr = d.name.split(" ");
      for (i = 0; i < arr.length; i++) {
        d3.select(this).append("tspan")
          .text(arr[i])
          .attr("dy", i ? "1.2em" : 0)
          .attr("x", 0)
          .attr("text-anchor", "middle")
          .attr("class", "tspan" + i);
      }
    });

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

    nodeUpdate.select("circle")
      .attr("r",40)
      .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);
  }

  function redraw() {
    //console.log("here", d3.event.translate, d3.event.scale);
    svg.attr("transform",
      "translate(" + d3.event.translate + ")"
      + " scale(" + d3.event.scale + ")");
  }

  var baseWidth = 600;

}//end of buildVerticalTree

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


推荐答案

使用超时:

function autoOpen(head, time) {
    window.setTimeout(function() {
        nodeclick(head); //do node click
        if (head.children) {
            //if has children
            var timeOut = 1000; //set the timout variable
            head.children.forEach(function(child) {
                autoOpen(child, timeOut); //open the child recursively
                timeOut = timeOut + 1000;
            })
        }
    }, time);
}
autoOpen(root, 1000)//start opening from root

工作代码

这篇关于D3js可折叠树使用深度第一风格进入转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:40