tree_test.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <style>
  4. .node {
  5. cursor: pointer;
  6. }
  7. .node circle {
  8. fill: #fff;
  9. stroke: steelblue;
  10. stroke-width: 1.5px;
  11. }
  12. .node text {
  13. font: 10px sans-serif;
  14. }
  15. .link {
  16. fill: none;
  17. stroke: #ccc;
  18. stroke-width: 1.5px;
  19. }
  20. </style>
  21. <body>
  22. <script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
  23. <script>
  24. var margin = {top: 20, right: 120, bottom: 20, left: 120},
  25. width = 960 - margin.right - margin.left,
  26. height = 800 - margin.top - margin.bottom;
  27. var i = 0,
  28. duration = 750,
  29. root;
  30. var tree = d3.layout.tree()
  31. .size([height, width]);
  32. var diagonal = d3.svg.diagonal()
  33. .projection(function(d) { return [d.y, d.x]; });
  34. var svg = d3.select("body").append("svg")
  35. .attr("width", width + margin.right + margin.left)
  36. .attr("height", height + margin.top + margin.bottom)
  37. .append("g")
  38. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  39. d3.json("http://mbostock.github.io/d3/talk/20111018/flare.json", function(error, flare) {
  40. if (error) throw error;
  41. root = flare;
  42. root.x0 = height / 2;
  43. root.y0 = 0;
  44. function collapse(d) {
  45. if (d.children) {
  46. d._children = d.children;
  47. d._children.forEach(collapse);
  48. d.children = null;
  49. }
  50. }
  51. root.children.forEach(collapse);
  52. update(root);
  53. });
  54. d3.select(self.frameElement).style("height", "800px");
  55. function update(source) {
  56. // Compute the new tree layout.
  57. var nodes = tree.nodes(root).reverse(),
  58. links = tree.links(nodes);
  59. // Normalize for fixed-depth.
  60. nodes.forEach(function(d) { d.y = d.depth * 180; });
  61. // Update the nodes…
  62. var node = svg.selectAll("g.node")
  63. .data(nodes, function(d) { return d.id || (d.id = ++i); });
  64. // Enter any new nodes at the parent's previous position.
  65. var nodeEnter = node.enter().append("g")
  66. .attr("class", "node")
  67. .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
  68. .on("click", click);
  69. nodeEnter.append("circle")
  70. .attr("r", 1e-6)
  71. .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
  72. nodeEnter.append("text")
  73. .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
  74. .attr("dy", ".35em")
  75. .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
  76. .text(function(d) { return d.name; })
  77. .style("fill-opacity", 1e-6);
  78. // Transition nodes to their new position.
  79. var nodeUpdate = node.transition()
  80. .duration(duration)
  81. .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
  82. nodeUpdate.select("circle")
  83. .attr("r", 4.5)
  84. .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
  85. nodeUpdate.select("text")
  86. .style("fill-opacity", 1);
  87. // Transition exiting nodes to the parent's new position.
  88. var nodeExit = node.exit().transition()
  89. .duration(duration)
  90. .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
  91. .remove();
  92. nodeExit.select("circle")
  93. .attr("r", 1e-6);
  94. nodeExit.select("text")
  95. .style("fill-opacity", 1e-6);
  96. // Update the links…
  97. var link = svg.selectAll("path.link")
  98. .data(links, function(d) { return d.target.id; });
  99. // Enter any new links at the parent's previous position.
  100. link.enter().insert("path", "g")
  101. .attr("class", "link")
  102. .attr("d", function(d) {
  103. var o = {x: source.x0, y: source.y0};
  104. return diagonal({source: o, target: o});
  105. });
  106. // Transition links to their new position.
  107. link.transition()
  108. .duration(duration)
  109. .attr("d", diagonal);
  110. // Transition exiting nodes to the parent's new position.
  111. link.exit().transition()
  112. .duration(duration)
  113. .attr("d", function(d) {
  114. var o = {x: source.x, y: source.y};
  115. return diagonal({source: o, target: o});
  116. })
  117. .remove();
  118. // Stash the old positions for transition.
  119. nodes.forEach(function(d) {
  120. d.x0 = d.x;
  121. d.y0 = d.y;
  122. });
  123. }
  124. // Toggle children on click.
  125. function click(d) {
  126. if (d.children) {
  127. d._children = d.children;
  128. d.children = null;
  129. } else {
  130. d.children = d._children;
  131. d._children = null;
  132. }
  133. update(d);
  134. }
  135. </script>