d3.js之樹形摺疊樹

1.效果

children和_childrenjavascript

2.技術分解

2.1摺疊函數html

// (1) 遞歸調用,有子孫的就把children(顯示)給_children(不顯示)暫存,便於摺疊,
function collapse(d) {
    if (d.children) {  console.log(d);
        d._children = d.children;
        d._children.forEach(collapse);
        d.children = null;
    }
}
// 摺疊根節點的每一個孩子
root.children.forEach(collapse);
// 摺疊以後要重繪
update(root);

  

2.2 根據交互的狀況更新佈局並輸出java

function update(source) {
  // (2-1) 計算新樹的佈局
  var nodes = tree.nodes(root).reverse(),
      links = tree.links(nodes);
  
  // (2-2) 樹的深度這裏樹d.y。樹的寬度最大720,要分四層,因此每層就乘180
  nodes.forEach(function(d) { 
    d.y = d.depth * 180;// 樹的x,y倒置了,因此這裏Y實際上是橫向的
  });

  // (2-3) 數據鏈接,根據id綁定數據
  var node = svg.selectAll("g.node")
      .data(nodes, function(d) {
        return d.id //最初新點開的節點都沒有id
        || (d.id = ++i); //爲沒有id的節點添加上ID
      });

  // (2-4) 點擊時增長新的子節點
  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);
  
  // (2-5) 原有節點更新到新位置
  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);
  
  // (2-6) 摺疊節點的子節點收縮回來
  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);
  
  // (2-7) 數據鏈接,根據目標節點的id綁定數據
  var link = svg.selectAll("path.link")
      .data(links, function(d) { return d.target.id; });
  
  // (2-8) 增長新鏈接
  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});
      });
  
  // (2-9) 原有鏈接更新位置
  link.transition()
      .duration(duration)
      .attr("d", diagonal);
  
  // (2-10) 摺疊的連接,收縮到源節點處
  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;
  });
}

 2.3 點擊時切換摺疊node

// (3) 切換摺疊與否
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);// 從新渲染
}

3.完整代碼

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>testD3-26-CollapsibleTree.html</title>
<script type="text/javascript" src="d3.js"></script>
<style>

.node circle {
  fill:yellow ;
  stroke: red;
  stroke-width: 1.5px;
}

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

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

</style>
</head>
<body>
<script type="text/javascript">
        //位置參數
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("tree.json", function(error, flare) {
    // 根節點和位置
    root = flare;
    root.x0 = height / 2;
    root.y0 = 0;
    //(1) 摺疊函數,遞歸調用,有子孫的就把children(顯示)給_children(不顯示)暫存,便於摺疊,
    function collapse(d) {
        if (d.children) {  console.log(d);
            d._children = d.children;
            d._children.forEach(collapse);
            d.children = null;
        }
    }
    // 摺疊根節點的每一個孩子
    root.children.forEach(collapse);
    // 摺疊以後要重繪
    update(root);
});

//(2) 更新佈局
function update(source) {
  // (2-1) 計算新樹的佈局
  var nodes = tree.nodes(root).reverse(),
      links = tree.links(nodes);
  
  // (2-2) 樹的深度這裏樹d.y。樹的寬度最大720,要分四層,因此每層就乘180
  nodes.forEach(function(d) { 
    d.y = d.depth * 180;// 樹的x,y倒置了,因此這裏Y實際上是橫向的
  });

  // (2-3) 數據鏈接,根據id綁定數據
  var node = svg.selectAll("g.node")
      .data(nodes, function(d) {
        return d.id //最初新點開的節點都沒有id
        || (d.id = ++i); //爲沒有id的節點添加上ID
      });

  // (2-4) 點擊時增長新的子節點
  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);
  
  // (2-5) 原有節點更新到新位置
  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);
  
  // (2-6) 摺疊節點的子節點收縮回來
  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);
  
  // (2-7) 數據鏈接,根據目標節點的id綁定數據
  var link = svg.selectAll("path.link")
      .data(links, function(d) { return d.target.id; });
  
  // (2-8) 增長新鏈接
  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});
      });
  
  // (2-9) 原有鏈接更新位置
  link.transition()
      .duration(duration)
      .attr("d", diagonal);
  
  // (2-10) 摺疊的連接,收縮到源節點處
  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;
  });
}

// (3) 切換摺疊與否
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);// 從新渲染
}</script></body></html>
相關文章
相關標籤/搜索