atlas.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. $(function() {
  2. var name = $('.input_text')
  3. $('.input_sub').on('click', function() {
  4. if ($('.input_text').val() == '') {
  5. alert('请输入')
  6. } else {
  7. fetchJSON('api/algorithmCall/findExperimentKg?experimentId=' + $('.input_text').val(), {}, "get", function(
  8. data) {
  9. if (data.status === 0) {
  10. var res_data = data.data;
  11. console.log(res_data)
  12. var links = [];
  13. for (var i = 0; i < res_data.length; i++) {
  14. var str = {
  15. sid: res_data[i].startNodeId,
  16. tid: res_data[i].endNodeId,
  17. source: res_data[i].startNode,
  18. target: res_data[i].endNode,
  19. type: "resolved",
  20. rela: res_data[i].relation
  21. }
  22. links.push(str)
  23. }
  24. console.log(links)
  25. var nodes = {};
  26. links.forEach(function(link) {
  27. link.source = nodes[link.source] || (nodes[link.source] = {
  28. name: link.source,
  29. id: link.sid
  30. });
  31. link.target = nodes[link.target] || (nodes[link.target] = {
  32. name: link.target,
  33. id: link.tid
  34. });
  35. });
  36. var width = 1100,
  37. height = 600;
  38. var force = d3.layout.force() //layout将json格式转化为力学图可用的格式
  39. .nodes(d3.values(nodes)) //设定节点数组
  40. .links(links) //设定连线数组
  41. .size([width, height]) //作用域的大小
  42. .linkDistance(180) //连接线长度
  43. .charge(-1500) //顶点的电荷数。该参数决定是排斥还是吸引,数值越小越互相排斥
  44. .on("tick", tick) //指时间间隔,隔一段时间刷新一次画面
  45. .start(); //开始转换
  46. var svg = d3.select("body").append("svg")
  47. .attr("width", width)
  48. .attr("height", height);
  49. //箭头
  50. var marker =
  51. svg.append("marker")
  52. .attr("id", "resolved")
  53. .attr("markerUnits", "userSpaceOnUse")
  54. .attr("viewBox", "0 -5 10 10") //坐标系的区域
  55. .attr("refX", 32) //箭头坐标
  56. .attr("refY", -1)
  57. .attr("markerWidth", 12) //标识的大小
  58. .attr("markerHeight", 12)
  59. .attr("orient", "auto") //绘制方向,可设定为:auto(自动确认方向)和 角度值
  60. .attr("stroke-width", 2) //箭头宽度
  61. .append("path")
  62. .attr("d", "M0,-5L10,0L0,5") //箭头的路径
  63. .attr('fill', 'gray'); //箭头颜色
  64. //设置连接线    
  65. var edges_line = svg.selectAll(".edgepath")
  66. .data(force.links())
  67. .enter()
  68. .append("path")
  69. .attr({
  70. 'd': function(d) {
  71. return 'M ' + d.source.x + ' ' + d.source.y + ' L ' + d
  72. .target.x + ' ' + d.target.y
  73. },
  74. 'class': 'edgepath',
  75. 'id': function(d, i) {
  76. return 'edgepath' + i;
  77. }
  78. })
  79. .style("stroke", "gray")
  80. .style("pointer-events", "none")
  81. .style("stroke-width", 0.5) //线条粗细
  82. .attr("marker-end", "url(#resolved)"); //根据箭头标记的id号标记箭头
  83. var edges_text = svg.append("g").selectAll(".edgelabel")
  84. .data(force.links())
  85. .enter()
  86. .append("text")
  87. .style("pointer-events", "none")
  88. .attr({
  89. 'class': 'edgelabel',
  90. 'id': function(d, i) {
  91. return 'edgepath' + i;
  92. },
  93. 'dx': 80,
  94. 'dy': 0,
  95. //'font-size':10,
  96. //                'fill':'#aaa'
  97. });
  98. //设置线条上的文字
  99. edges_text.append('textPath')
  100. .attr('xlink:href', function(d, i) {
  101. return '#edgepath' + i
  102. })
  103. .style("pointer-events", "none")
  104. .text(function(d) {
  105. return d.rela;
  106. });
  107. //圆圈
  108. var circle = svg.append("g").selectAll("circle")
  109. .data(force.nodes()) //表示使用force.nodes数据
  110. .enter().append("circle")
  111. .style("fill", "green")
  112. .attr("r", 28) //设置圆圈半径
  113. .call(force.drag); //将当前选中的元素传到drag函数中,使顶点可以被拖动
  114. //圆圈的提示文字
  115. circle.append("svg:title")
  116. .text(function(node) {
  117. return "双击可查看详情";
  118. });
  119. var text = svg.append("g").selectAll("text")
  120. .data(force.nodes())
  121. //返回缺失元素的占位对象(placeholder),指向绑定的数据中比选定元素集多出的一部分元素。
  122. .enter()
  123. .append("text")
  124. .attr("dy", ".35em")
  125. .attr("text-anchor", "middle") //在圆圈中加上数据  
  126. .style('fill', "white")
  127. .attr('x', function(d) {
  128. // console.log(d.name+"---"+ d.name.length);
  129. var re_en = /[a-zA-Z]+/g;
  130. //如果是全英文,不换行
  131. if (d.name.match(re_en)) {
  132. d3.select(this).append('tspan')
  133. .attr('x', 0)
  134. .attr('y', 2)
  135. .text(function() {
  136. return d.name;
  137. });
  138. }
  139. // 如果小于四个字符,不换行
  140. else
  141. if (d.name.length <= 4) {
  142. d3.select(this).append('tspan')
  143. .attr('x', 0)
  144. .attr('y', 2)
  145. .text(function() {
  146. return d.name;
  147. });
  148. } else {
  149. var top = d.name.substring(0, 4);
  150. var bot = d.name.substring(4, d.name.length);
  151. d3.select(this).text(function() {
  152. return '';
  153. });
  154. d3.select(this).append('tspan')
  155. .attr('x', 0)
  156. .attr('y', -7)
  157. .text(function() {
  158. return top;
  159. });
  160. d3.select(this).append('tspan')
  161. .attr('x', 0)
  162. .attr('y', 10)
  163. .text(function() {
  164. return bot;
  165. });
  166. }
  167. });
  168. function tick() {
  169. circle.attr("transform", transform1); //圆圈
  170. text.attr("transform", transform2); //顶点文字
  171. edges_line.attr('d', function(d) {
  172. var path = 'M ' + d.source.x + ' ' + d.source.y + ' L ' + d
  173. .target.x + ' ' + d.target.y;
  174. return path;
  175. });
  176. edges_text.attr('transform', function(d, i) {
  177. if (d.target.x < d.source.x) {
  178. bbox = this.getBBox();
  179. rx = bbox.x + bbox.width / 2;
  180. ry = bbox.y + bbox.height / 2;
  181. return 'rotate(180 ' + rx + ' ' + ry + ')';
  182. } else {
  183. return 'rotate(0)';
  184. }
  185. });
  186. }
  187. //设置连接线的坐标,使用椭圆弧路径段双向编码
  188. function linkArc(d) {
  189. return 'M ' + d.source.x + ' ' + d.source.y + ' L ' + d.target.x + ' ' +
  190. d
  191. .target.y
  192. }
  193. //设置圆圈和文字的坐标
  194. function transform1(d) {
  195. return "translate(" + d.x + "," + d.y + ")";
  196. }
  197. function transform2(d) {
  198. return "translate(" + (d.x) + "," + d.y + ")";
  199. }
  200. } else {
  201. }
  202. });
  203. }
  204. $('.input_sub').off('click')
  205. })
  206. })