2
0

packd3.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <style>
  4. .node {
  5. cursor: pointer;
  6. }
  7. .node:hover {
  8. stroke: #000;
  9. stroke-width: 1.5px;
  10. }
  11. .node--leaf {
  12. fill: white;
  13. }
  14. .label {
  15. font: 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
  16. text-anchor: middle;
  17. text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff, 0 -1px 0 #fff;
  18. }
  19. .label,
  20. .node--root,
  21. .node--leaf {
  22. pointer-events: none;
  23. }
  24. </style>
  25. <body>
  26. <script src="http://d3js.org/d3.v3.min.js"></script>
  27. <script>
  28. var margin = 20,
  29. diameter = 500;
  30. var color = d3.scale.linear()
  31. .domain([-1, 5])
  32. .range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
  33. .interpolate(d3.interpolateHcl);
  34. var pack = d3.layout.pack()
  35. .padding(2)
  36. .size([diameter - margin, diameter - margin])
  37. .value(function(d) { return 1/*d.size*/; })
  38. var svg = d3.select("body").append("svg")
  39. .attr("width", diameter)
  40. .attr("height", diameter)
  41. .append("g")
  42. .attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
  43. d3.json("flare.json", function(error, root) {
  44. if (error) throw error;
  45. var focus = root,
  46. nodes = pack.nodes(root),
  47. view;
  48. var circle = svg.selectAll("circle")
  49. .data(nodes)
  50. .enter().append("circle")
  51. .attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
  52. .style("fill", function(d) { return d.children ? color(d.depth) : null; })
  53. .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); });
  54. var text = svg.selectAll("text")
  55. .data(nodes)
  56. .enter().append("text")
  57. .attr("class", "label")
  58. .style("fill-opacity", function(d) { return d.parent === root ? 1 : 0; })
  59. .style("display", function(d) { return d.parent === root ? "inline" : "none"; })
  60. .text(function(d) { return d.name; });
  61. var node = svg.selectAll("circle,text");
  62. d3.select("body")
  63. .style("background", color(-1))
  64. .on("click", function() { zoom(root); });
  65. zoomTo([root.x, root.y, root.r * 2 + margin]);
  66. function zoom(d) {
  67. var focus0 = focus; focus = d;
  68. var transition = d3.transition()
  69. .duration(d3.event.altKey ? 7500 : 750)
  70. .tween("zoom", function(d) {
  71. var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
  72. return function(t) { zoomTo(i(t)); };
  73. });
  74. transition.selectAll("text")
  75. .filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
  76. .style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
  77. .each("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
  78. .each("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
  79. }
  80. function zoomTo(v) {
  81. var k = diameter / v[2]; view = v;
  82. node.attr("transform", function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
  83. circle.attr("r", function(d) { return d.r * k; });
  84. }
  85. });
  86. d3.select(self.frameElement).style("height", diameter + "px");
  87. </script>