Guest User

Untitled

a guest
Oct 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. var width = 960,
  2. height = 500;
  3.  
  4. var radius = 30; /* radius of circles */
  5. var numCircles = 6; /* number of circles - you must update link source/target values to match changes in the number of circles */
  6. var d3LineLinear = d3.svg.line().interpolate("linear");
  7. var d3color = d3.interpolateRgb("#BAE4B3", "#006D2C"); /* color range for flow lines */
  8.  
  9. //A LIST OF LINKS BETWEEN CIRCLES
  10. var links = [
  11. {source: 0, target: 5, strength: Math.round(Math.random()*10)},
  12. {source: 0, target: 2, strength: Math.round(Math.random()*10)},
  13. {source: 1, target: 3, strength: Math.round(Math.random()*10)},
  14. {source: 2, target: 4, strength: Math.round(Math.random()*10)},
  15. {source: 3, target: 5, strength: Math.round(Math.random()*10)},
  16. {source: 5, target: 0, strength: Math.round(Math.random()*10)},
  17. {source: 2, target: 0, strength: Math.round(Math.random()*10)},
  18. {source: 3, target: 1, strength: Math.round(Math.random()*10)}
  19. ];
  20.  
  21. function createDefs(defs) {
  22. var dropShadowFilter = defs.append('svg:filter')
  23. .attr('id', 'dropShadow');
  24. dropShadowFilter.append('svg:feGaussianBlur')
  25. .attr('in', 'SourceAlpha')
  26. .attr('stdDeviation', 1);
  27. dropShadowFilter.append('svg:feOffset')
  28. .attr('dx', 0)
  29. .attr('dy', 1)
  30. .attr('result', 'offsetblur');
  31. var feMerge = dropShadowFilter.append('svg:feMerge');
  32. feMerge.append('svg:feMergeNode');
  33. feMerge.append('svg:feMergeNode')
  34. .attr('in', "SourceGraphic");
  35. }
  36.  
  37. var drag = d3.behavior.drag()
  38. .origin(Object)
  39. .on("drag", function(){ dragmove(this); });
  40.  
  41. //RANDOMLY GENERATE COORDINATES FOR CIRCLES
  42. var circles = d3.range(numCircles).map(function(i, d) { return [Math.round(50 + (i/numCircles)*(width - 50)), Math.round(30 + Math.random()*(height - 80))]; });
  43.  
  44. //GLOBAL STRENGTH SCALE
  45. var strength_scale = d3.scale.linear()
  46. .range([2, 10]) /* thickness range for flow lines */
  47. .domain([0, d3.max(links, function(d) { return d.strength; })]);
  48.  
  49. var color_scale = d3.scale.linear()
  50. .range([0, 1])
  51. .domain([0, d3.max(links, function(d) { return d.strength; })]);
  52.  
  53. var svg = d3.select("body").append("svg")
  54. .attr("width", width)
  55. .attr("height", height);
  56.  
  57. var g_lines = svg.append("g")
  58. .attr("class","lines");
  59. var g_circles = svg.append("g")
  60. .attr("class","circles");
  61. var g_midpoints = svg.append("g")
  62. .attr("class","midpoints");
  63.  
  64. //SHADOW DEFINITION
  65. createDefs(svg.append('svg:defs'));
  66.  
  67. $.each(circles, function(i, d) {
  68. g_circles.append("circle")
  69. .attr('filter', 'url(#dropShadow)')
  70. .attr("class","circle")
  71. .attr("id", "circle" + i)
  72. .attr("r", radius)
  73. .attr("cx", d[0])
  74. .attr("cy", d[1])
  75. .call(drag);
  76. });
  77.  
  78. g_lines.selectAll(".link_line").data(links).enter().append("path")
  79. .attr("class", "link_line")
  80. .attr("fill", function(d) { return d3color(color_scale(d.strength)); })
  81. .attr("id", function(i, d) { return "link_line" + d; } )
  82. .attr("d", function(d){ return drawCurve(d); });
  83.  
  84. function dragmove(dragged) {
  85. var x = d3.select(dragged).attr("cx");
  86. var y = d3.select(dragged).attr("cy");
  87. d3.select(dragged)
  88. .attr("cx", Math.max(radius, Math.min(width - radius, +x + d3.event.dx)))
  89. .attr("cy", Math.max(radius, Math.min(height - radius, +y + d3.event.dy)));
  90. $.each(links, function(i, link){
  91. if (link.source == dragged.id.match(/\d+/)[0] || link.target == dragged.id.match(/\d+/)[0]) {
  92. d3.select('#link_line' + i).attr("d", function(d){ return drawCurve(d); });
  93. }
  94. });
  95. }
  96.  
  97. function drawCurve(d) {
  98. var slope = Math.atan2((+d3.select('#circle' + d.target).attr("cy") - d3.select('#circle' + d.source).attr("cy")), (+d3.select('#circle' + d.target).attr("cx") - d3.select('#circle' + d.source).attr("cx")));
  99. var slopePlus90 = Math.atan2((+d3.select('#circle' + d.target).attr("cy") - d3.select('#circle' + d.source).attr("cy")), (+d3.select('#circle' + d.target).attr("cx") - d3.select('#circle' + d.source).attr("cx"))) + (Math.PI/2);
  100.  
  101. var sourceX = +d3.select('#circle' + d.source).attr("cx");
  102. var sourceY = +d3.select('#circle' + d.source).attr("cy");
  103. var targetX = +d3.select('#circle' + d.target).attr("cx");
  104. var targetY = +d3.select('#circle' + d.target).attr("cy");
  105.  
  106. var points = [];
  107.  
  108. var bothDirections = -1;
  109. $.each(links, function(index, value) {
  110. if(value.source == d.target && value.target == d.source) {
  111. bothDirections = index;
  112. return false;
  113. }
  114. });
  115.  
  116. if(bothDirections >= 0) {
  117. points.push([sourceX - 0.5*radius*Math.cos(slope), sourceY - 0.5*radius*Math.sin(slope)]);
  118. points.push([targetX + radius*Math.cos(slope) + (strength_scale(links[bothDirections].strength)) * Math.cos(slopePlus90), targetY + radius*Math.sin(slope) + (strength_scale(links[bothDirections].strength)) * Math.sin(slopePlus90)]);
  119. points.push([sourceX - 0.5*radius*Math.cos(slope) + 2 * strength_scale(links[bothDirections].strength) * Math.cos(slopePlus90), sourceY - 0.5*radius*Math.sin(slope) + 2 * strength_scale(links[bothDirections].strength) * Math.sin(slopePlus90)]);
  120. } else {
  121. points.push([sourceX - radius*Math.cos(slope) - strength_scale(d.strength) * Math.cos(slopePlus90), sourceY - radius*Math.sin(slope) - strength_scale(d.strength) * Math.sin(slopePlus90)]);
  122. points.push([targetX + radius * Math.cos(slope), targetY + radius * Math.sin(slope)]);
  123. points.push([sourceX - radius*Math.cos(slope) + strength_scale(d.strength) * Math.cos(slopePlus90), sourceY - radius*Math.sin(slope) + strength_scale(d.strength) * Math.sin(slopePlus90)]);
  124. }
  125.  
  126.  
  127. return d3LineLinear(points) + "Z";
  128. }
Add Comment
Please, Sign In to add comment