垂直折叠树图

时间:2015-07-25 09:39:34

标签: javascript d3.js svg

使用书中的示例 D3提示和技巧我制作了垂直折叠/展开树状图/树形图。

see what I did here! (the problem)

到现在为止还挺好。 ......然而,
Q1 :我希望地图开始折叠,即只显示第一个节点。 (我找到了关于堆栈溢出的答案(几乎找到它,但是它使用了flare图表,我无法在我的图表上使用更改后的代码。)
Q2 :节点从左下角飞出,但我希望它们将第一个节点作为原点。摆弄各种设置,没有任何帮助。
Q3 :我希望节点“kwaliteit”成为指向另一页的超链接。
有人有答案吗?
这是我的代码:

<script src="http://d3js.org/d3.v3.min.js"></script>
    <script>

    var treeData = [
  {
    "name": "jaar",
    "parent": "null",
    "children": [
      {
        "name": "2014",
        "parent": "jaar",
        "children": [
          {
            "name": "kwaliteit",
            "parent": "2014"
          },
          {
            "name": "geen data",
            "parent": "2014"
          }
        ]
      },
      {
        "name": "2015 (geen data)",
        "parent": "jaar"
      }
    ]
  }
];
// ************** Generate the tree diagram  *****************
var margin = {top: 20, right: 120, bottom: 20, left: 120},
    width = 960 - margin.right - margin.left,
    height = 500 - margin.top - margin.bottom;

var i = 0,
    duration = 750,
    root;
var tree = d3.layout.tree()
    .size([width, height]);
var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.x, d.y]; });
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 + ")");
root = treeData[0];
root.x0 = height / 2;
root.y0 = 0;




update(root);
d3.select(self.frameElement).style("height", "500px");
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 * 100; });
  // 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("y", function(d) { 
    return d.children || d._children ? -18 : 18; })
   .attr("dy", ".35em")
   .attr("text-anchor", "middle")
   .text(function(d) { return d.name; })
   .style("fill-opacity", 1);



  // Transition nodes to their new position.
  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);
  // 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>

1 个答案:

答案 0 :(得分:2)

Q1:我希望地图开始折叠,即只显示第一个节点

最简单的方法是重命名所有children个属性,然后将其称为_children

var treeData = [
    {
        "name": "jaar",
        "parent": "null",
        "_children": [
        {
            "name": "2014",
            "parent": "jaar",
            "_children": [
                {
                    "name": "kwaliteit",
                    "parent": "2014"
                },
                {
                    "name": "geen data",
                    "parent": "2014"
                }
            ]
        },
        {
            "name": "2015 (geen data)",
            "parent": "jaar"
        }
        ]
    }
];

Q2:节点从左下角飞出,但我希望它们将第一个节点作为原点。

只需在enter()

中交换x0和y0值即可
var nodeEnter = node.enter().append("g")
    .attr("class", "node")
    .attr("transform", function (d) {
        return "translate(" + source.x0 + "," + source.y0 + ")";
    })
    .on("click", click);

问题3:我喜欢节点&#34; kwaliteit&#34;成为超链接

附加a并通过查看文本设置超链接(或者,您也可以将其放入数据结构中)

.append("a")
.attr("href", function (d) { if (d.name === "kwaliteit") return "myurl" })

如果您无法在Chrome中使用它,请使用此代替

.on("click", function (d) { if (d.name === "kwaliteit") window.location.href = "myurl" })

小提琴 - http://jsfiddle.net/gaa4v3gu/