atlas.js 6.6 KB

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