使用D3.js V4缩进树

时间:2016-10-06 22:17:55

标签: d3.js

我正在尝试使用D3.js V4显示缩进的树视图但由于某种原因我不能使矩形以正确的顺序显示(父子关系)。

等级2:B应显示在与等级2:A相同的x位置,但是y的位置似乎是错误的。我期望的是这样的:

Treeview Image

有人可以告诉我我做错了吗?

我是D3的新手,所以请事先道歉。 这是我的代码片段:

 var treeData =
     {
      "name": "Top Level",
      "parent": "null",
      "children":  [
        {
          "name": "Level 2: A",
          "parent": "Top Level",
          "children": [
            {
              "name": "Son of A",
              "parent": "Level 2: A",
              "children" : [
                {
                  "name": "child of Son",
                  "parent": "Son of A"
                }
              ]
            },//children
            {
              "name": "Daughter of A",
              "parent": "Level 2: A"
            }
          ]
        },
        {
          "name": "Level 2: B",
          "parent": "Top Level"
        }
      ]
    }

var margin = {top: 30, right: 20, bottom: 30, left: 20},
    width = 960 - margin.left - margin.right,
    barHeight = 30,
    barWidth = width * .1;

    height = 600;

var i = 0,
    duration = 400,
    root;

var tree = d3.tree()
    .nodeSize([0, 20]);


var svg = d3.select("body").append("svg")
    .attr('viewBox',  '0 -10 '+ width  + " " + 500)
    //.attr("width", width + margin.left + margin.right)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var root = d3.hierarchy(treeData, function (d) { return d.children;});

update(root);



function update(source) {

  // Compute the flattened node list. TODO use d3.layout.hierarchy.
   nodes = tree(root).descendants();
  console.log(nodes)
  var height = Math.max(500, nodes.length * barHeight + margin.top + margin.bottom);

  d3.select("svg").transition()
      .duration(duration)
      .attr("height", height);

  // Compute the "layout".
  nodes.forEach(function (n,i) {
   n.x = i * 30;
  });

  // Update the nodes…
  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) {  return "translate(" + source.y + "," + source.x + ")"; })
      .style("opacity", 1e-6);

  // Enter any new nodes at the parent's previous position.
  nodeEnter.append("rect")
      .attr("y", -barHeight / 2)
      .attr("height", barHeight)
      .attr("width", barWidth)
      .style("fill", color)
      .on("click", click);

  nodeEnter.append("text")
      .attr("dy", 3.5)
      .attr("dx", 5.5)
      .text(function (d) {
        return d.depth == 0 ? d.data.name + " >>>" : d.depth == 1 ? d.data.name + " >>" : d.data.name ; });

  // Transition nodes to their new position.
  nodeEnter.transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
      .style("opacity", 1);

  node.transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
      .style("opacity", 1)
    .select("rect")
      .style("fill", color);

  // Transition exiting nodes to the parent's new position.
  node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
      .style("opacity", 1e-6)
      .remove();

    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);
}

function color(d) {
  return d._children ? "#3182bd" : d.children ? "#c6dbef" : "lightgreen";
}
.node rect {
  cursor: pointer;
  fill: #fff;
  fill-opacity: .5;
  stroke: #3182bd;
  stroke-width: 1px;
}

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

path.link {
  fill: none;
  stroke: #9ecae1;
  stroke-width: 1.5px;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<body></body>

0 个答案:

没有答案