simple_tree_test.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Collapsible Tree Example</title>
  6. <style>
  7. .node circle {
  8. fill: #fff;
  9. stroke: steelblue;
  10. stroke-width: 3px;
  11. }
  12. .node text { font: 12px sans-serif; }
  13. .link {
  14. fill: none;
  15. stroke: #ccc;
  16. stroke-width: 2px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <!-- load the d3.js library -->
  22. <script src="http://d3js.org/d3.v3.min.js"></script>
  23. <script>
  24. var treeData = [
  25. {
  26. "name": "Top Level",
  27. "parent": "null",
  28. "children": [
  29. {
  30. "name": "Level 2: A",
  31. "parent": "Top Level",
  32. "children": [
  33. {
  34. "name": "Son of A",
  35. "parent": "Level 2: A"
  36. },
  37. {
  38. "name": "Daughter of A",
  39. "parent": "Level 2: A"
  40. }
  41. ]
  42. },
  43. {
  44. "name": "Level 2: B",
  45. "parent": "Top Level"
  46. }
  47. ]
  48. }
  49. ];
  50. // ************** Generate the tree diagram *****************
  51. var margin = {top: 40, right: 120, bottom: 20, left: 120},
  52. width = 960 - margin.right - margin.left,
  53. height = 500 - margin.top - margin.bottom;
  54. var i = 0;
  55. var tree = d3.layout.tree()
  56. .size([width, height]);
  57. var diagonal = d3.svg.diagonal();
  58. var svg = d3.select("body").append("svg")
  59. .attr("width", width + margin.right + margin.left)
  60. .attr("height", height + margin.top + margin.bottom)
  61. .append("g")
  62. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  63. root = treeData[0];
  64. update(root);
  65. function update(source) {
  66. // Compute the new tree layout.
  67. console.log(source);
  68. var nodes = tree.nodes(source).reverse(),
  69. links = tree.links(nodes);
  70. // Normalize for fixed-depth.
  71. nodes.forEach(function(d) { d.y = d.depth * 100; });
  72. // Declare the nodes…
  73. var node = svg.selectAll("g.node")
  74. .data(nodes, function(d) { return d.id || (d.id = ++i); });
  75. // Enter the nodes.
  76. var nodeEnter = node.enter().append(function(n) {console.log(n); return document.createElementNS("http://www.w3.org/2000/svg", "g");})
  77. .attr("class", "node")
  78. .attr("transform", function(d) {
  79. return "translate(" + d.x + "," + d.y + ")"; });
  80. nodeEnter.append("circle")
  81. .attr("r", 10)
  82. .style("fill", "#fff");
  83. nodeEnter.append("text")
  84. .attr("y", function(d) {
  85. return d.children || d._children ? -18 : 18; })
  86. .attr("dy", ".35em")
  87. .attr("text-anchor", "middle")
  88. .text(function(d) { return d.name; })
  89. .style("fill-opacity", 1);
  90. // Declare the links…
  91. var link = svg.selectAll("path.link")
  92. .data(links, function(d) { return d.target.id; });
  93. // Enter the links.
  94. link.enter().insert("path", "g")
  95. .attr("class", "link")
  96. .attr("d", diagonal);
  97. }
  98. </script>
  99. </body>
  100. </html>