Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <style>
  4.  
  5. .node circle {
  6. stroke: white;
  7. stroke-width: 2px;
  8. opacity: 1.0;
  9. }
  10.  
  11. line {
  12. stroke-width: 3.5px;
  13. stroke-opacity: 1.0;
  14. }
  15.  
  16. </style>
  17. <body>
  18. <script src="http://d3js.org/d3.v3.js"></script>
  19. <script>
  20.  
  21. data = {
  22. nodes: [
  23. {size: 200, fill: '#ffffff', line: '#8C8C8C', id: 'Me'},
  24. {size: 30, fill: '#D2D2D2', line: '#ffffff', id: 'Bob'},
  25. {size: 30, fill: '#D2D2D2', line: '#ffffff', id: 'Alice'},
  26. {size: 30, fill: '#D2D2D2', line: '#ffffff', id: 'Tim Tim'},
  27. {size: 30, fill: '#D2D2D2', line: '#ffffff', id: 'Mustafa'},
  28. {size: 30, fill: '#D2D2D2', line: '#ffffff', id: 'Zeus'},
  29. {size: 30, fill: '#D2D2D2', line: '#ffffff', id: 'McHammer'},
  30. {size: 30, fill: '#D2D2D2', line: '#ffffff', id: 'Thoooor'},
  31. {size: 30, fill: '#D2D2D2', line: '#ffffff', id: 'Le Viking'}
  32. ],
  33. links: [
  34. {source: 0,target: 1, calls: 20, texts:0},
  35. {source: 0,target: 2, calls: 5, texts:0},
  36. {source: 0,target: 3, calls: 8, texts:0},
  37. {source: 0,target: 4, calls: 3, texts:0},
  38. {source: 0,target: 5, calls: 2, texts:0},
  39. {source: 0,target: 6, calls: 3, texts:0},
  40. {source: 0,target: 7, calls: 5, texts:0},
  41. {source: 0,target: 8, calls: 2, texts:0}
  42. ]
  43. }
  44.  
  45. var total_interactions = data.links.reduce(function(result, currentObject) {
  46. return result + currentObject.calls;
  47. }, 0);
  48. console.log(total_interactions);
  49.  
  50. var mouseOverFunction = function(d) {
  51. var circle = d3.select(this);
  52.  
  53. circle
  54. .transition(500)
  55. .attr("r", function(){ return 1.4 * node_radius(d)});
  56. }
  57.  
  58. var mouseOutFunction = function() {
  59. var circle = d3.select(this);
  60.  
  61. circle
  62. .transition(500)
  63. .attr("r", node_radius);
  64. }
  65.  
  66. function isConnected(a, b) {
  67. return isConnectedAsTarget(a, b) || isConnectedAsSource(a, b) || a.index == b.index;
  68. }
  69.  
  70. function isConnectedAsSource(a, b) {
  71. return linkedByIndex[a.index + "," + b.index];
  72. }
  73.  
  74. function isConnectedAsTarget(a, b) {
  75. return linkedByIndex[b.index + "," + a.index];
  76. }
  77.  
  78. function isEqual(a, b) {
  79. return a.index == b.index;
  80. }
  81.  
  82. var line_diff = 0
  83.  
  84. function tick() {
  85. callLink
  86. .attr("x1", function(d) { return d.source.x-line_diff; })
  87. .attr("y1", function(d) { return d.source.y-line_diff; })
  88. .attr("x2", function(d) { return d.target.x-line_diff; })
  89. .attr("y2", function(d) { return d.target.y-line_diff; });
  90. textLink
  91. .attr("x1", function(d) { return d.source.x+line_diff; })
  92. .attr("y1", function(d) { return d.source.y+line_diff; })
  93. .attr("x2", function(d) { return d.target.x+line_diff; })
  94. .attr("y2", function(d) { return d.target.y+line_diff; });
  95.  
  96. node
  97. .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
  98. }
  99.  
  100. function node_radius(d) { return Math.pow(40.0 * d.size, 1/3); }
  101.  
  102. var width = 1000;
  103. var height = 500;
  104.  
  105. var nodes = data.nodes
  106. var links = data.links
  107.  
  108. var force = d3.layout.force()
  109. .nodes(nodes)
  110. .links(links)
  111. .charge(-3000) // -3000
  112. .friction(0.6)
  113. .gravity(0.6)
  114. .size([width,height])
  115. .start();
  116.  
  117. var linkedByIndex = {};
  118. links.forEach(function(d) {
  119. linkedByIndex[d.source.index + "," + d.target.index] = true;
  120. });
  121.  
  122. var svg = d3.select("body").append("svg")
  123. .attr("width", width)
  124. .attr("height", height);
  125.  
  126. var callLink = svg.selectAll(".call-line")
  127. .data(links)
  128. .enter().append("line");
  129. var textLink = svg.selectAll(".text-line")
  130. .data(links)
  131. .enter().append("line");
  132. var link = svg.selectAll("line");
  133.  
  134. var node = svg.selectAll(".node")
  135. .data(nodes)
  136. .enter().append("g")
  137. .attr("class", "node")
  138. .call(force.drag);
  139.  
  140. node
  141. .append("circle")
  142. .attr("r", node_radius)
  143. .style("fill", function(d) {return d.fill; })
  144. .style("stroke", function(d) {return d.line; })
  145. .on("mouseover", mouseOverFunction)
  146. .on("mouseout", mouseOutFunction);
  147.  
  148. svg
  149. .append("marker")
  150. .attr("id", "arrowhead")
  151. .attr("refX", 6 + 7)
  152. .attr("refY", 2)
  153. .attr("markerWidth", 6)
  154. .attr("markerHeight", 4)
  155. .attr("orient", "auto")
  156. .append("path")
  157. .attr("d", "M 0,0 V 4 L6,2 Z");
  158.  
  159. // link
  160. // .style("stroke-width", function stroke(d) {return d.calls/data.total_interactions*20; })
  161.  
  162. var line_width_factor = 40 // width for a line with all points
  163.  
  164. textLink
  165. .style("stroke-width", function stroke(d) {return d.texts / total_interactions*line_width_factor; })
  166. .style("stroke", "#70C05A")
  167.  
  168. callLink
  169. .style("stroke-width", function stroke(d) {return d.calls / total_interactions*line_width_factor; })
  170. .style("stroke", "#438DCA")
  171.  
  172. force
  173. .on("tick",tick);
  174.  
  175. </script>
  176. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement