Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. Shiny.addCustomMessageHandler("jsondata", function(links) {
  2. console.log("got new data:", links);
  3. console.log(links[0]["Source"]);
  4.  
  5. //for(i=0;i<links.length;i++) {
  6. //var allnodes += links[i]["Source"] ;
  7. //}
  8.  
  9. var width = 950, height=1000,color = d3.scale.category20();
  10.  
  11. var svg = d3.select("body").append("svg")
  12. .attr("width", width)
  13. .attr("height",height);
  14.  
  15. var force = d3.layout.force()
  16. .size([width,height])
  17. .linkDistance(130)
  18. .charge(function (d) {return d.weight * - 150});
  19.  
  20.  
  21. var nodesByName = {};
  22.  
  23. //console.log(links);
  24. //console.log(typeof(links));
  25.  
  26. //create nodes for each unique source and target in csv
  27. links.forEach(function(link) {
  28. link.source = nodeByName(link.Source);
  29. link.target = nodeByName(link.Target);
  30. });
  31.  
  32. //extract array of nodes from map by name
  33. var nodes = d3.values(nodesByName);
  34.  
  35. //create the link lines
  36. var link = svg.selectAll(".link")
  37. .data(links)
  38. .enter().append("line")
  39. .attr("class", "link");
  40.  
  41. // Create the node circles.
  42. var node = svg.selectAll(".node")
  43. .data(nodes)
  44. .enter().append("circle")
  45. .attr("class", "node")
  46. .attr("r", 4.5)
  47. .style("fill", function(d) { return color(d.name); })
  48. .call(force.drag);
  49.  
  50.  
  51.  
  52. //node.append("svg:text")
  53. // .text(function (d) { return d.source; })
  54. // .attr("x", function(d) { return d.x; })
  55. // .attr("y", function(d) { return d.y; });
  56.  
  57. node.append("text")
  58. .attr("x",12)
  59. .attr("dy",".35em")
  60. .text(function(d) { return d.name; });
  61.  
  62.  
  63. // Start the force layout.
  64. force.nodes(nodes)
  65. .links(links)
  66. .on("tick", tick)
  67. .start();
  68.  
  69. function tick() {
  70. link.attr("x1", function(d) { return d.source.x; })
  71. .attr("y1", function(d) { return d.source.y; })
  72. .attr("x2", function(d) { return d.target.x; })
  73. .attr("y2", function(d) { return d.target.y; });
  74.  
  75. node.attr("cx", function(d) { return d.x; })
  76. .attr("cy", function(d) { return d.y; });
  77. }
  78.  
  79. function nodeByName(name) {
  80. return nodesByName[name] || (nodesByName[name] = {name: name});
  81. }
  82. });
  83.  
  84. No Source Target value
  85. 1 a b 3
  86. 2 b c 9
  87. 3 a c 7
  88. 4 b e 4
  89. 5 c d 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement