Advertisement
Guest User

d3-v4-force-layout-with-labels

a guest
Sep 20th, 2016
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.66 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <title>grSpotify</title>
  6.     <style>
  7.         .nodes circle {
  8.             fill: #ccc;
  9.             stroke: #fff;
  10.             stroke-width: 3px;
  11.             stroke-opacity: 0.8;
  12.         }
  13.         .links line {
  14.             stroke: #777;
  15.             stroke-width: 1.5px;
  16.             stroke-opacity: 0.6;
  17.         }
  18.         .gnode text {
  19.             font: 10px sans-serif;
  20.             transform: translate(5px, 0px);
  21.         }
  22.     </style>
  23. </head>
  24. <body>
  25.     <svg width="1024" height="768"></svg>
  26.     <script src="http://d3js.org/d3.v4.min.js"></script>
  27.     <script>
  28.         var svg = d3.select("svg"), width = +svg.attr("width"), height = +svg.attr("height");
  29.         var color = d3.scaleOrdinal(d3.schemeCategory20);
  30.         var simulation = d3.forceSimulation()
  31.             .force("link", d3.forceLink().id(function(d) { return d.id; }))
  32.             .force("charge", d3.forceManyBody().strength(-500))
  33.             .force("center", d3.forceCenter(width/2, height/2));
  34.         d3.json("data.json", function(error, graph) {
  35.             if (error) throw error;
  36.             var link = svg.append("g").attr("class", "links")
  37.                 .selectAll("line")
  38.                 .data(graph.links)
  39.                 .enter().append("line")
  40.                     .attr("stroke-width", function(d) { return Math.sqrt(d.value); });
  41.             var gnodes = svg.selectAll("g.gnode")
  42.                 .data(graph.nodes)
  43.                 .enter()
  44.                 .append("g")
  45.                 .classed("gnode", true);
  46.             var node = gnodes.append("circle")
  47.                 .attr("class", "nodes")
  48.                 .attr("r", 5)
  49.                 .style("fill", function(d) { return color(d.group); })
  50.                 .call(d3.drag()
  51.                     .on("start", dragstarted)
  52.                     .on("drag", dragged)
  53.                     .on("end", dragended));
  54.             gnodes.append("title").text(function(d) { return d.label });
  55.             var labels = gnodes.append("text").text(function(d) { return d.label; });
  56.             labels.attr("transform", function(d) {
  57.                     return "translate(" + (d.x) + "," + (d.y) + ")";
  58.             });
  59.             simulation.nodes(graph.nodes).on("tick", ticked);
  60.             simulation.force("link").links(graph.links);
  61.             function ticked() {
  62.                 link
  63.                     .attr("x1", function(d) { return d.source.x; })
  64.                     .attr("y1", function(d) { return d.source.y; })
  65.                     .attr("x2", function(d) { return d.target.x; })
  66.                     .attr("y2", function(d) { return d.target.y; });
  67.                 gnodes
  68.                 //node
  69.                     .attr("transform", function(d) {
  70.                         return "translate(" + [d.x, d.y] + ")";});
  71.                 labels.attr("transform", function(d) { return "translate(" + (d.x) + "," + (d.y) + ")"; });
  72.             }
  73.         });
  74.         function dragstarted(d) {
  75.             if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  76.             d.fx = d.x;
  77.             d.fy = d.y;
  78.         }
  79.         function dragged(d) {
  80.             d.fx = d3.event.x;
  81.             d.fy = d3.event.y;
  82.         }
  83.         function dragended(d) {
  84.             if (!d3.event.active) simulation.alphaTarget(0);
  85.             d.fx = null;
  86.             d.fy = null;
  87.         }
  88.     </script>
  89. </body>
  90. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement