Guest User

Untitled

a guest
Feb 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <title>Modifying a force layout v4</title>
  4.  
  5. <style>
  6.  
  7. .link {
  8. stroke: #000;
  9. stroke-width: 1.5px;
  10. }
  11.  
  12. .node {
  13. fill: #000;
  14. stroke: #fff;
  15. stroke-width: 1.5px;
  16. }
  17.  
  18.  
  19. .node.a { fill: #1f77b4; }
  20. .node.b { fill: #ff7f0e; }
  21. .node.c { fill: #2ca02c; }
  22.  
  23.  
  24. </style>
  25. <body>
  26. <script src="//d3js.org/d3.v4.js"></script>
  27. <script>
  28.  
  29. var width = 960,
  30. height = 500;
  31.  
  32. var color = d3.scaleOrdinal(d3.schemeCategory10);
  33.  
  34. var nodes = [],
  35. links = [];
  36.  
  37.  
  38.  
  39. var simulation = d3.forceSimulation()
  40. .force("link", d3.forceLink().distance(200).strength(.6))
  41. .force("charge", d3.forceManyBody())
  42. // use forceX and forceY instead to change the relative positioning
  43. // .force("centering", d3.forceCenter(width/2, height/2))
  44. .force("x", d3.forceX(width/2))
  45. .force("y", d3.forceY(height/2))
  46. .on("tick", tick);
  47.  
  48.  
  49. var svg = d3.select("body").append("svg")
  50. .attr("width", width)
  51. .attr("height", height);
  52.  
  53. var a = createNode("a");
  54. var b = createNode("b");
  55. var c = createNode("c");
  56.  
  57. nodes= [a];
  58. start();
  59.  
  60. // 1. add the links
  61. setTimeout(function() {
  62. nodes= [a,b,c];
  63. links.push({source: a, target: b}, {source: a, target: c}, {source: b, target: c});
  64. start();
  65. }, 1000);
  66.  
  67. // 2. Remove node B and associated links.
  68. setTimeout(function() {
  69. // remove b
  70. nodes = [a,c];
  71. // remove a-b
  72. links = [links[1]];
  73. start();
  74. }, 2000);
  75.  
  76.  
  77. // Add node B back.
  78. setTimeout(function() {
  79. b = createNode("b")
  80. nodes.push(b);
  81. links.push({source: a, target: b}, {source: b, target: c});
  82. start();
  83. }, 3000);
  84.  
  85. function createNode(id) {
  86. return {id: id, x: width/2, y:height/2}
  87. }
  88.  
  89. function start() {
  90.  
  91. var nodeElements = svg.selectAll(".node").data(nodes, function(d){return d.id});
  92. var linkElements = svg.selectAll(".link").data(links);
  93.  
  94. nodeElements.enter().append("circle").attr("class", function(d) {return "node " + d.id; }).attr("r", 8);
  95. linkElements.enter().insert("line", ".node").attr("class", "link");
  96.  
  97. nodeElements.exit().remove();
  98. linkElements.exit().remove();
  99.  
  100. simulation.nodes(nodes)
  101. simulation.force("link").links(links)
  102. simulation.restart();
  103. }
  104.  
  105. function tick() {
  106. var nodeElements = svg.selectAll(".node");
  107. var linkElements = svg.selectAll(".link");
  108.  
  109. nodeElements.attr("cx", function(d,i) {return d.x; })
  110. .attr("cy", function(d) { return d.y; })
  111.  
  112. linkElements.attr("x1", function(d) { return d.source.x; })
  113. .attr("y1", function(d) { return d.source.y; })
  114. .attr("x2", function(d) { return d.target.x; })
  115. .attr("y2", function(d) { return d.target.y; });
  116.  
  117.  
  118. }
  119.  
  120. </script>
Add Comment
Please, Sign In to add comment