Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. var svg = d3.select('#word-network-preview-' + scope.card._id)
  2. .append('svg')
  3. .attr("width", width)
  4. .attr("height", height);
  5.  
  6. var zoom = d3.zoom()
  7. .scaleExtent([.1, 1])
  8. .on("zoom", zoomed);
  9.  
  10. svg.call(zoom);
  11.  
  12. var color = d3.scaleOrdinal(d3.schemeCategory20);
  13.  
  14. var simulation = d3.forceSimulation()
  15. .force("link", d3.forceLink().id(function(d) { return d.id; }).distance(function(d) { return 100+(Math.random()*50) ; }).strength(0.5))
  16. .force("charge", d3.forceManyBody().distanceMax(40))
  17. .force("center", d3.forceCenter(width / 2, height / 2));
  18.  
  19. var link = svg.append("g")
  20. .attr("class", "links")
  21. .selectAll("line")
  22. .data(scope.graph.links)
  23. .enter().append("line")
  24. .attr("stroke", "#ddd")
  25. .attr("stroke-width", function(d) { return Math.sqrt(d.value); });
  26.  
  27. var node = svg.append("g")
  28. .attr("class", "nodes")
  29. .selectAll("circle")
  30. .data(scope.graph.nodes)
  31. .enter().append("circle")
  32. .attr("r", function(d) { return 10; })
  33. .attr("fill", function(d) { return color(d.group); })
  34. .call(d3.drag()
  35. .on("start", dragstarted)
  36. .on("drag", dragged)
  37. .on("end", dragended));
  38.  
  39. var labels = svg.append("g")
  40. .attr("class", "label")
  41. .selectAll("text")
  42. .data(scope.graph.nodes)
  43. .enter().append("text")
  44. .attr("dx", (function(d) { return 10; }))
  45. .attr("dy", ".35em")
  46. .text(function(d) { return d.id });
  47.  
  48. simulation
  49. .nodes(scope.graph.nodes)
  50. .on("tick", ticked);
  51.  
  52. // connections
  53. simulation.force("link")
  54. .links(scope.graph.links);
  55. // end connectons
  56.  
  57. function ticked() {
  58. link
  59. .attr("x1", function(d) { return d.source.x; })
  60. .attr("y1", function(d) { return d.source.y; })
  61. .attr("x2", function(d) { return d.target.x; })
  62. .attr("y2", function(d) { return d.target.y; });
  63.  
  64. node
  65. .attr("cx", function(d) { return d.x; })
  66. .attr("cy", function(d) { return d.y; });
  67.  
  68. labels
  69. .attr("x", function(d) { return d.x; })
  70. .attr("y", function(d) { return d.y; });
  71. }
  72.  
  73. function dragstarted(d) {
  74. if (!d3.event.active)
  75. simulation.alphaTarget(0.3).restart();
  76.  
  77. d.fx = d.x;
  78. d.fy = d.y;
  79. }
  80.  
  81. function dragged(d) {
  82. d.fx = d3.event.x;
  83. d.fy = d3.event.y;
  84. }
  85.  
  86. function dragended(d) {
  87. if (!d3.event.active) simulation.alphaTarget(0);
  88. d.fx = null;
  89. d.fy = null;
  90. }
  91.  
  92. function zoomed() {
  93. svg.attr("transform", d3.event.transform);
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement