Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. var svg;
  2. var cursor;
  3. var node;
  4. var link;
  5. var nodes ;
  6. var force;
  7. var width = 960,
  8. var height = 500;
  9.  
  10. var fill = d3.scale.category20();
  11. var maquina = { "name": "Máquina", "children": [] };
  12. var material = { "name": "Material", "children": [] };
  13. var medida = { "name": "Medida", "children": [] };
  14. var maodeobra = { "name": "Mão-de-obra", "children": [] };
  15. var meioambiente = { "name": "Meio ambiente", "children": [] };
  16. var metodo = { "name": "Método", "children": [] };
  17.  
  18. var data2 = {
  19. "name": "Efeito",
  20. "children": [
  21.  
  22. ],
  23. "type": "user"
  24. }
  25.  
  26.  
  27. data2.children.push(maquina);
  28. data2.children.push(material);
  29. data2.children.push(medida);
  30. data2.children.push(maodeobra);
  31. data2.children.push(meioambiente);
  32. data2.children.push(metodo);
  33.  
  34.  
  35. var fishbone = d3.fishbone();
  36.  
  37. var size = (function(){
  38. return {width: this.clientWidth, height: this.clientHeight};
  39. }).bind(window.document.documentElement);
  40. //x = w.innerWidth || e.clientWidth || g.clientWidth;
  41. //y = w.innerHeight|| e.clientHeight|| g.clientHeight;
  42.  
  43. // var width = 500, height = 500;
  44. var creation=Date.now();
  45. d3.select(window).on('resize.updatesvg', updateWindow);
  46. svg = d3.select("#fishbone")
  47. .append("svg")
  48.  
  49. // firefox needs a real size
  50. .attr(size())
  51. .on("mousemove", mousemove).on("mousedown", mousedownCanvas)
  52. //.on('mouseover', function(d){
  53. // var nodeSelection = d3.select(this).style({opacity:'0.8'});
  54. // nodeSelection.select("text").style({opacity:'5.0'});
  55. //})
  56. // .attr("width", width)
  57. //.attr("height", height)
  58. // .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
  59. // set the data so the reusable chart can find it
  60. //.attr('id','rect_'+creation)
  61. //.attr('onclick',"removeRect('rect_"+"')")
  62. .datum(data2)
  63. // set up the default arrowhead
  64. .call(fishbone.defaultArrow)
  65. // call the selection modifier
  66. .call(fishbone);
  67.  
  68.  
  69. force = d3.layout.force()
  70. .size([ this.clientWidth, this.clientHeight])
  71. .nodes([{}]) // initialize with a single node
  72. .linkDistance(30)
  73. .charge(-60)
  74. .on("tick", tick);
  75.  
  76. cursor = svg.append("circle")
  77. .attr("r", 30)
  78. .attr("transform", "translate(-100,-100)")
  79. .attr("class", "cursor");
  80.  
  81. nodes = force.nodes(),
  82. links = force.links(),
  83. node = svg.selectAll(".node"),
  84. link = svg.selectAll(".link");
  85.  
  86. // svg.on("mouseover", function()
  87. // {d3.select(this).style("fill", "aliceblue");})
  88. //.on("mouseout", function(){d3.select(this).style("fill", "white");});
  89.  
  90. fishbone.force().start();
  91.  
  92. function tick() {
  93. link.attr("x1", function(d) { return d.source.x; })
  94. .attr("y1", function(d) { return d.source.y; })
  95. .attr("x2", function(d) { return d.target.x; })
  96. .attr("y2", function(d) { return d.target.y; });
  97.  
  98. node.attr("cx", function(d) { return d.x; })
  99. .attr("cy", function(d) { return d.y; });
  100. }
  101.  
  102. function restart() {
  103. node = node.data(nodes);
  104. //alert(node);
  105. //node.enter().insert("circle", ".cursor")
  106. // .attr("class", "node")
  107. // .attr("r", 5)
  108. // .on("mousedown", mousedownNode);
  109.  
  110. //node.exit()
  111. // .remove();
  112.  
  113. // link = link.data(links);
  114.  
  115. //link.enter().insert("line", ".node")
  116. // .attr("class", "link");
  117. //link.exit()
  118. // .remove();
  119.  
  120. force.start();
  121. }
  122. function mousemove() {
  123. cursor.attr("transform", "translate(" + d3.mouse(this) + ")");
  124. }
  125.  
  126. function mousedownCanvas() {
  127. var point = d3.mouse(this),
  128. node = {x: point[0], y: point[1]},
  129. n = nodes.push(node);
  130.  
  131. // add links to any nearby nodes
  132. nodes.forEach(function(target) {
  133. var x = target.x - node.x,
  134. y = target.y - node.y;
  135. if (Math.sqrt(x * x + y * y) < 30) {
  136. links.push({source: node, target: target});
  137. }
  138. });
  139.  
  140. restart();
  141. }
  142.  
  143.  
  144. function mousedownNode(d, i) {
  145. nodes.splice(i, 1);
  146. links = links.filter(function(l) {
  147. return l.source !== d && l.target !== d;
  148. });
  149. d3.event.stopPropagation();
  150.  
  151. restart();
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement