Advertisement
Guest User

Untitled

a guest
Aug 16th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. <html><head>
  2. <script src="http://d3js.org/d3.v4.min.js" charset="utf-8"></script>
  3. </head>
  4. <body>
  5. <div id="canvas" style="height: 600px; weight: 800px;"></div>
  6. <div id="toolbar" style="height: 600px; weight: 800px;">
  7. <h1 id="toolbarTitle"></h1>
  8. <p id="toolbarParagraph"></p>
  9. </div>
  10.  
  11. <style>
  12. .node {stroke: #fff; stroke-width: 1.5px;}
  13. .node text {font: 10px sans-serif; color: #000000;}
  14. .link {stroke: #999; stroke-opacity: 0.9;}
  15. </style>
  16.  
  17. <script type="text/javascript">
  18.  
  19. var width = 800, height = 600;
  20. var color = d3.scaleOrdinal(d3.schemeCategory10);
  21.  
  22. var simulation = d3.forceSimulation()
  23. .force('link', d3.forceLink().id(function (d) { return d.id;}).distance(100).strength(1))
  24. .force('charge', d3.forceManyBody())
  25. .force('center', d3.forceCenter(width/2, height/2));
  26.  
  27. var svg = d3.select('#canvas').select('svg');
  28. if (svg.empty()) {
  29. svg = d3.select('#canvas').append('svg')
  30. .attr('width', width)
  31. .attr('height', height);
  32. }
  33.  
  34. d3.json('graph.json', function(error, graph) {
  35. if (error) throw error;
  36. update(graph.links, graph.nodes);
  37.  
  38. function update(links, nodes) {
  39.  
  40. var link = svg.selectAll('.link')
  41. .data(graph.links)
  42. .enter().append('line')
  43. .attr('class', 'link');
  44.  
  45. var node = svg.selectAll('.node')
  46. .data(nodes)
  47. .enter().append('g')
  48. .attr('class', 'node')
  49. .call(d3.drag()
  50. .on('start', dragstarted)
  51. .on('drag', dragged)
  52. )
  53. .on('click', connectedNodes);
  54.  
  55. node.append('circle')
  56. .attr('r', 15)
  57. .style('fill', function(d) {
  58. return color(d.degree);
  59. });
  60.  
  61. simulation
  62. .nodes(nodes)
  63. .on('tick', ticked);
  64.  
  65. simulation.force('link').links(links);
  66.  
  67. function ticked() {
  68. link
  69. .attr("x1", function (d) {return d.source.x;})
  70. .attr("y1", function (d) {return d.source.y;})
  71. .attr("x2", function (d) {return d.target.x;})
  72. .attr("y2", function (d) {return d.target.y;});
  73.  
  74. node
  75. .attr("transform", function (d) {return "translate(" + d.x + ", " + d.y + ")";});
  76.  
  77. function dragstarted(d) {
  78. if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  79. d.fx = d.x;
  80. d.fy = d.y;
  81. }
  82.  
  83. function dragged(d) {
  84. d.fx = d3.event.x;
  85. d.fy = d3.event.y;
  86. }
  87.  
  88. function dragended(d) {
  89. if (!d3.event.active) simulation.alphaTarget(0);
  90. d.fx = null;
  91. d.fy = null;
  92. }
  93.  
  94. node.append('title')
  95. .text(function(d) {
  96. return "Node: " + d.id + "\n" + "Degree: " + d.degree + "\n" + "Katz: " + d.katz;
  97. });
  98.  
  99. var toggle = 0;
  100. var linkedByIndex = {};
  101. for (var i = 0; i < graph.nodes.length; i++) {
  102. linkedByIndex[i + "," + i] = 1;
  103. }
  104. graph.links.forEach(function(d) {
  105. linkedByIndex[d.source.index + "," + d.target.index] = 1;
  106. });
  107.  
  108. function neighboring(a, b) {
  109. return linkedByIndex[a.index + "," + b.index];
  110. }
  111.  
  112. function connectedNodes() {
  113. if (toggle == 0) {
  114. var d = d3.select(this).node().__data__;
  115. node.style("opacity", function(o) {
  116. return neighboring(d, o) || neighboring(o, d) ? 1 : 0.3;
  117. });
  118. node.attr('r', function(o) {
  119. return neighboring(d, o) || neighboring(o, d) ? 20 : 15;
  120. });
  121. link.style("opacity", function(o) {
  122. return d.index == o.source.index || d.index == o.target.index ? 1 : 0.8;
  123. });
  124. link.style('stroke-width', function(o) {
  125. return d.index == o.source.index || d.index == o.target.index ? 3 : 0.8;
  126. });
  127. toggle = 1;
  128. } else {
  129. node.style('opacity', 1);
  130. link.style('opacity', 1);
  131. link.style('stroke-width', 1);
  132. toggle = 0;
  133. }
  134. }
  135. }
  136. });
  137.  
  138. </script>
  139. </body>
  140. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement