Guest User

Untitled

a guest
Nov 19th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. d3.select('.link, #bacteria in suspended poc_other suspension feeders').node()
  2. <line class="link" id="phytoplankton_ciliates" x1="230" x2="130.65489174816582" y1="0" y2="189.286289155541"></line>
  3.  
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <title> Subject Clusters </title>
  8. <style>
  9.  
  10. .link {
  11. fill: none;
  12. stroke: #888888;
  13. stroke-weight: 1px;
  14. opacity: 0.4;
  15. }
  16.  
  17. </style>
  18. <script src="https://d3js.org/d3.v4.min.js"></script>
  19.  
  20. </head>
  21.  
  22.  
  23. <svg id = "canvas"></svg>
  24. <body>
  25.  
  26. <script>
  27.  
  28. diameter = 500;
  29. radius = diameter / 2
  30.  
  31. d3.select("#canvas")
  32. .attr("width", diameter)
  33. .attr("height", diameter)
  34.  
  35.  
  36. var plot = d3.select("#canvas")
  37. .append("g")
  38. .attr("id", "plot")
  39. .attr("transform", "translate(" + radius + "," + radius + ")");
  40.  
  41. d3.json("test.json", function(data) {
  42. the_data = data
  43.  
  44. circular_layout(data.nodes);
  45. create_nodes(data.nodes)
  46. match_data(data);
  47. draw_links(data.links);
  48. });
  49.  
  50. <!-- compute circular coordinates -->
  51. function circular_layout(node_list){
  52.  
  53. var scale = d3.scaleLinear()
  54. .domain([0, node_list.length])
  55. .range([0,2*Math.PI]);
  56.  
  57.  
  58. node_list.forEach( function(d,i) {
  59. var theta = scale(i);
  60. var radial = radius - 20;
  61.  
  62. d.x = radial*Math.cos(theta);
  63. d.y = radial*Math.sin(theta);
  64. });
  65. };
  66.  
  67. <!-- create nodes -->
  68. function create_nodes(node_list){
  69.  
  70. d3.select("#plot").selectAll(".node")
  71. .data(node_list)
  72. .enter()
  73. .append("circle")
  74. .attr("class", "node")
  75. .attr("id", function(d) { return d.name })
  76. .attr("cx", function(d) { return d.x })
  77. .attr("cy", function(d) { return d.y })
  78. .attr("fill", "black")
  79. .attr("r", 5);
  80.  
  81. }
  82.  
  83.  
  84. <!-- convert node names into objects containing (x,y) coordinates -->
  85. function match_data(graph_data){
  86.  
  87. names = [];
  88. graph_data.nodes.forEach( function(d,i) {
  89. names[i] = d.name[0]
  90. })
  91.  
  92. graph_data.links.forEach( function(d,i) {
  93. var v1 = d.from[0];
  94. var v2 = d.to[0]
  95.  
  96. d.from = graph_data.nodes[ names.indexOf(v1) ];
  97. d.to = graph_data.nodes[ names.indexOf(v2) ];
  98.  
  99. })
  100. }
  101.  
  102. <!-- create links -->
  103. function draw_links(link_list){
  104.  
  105. d3.select("#plot").selectAll(".link")
  106. .data(link_list)
  107. .enter()
  108. .append("line")
  109. .attr("class", "link")
  110. .attr("id", function(d){ return d.from.name[0] + "_" + d.to.name[0]} ) <!-- this is where IDs are assigned -->
  111. .attr("x1", function(d){ return d.from.x; })
  112. .attr("x2", function(d){ return d.to.x; })
  113. .attr("y1", function(d){ return d.from.y; })
  114. .attr("y2", function(d){ return d.to.y; });
  115.  
  116. }
  117. </script>
  118. </body>
  119. </html>
Add Comment
Please, Sign In to add comment