d6.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 data = [
  25. { "name" : "csdn.blog.world.python", "parent":"csdn.blog.world" },
  26. { "name" : "csdn.blog.world", "parent":"null" },
  27. { "name" : "csdn.blog.world.python_a", "parent":"csdn.blog.world.python" },
  28. { "name" : "csdn.blog.world.python_b", "parent":"csdn.blog.world.python" },
  29. { "name" : "csdn.blog.world.python_c", "parent":"csdn.blog.world.python" },
  30. { "name" : "csdn.blog.world.golang", "parent":"csdn.blog.world" },
  31. { "name" : "csdn.blog.world.c", "parent":"csdn.blog.world" },
  32. { "name" : "csdn.blog.world.java", "parent":"csdn.blog.world" }
  33. ];
  34. // *********** Convert flat data into a nice tree ***************
  35. // create a name: node map
  36. var dataMap = data.reduce(function(map, node) {
  37. map[node.name] = node;
  38. return map;
  39. }, {});
  40. // create the tree array
  41. var treeData = [];
  42. data.forEach(function(node) {
  43. // add to parent
  44. var parent = dataMap[node.parent];
  45. if (parent) {
  46. // create child array if it doesn't exist
  47. (parent.children || (parent.children = []))
  48. // add node to child array
  49. .push(node);
  50. } else {
  51. // parent is null or missing
  52. treeData.push(node);
  53. }
  54. });
  55. // ************** Generate the tree diagram *****************
  56. var margin = {top: 20, right: 120, bottom: 20, left: 120},
  57. width = 960 - margin.right - margin.left,
  58. height = 500 - margin.top - margin.bottom;
  59. var i = 0;
  60. console.log(width,height);
  61. width=2000;
  62. height=1600;
  63. var tree = d3.layout.tree()
  64. .size([height, width]);
  65. var diagonal = d3.svg.diagonal()
  66. .projection(function(d) { return [d.y, d.x]; });
  67. var svg = d3.select("body").append("svg")
  68. .attr("width", width + margin.right + margin.left)
  69. .attr("height", height + margin.top + margin.bottom)
  70. .append("g")
  71. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  72. root = treeData[0];
  73. update(root);
  74. function update(source) {
  75. // Compute the new tree layout.
  76. var nodes = tree.nodes(root).reverse(),
  77. links = tree.links(nodes);
  78. // Normalize for fixed-depth.
  79. nodes.forEach(function(d) { d.y = d.depth * 180; });
  80. // Declare the nodes…
  81. var node = svg.selectAll("g.node")
  82. .data(nodes, function(d) { return d.id || (d.id = ++i); });
  83. // Enter the nodes.
  84. var nodeEnter = node.enter().append("g")
  85. .attr("class", "node")
  86. .attr("transform", function(d) {
  87. return "translate(" + d.y + "," + d.x + ")"; });
  88. nodeEnter.append("circle")
  89. .attr("r", 10)
  90. .style("fill", "#fff");
  91. nodeEnter.append("text")
  92. .attr("x", function(d) {
  93. return d.children || d._children ? -13 : 13; })
  94. .attr("dy", ".35em")
  95. .attr("text-anchor", function(d) {
  96. return d.children || d._children ? "end" : "start"; })
  97. .text(function(d) { return d.name; })
  98. .style("fill-opacity", 1);
  99. // Declare the links…
  100. var linkEnter = svg.selectAll("path.link").append("g")
  101. .data(links, function(d) { return d.target.id; });
  102. linkEnter.enter().insert("path", "g")//在指定元素之前插入一个元素
  103. .attr("class", "link")
  104. .attr("d", diagonal)
  105. // 首先为每条节点连线添加标识id
  106. .attr("id", function(d, i){
  107. return "mypath" + i;
  108. });
  109. //为连线添加文字
  110. linkEnter.enter().append('text')
  111. .attr('x', 90)
  112. .attr('y', 20)
  113. .style('fill', 'green')
  114. .style('font-size', '10px')
  115. .style('font-weight', 'bold')
  116. .append('textPath')
  117. .attr({//引用路径
  118. 'xlink:href': function(d, i){
  119. return "#mypath" + i;
  120. }
  121. })
  122. .text(function (d, i) {
  123. return i + '_Service.Pay()'
  124. });
  125. }
  126. </script>
  127. </body>
  128. </html>