Advertisement
Guest User

Untitled

a guest
Jan 30th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 6.20 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <style>
  4.  
  5. /*body { font: 12px Arial;}*/
  6.  
  7. .node {
  8.   cursor: pointer;
  9. }
  10.  
  11. .node:hover {
  12.   stroke: #000;
  13.   stroke-width: 1.5px;
  14. }
  15.  
  16. .node--leaf {
  17.   fill: white;
  18. }
  19.  
  20. .label {
  21.   font: 0px "Helvetica Neue", Helvetica, Arial, sans-serif;
  22.   text-anchor: middle;
  23.   text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff, 0 -1px 0 #fff;
  24. }
  25.  
  26. .label,
  27. .node--root,
  28. .node--leaf {
  29.   pointer-events: none;
  30. }
  31.  
  32. </style>
  33. <script src="https://d3js.org/d3.v4.min.js"></script>
  34.  
  35.  
  36. <div id="area1"></div>
  37. <div id="area2"></div>
  38. <body>
  39. <script>
  40. var width = 960,
  41.     height = 960,
  42.     margin = 30;
  43.  
  44. var svgs = d3.select('#area1')
  45.     .selectAll('svg')
  46.     .data([twitter_profiles.json, facebook3.json])
  47.     .enter()
  48.     .append('svg')
  49.     .attr('width', width)
  50.     .attr('height', height)
  51.  
  52. svgs.append("g")
  53.     .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
  54.     .each(function(d) {console.log(d)})
  55.  
  56. // var svg2 = d3.select("svg"),
  57. //     margin = 20,
  58. //     diameter = +svg2.attr("width"),
  59. //     g = svg2.append("g").attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
  60.  
  61. var color = d3.scaleLinear()
  62.     .domain([-1, 5])
  63.     .range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
  64.     .interpolate(d3.interpolateHcl);
  65.  
  66. var pack = d3.pack()
  67.     .size([diameter - margin, diameter - margin])
  68.     .padding(2);
  69.  
  70. // d3.json("twitter_profiles.json", function(error, root) {
  71. //   if (error) throw error;
  72.  
  73.   root = d3.hierarchy(root)
  74.       .sum(function(d) { return d.size; })
  75.       .sort(function(a, b) { return b.value - a.value; });
  76.  
  77.   var focus = root,
  78.       nodes = pack(root).descendants(),
  79.       view;
  80.  
  81.   var circle = g.selectAll("circle")
  82.     .data(nodes)
  83.     .enter().append("circle")
  84.       .attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
  85.       .style("fill", function(d) { return d.children ? color(d.depth) : null; })
  86.       .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); });
  87.  
  88.   var text = g.selectAll("text")
  89.     .data(nodes)
  90.     .enter().append("text")
  91.       .attr("class", "label")
  92.       .style("fill-opacity", function(d) { return d.parent === root ? 1 : 0; })
  93.       .style("display", function(d) { return d.parent === root ? "inline" : "none"; })
  94.       .text(function(d) { return d.data.name; });
  95.  
  96.   var node = g.selectAll("circle,text");
  97.  
  98.   svg2
  99.       .style("background", color(-1))
  100.       .on("click", function() { zoom(root); });
  101.  
  102.   zoomTo([root.x, root.y, root.r * 2 + margin]);
  103.  
  104.   function zoom(d) {
  105.     var focus0 = focus; focus = d;
  106.  
  107.     var transition = d3.transition()
  108.         .duration(d3.event.altKey ? 7500 : 750)
  109.         .tween("zoom", function(d) {
  110.           var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
  111.           return function(t) { zoomTo(i(t)); };
  112.         });
  113.  
  114.     transition.selectAll("text")
  115.       .filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
  116.         .style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
  117.         .on("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
  118.         .on("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
  119.   }
  120.  
  121.   function zoomTo(v) {
  122.     var k = diameter / v[2]; view = v;
  123.     node.attr("transform", function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
  124.     circle.attr("r", function(d) { return d.r * k; });
  125.   }
  126. });
  127.  
  128.  
  129.  
  130.  
  131. </script>
  132. </body>
  133. <!-- <p>facebook</p>
  134.  
  135. <script>
  136. var facebookgraph = d3.select("svg"),
  137.    margin = 20,
  138.    diameter = +svg.attr("width"),
  139.    g = svg.append("g").attr("transform", "translate(" + diameter / 2 + "," + diameter / 2 + ")");
  140.  
  141. var color = d3.scaleLinear()
  142.    .domain([-1, 5])
  143.    .range(["hsl(152,80%,80%)", "hsl(228,30%,40%)"])
  144.    .interpolate(d3.interpolateHcl);
  145.  
  146. var pack = d3.pack()
  147.    .size([diameter - margin, diameter - margin])
  148.    .padding(2);
  149.  
  150. d3.json("facebook3.json", function(error, root) {
  151.  if (error) throw error;
  152.  
  153.  root = d3.hierarchy(root)
  154.      .sum(function(d) { return d.size; })
  155.      .sort(function(a, b) { return b.value - a.value; });
  156.  
  157.  var focus = root,
  158.      nodes = pack(root).descendants(),
  159.      view;
  160.  
  161.  var circle = g.selectAll("circle")
  162.    .data(nodes)
  163.    .enter().append("circle")
  164.      .attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
  165.      .style("fill", function(d) { return d.children ? color(d.depth) : null; })
  166.      .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); });
  167.  
  168.  var text = g.selectAll("text")
  169.    .data(nodes)
  170.    .enter().append("text")
  171.      .attr("class", "label")
  172.      .style("fill-opacity", function(d) { return d.parent === root ? 1 : 0; })
  173.      .style("display", function(d) { return d.parent === root ? "inline" : "none"; })
  174.      .text(function(d) { return d.data.name; });
  175.  
  176.  var node = g.selectAll("circle,text");
  177.  
  178.  facebookgraph
  179.      .style("background", color(-1))
  180.      .on("click", function() { zoom(root); });
  181.  
  182.  zoomTo([root.x, root.y, root.r * 2 + margin]);
  183.  
  184.  function zoom(d) {
  185.    var focus0 = focus; focus = d;
  186.  
  187.    var transition = d3.transition()
  188.        .duration(d3.event.altKey ? 7500 : 750)
  189.        .tween("zoom", function(d) {
  190.          var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
  191.          return function(t) { zoomTo(i(t)); };
  192.        });
  193.  
  194.    transition.selectAll("text")
  195.      .filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
  196.        .style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
  197.        .on("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
  198.        .on("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
  199.  }
  200.  
  201.  function zoomTo(v) {
  202.    var k = diameter / v[2]; view = v;
  203.    node.attr("transform", function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
  204.    circle.attr("r", function(d) { return d.r * k; });
  205.  }
  206. });
  207.  
  208. </script>
  209. </script>
  210. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement