Guest User

Untitled

a guest
Jan 22nd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. var w = 960,
  2. h = 700,
  3. fill = d3.scale.category20();
  4.  
  5. var vis = d3.select("#chart")
  6. .append("svg:svg")
  7. .attr("width", w)
  8. .attr("height", h);
  9.  
  10. vis.append("svg:g").attr("class", "edges");
  11. vis.append("svg:g").attr("class", "nodes");
  12.  
  13.  
  14. $(function() {
  15. $.ajax({
  16. url: 'egonet.json',
  17. //crossDomain: true,
  18. //dataType: 'jsonp',
  19. success : function(json) {
  20.  
  21. var force;
  22.  
  23. var nodes_by_id = _.reduce(json.node_metadata, function(acc, n) {
  24. acc[n._id] = n;
  25. return acc;
  26. }, {});
  27.  
  28.  
  29. var follows_edges = _(json.is_followed_by).chain().map(function(e) {
  30. e.source = nodes_by_id[e.from];
  31. e.target = nodes_by_id[e.to];
  32. e.type = 'follows_edges';
  33. return e;
  34. }).filter(function(e){
  35. return nodes_by_id[e.from] && nodes_by_id[e.to]
  36. }).value();
  37.  
  38. var mentions_edges = _(json.is_mentioned_by).chain().map(function(e) {
  39. e.source = nodes_by_id[e.from];
  40. e.target = nodes_by_id[e.to];
  41. e.type = 'mentions_edges';
  42. return e;
  43. }).filter(function(e){
  44. return nodes_by_id[e.from] && nodes_by_id[e.to]
  45. }).value();
  46.  
  47.  
  48.  
  49.  
  50. nodes_by_id[189087228].x = w/2.0;
  51. nodes_by_id[189087228].y = h/2.0;
  52.  
  53. var force = d3.layout.force()
  54. .linkStrength(0.5)
  55. .charge(-2000)
  56. .friction(0.7)
  57. .linkDistance(50)
  58. .nodes([])
  59. .links([])
  60. .size([w, h])
  61. .start();
  62.  
  63. function update(edges){
  64.  
  65. _.each(nodes_by_id, function(n){n.added = false});
  66.  
  67. var nodes = _.reduce(edges, function(acc, e) {
  68. if(nodes_by_id[e.from] && !nodes_by_id[e.from].added){
  69. nodes_by_id[e.from].added = true;
  70. acc.push(nodes_by_id[e.from]);
  71. }
  72. if(nodes_by_id[e.to] && !nodes_by_id[e.to].added){
  73. nodes_by_id[e.to].added = true;
  74. acc.push(nodes_by_id[e.to]);
  75. }
  76. return acc;
  77. }, []);
  78.  
  79. force.nodes(nodes);
  80. force.links(edges);
  81. force.start();
  82.  
  83. var link = d3.select("#chart g.edges").selectAll("line.link")
  84. .data(edges, function(e){return e.from + "-" + e.to + "-" + e.type});
  85.  
  86. link.enter().append("svg:line")
  87. .attr("class", "link")
  88. .style("stroke-width", function(d) {
  89. return Math.sqrt(d.value);
  90. })
  91. .attr("x1", function(d) {
  92. return d.source.x;
  93. })
  94. .attr("y1", function(d) {
  95. return d.source.y;
  96. })
  97. .attr("x2", function(d) {
  98. return d.target.x;
  99. })
  100. .attr("y2", function(d) {
  101. return d.target.y;
  102. });
  103.  
  104. link.exit().remove();
  105.  
  106. var node = d3.select("#chart g.nodes").selectAll("g.node").data(nodes);
  107.  
  108. var new_g = node.enter().append("svg:g")
  109. .attr("class", "node")
  110. .call(force.drag);
  111.  
  112. new_g.append("svg:image").attr('xlink:href',
  113. function(d) {
  114. return d.profile_image_url;
  115. }).attr('height', 32).attr('width', 32);
  116.  
  117. new_g.append("svg:text")
  118. .attr("dy", 46)
  119. .attr("text-anchor", "middle")
  120. .text(function(d) {
  121. return d.screen_name;
  122. });
  123.  
  124. node.exit().remove();
  125.  
  126.  
  127. force.on("tick", function() {
  128.  
  129. var x_center = $("#chart").width() / 2;
  130. var y_center = $("#chart").height() / 2;
  131.  
  132. link.attr("x1", function(d) { return d.source.x; })
  133. .attr("y1", function(d) { return d.source.y; })
  134. .attr("x2", function(d) { return d.target.x; })
  135. .attr("y2", function(d) { return d.target.y; });
  136.  
  137. node.attr("transform", function(d) { return "translate(" + (d.x-16) + "," + (d.y-16) + ")"; });
  138.  
  139. });
  140. }
  141.  
  142. update(follows_edges);
  143. vis.style("opacity", 1e-6)
  144. .transition()
  145. .duration(1000)
  146. .style("opacity", 1);
  147.  
  148. $('input#follows').change(function(){
  149. update(follows_edges);
  150. });
  151.  
  152. $('input#mentions').change(function(){
  153. update(mentions_edges);
  154. });
  155. }
  156. });
  157.  
  158. $("#relation_type").buttonset();
  159.  
  160. $('input#mentions').change(function(){console.log(this)});
  161. $('input#hashtags').change(function(){console.log(this)});
  162.  
  163. });
Add Comment
Please, Sign In to add comment