d3.html 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title>树状图</title>
  5. <style>
  6. .node circle {
  7. fill: #fff;
  8. stroke: steelblue;
  9. stroke-width: 1.5px;
  10. }
  11. .node {
  12. font: 12px sans-serif;
  13. }
  14. .link {
  15. fill: none;
  16. stroke: #ccc;
  17. stroke-width: 1.5px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <script src="http://d3js.org/d3.v3.min.js"></script>
  23. <script>
  24. var width = 2000,
  25. height = 8000;
  26. var R = 600;
  27. var tree = d3.layout.tree()
  28. .size([360, 1000])
  29. .separation(function(a, b) { return (a.parent == b.parent ? 1 : 2); });
  30. var diagonal = d3.svg.diagonal()
  31. .projection(function(d) {
  32. var r = d.y, a = (d.x-90) / 180 * Math.PI;
  33. return [r * Math.cos(a), r * Math.sin(a)];
  34. });
  35. var svg = d3.select("body").append("svg")
  36. .attr("width", width)
  37. .attr("height", height)
  38. .append("g")
  39. .attr("transform", "translate(40,0)");
  40. d3.json("data.json", function(error, root) {
  41. var nodes = tree.nodes(root);
  42. var links = tree.links(nodes);
  43. console.log(nodes);
  44. console.log(links);
  45. var link = svg.selectAll(".link")
  46. .data(links)
  47. .enter()
  48. .append("path")
  49. .attr("class", "link")
  50. .attr("d", diagonal);
  51. var node = svg.selectAll(".node")
  52. .data(nodes)
  53. .enter()
  54. .append("g")
  55. .attr("class", "node")
  56. .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
  57. node.append("circle")
  58. .attr("r", 4.5);
  59. node.append("text")
  60. .attr("dx", function(d) { return d.children ? -8 : 8; })
  61. .attr("dy", 3)
  62. .style("text-anchor", function(d) { return d.children ? "end" : "start"; })
  63. .text(function(d) { return d.name; });
  64. });
  65. </script>
  66. </body>
  67. </html>