Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. const screen_width = 1000;
  2. const screen_height = 800;
  3. const node_size = 20;
  4.  
  5. const relation = {
  6. out: {
  7. l1: [
  8. tx1, // near
  9. tx2 // far
  10. ]
  11. },
  12. in: {
  13. r1 : [
  14. tx1, // near
  15. tx2, // far
  16. tx3
  17. ]
  18. }
  19. }
  20. var nodes = [];
  21. var edges = [];
  22. var centerNode = {
  23. id: 'center',
  24. x: screen_width / 2,
  25. y: screen_height / 2,
  26. shape: 'center-node',
  27. size: node_size
  28. };
  29. const nodeDistance = 40;
  30. nodes.push(centerNode);
  31. // 左侧添加 4 个节点
  32. // left
  33. const outSize = Object.keys(relation.out).length;
  34. const topY = centerNode.y - outSize / 2 * nodeDistance;
  35. Object.keys(relation.out).maps((ptyName, index) => {
  36. const ptyTxs = relation.out[ptyName];
  37. ptyTxs.map((tx, txInde) => {
  38.  
  39. nodes.push({
  40. id: `${ptyName}-${txIndex}`,
  41. x: centerNode.x - nodeDistance * index,
  42. y: topY + itxIndex * nodeDistance,
  43. shape: 'leaf-node'
  44. });
  45. edges.push({
  46. source: nodes[node.length -1].id,
  47. target: txIndex < 1 ? 'center' : nodes[node.length -2].id,
  48. shape: 'can-running'
  49. });
  50.  
  51. })
  52.  
  53. })
  54. for (var i = 0; i < 4; i++) {
  55. var id = 'left' + i;
  56. nodes.push({
  57. id: id,
  58. x: 250,
  59. y: (i + 1) * 100 + 50,
  60. shape: 'leaf-node'
  61. });
  62. edges.push({
  63. source: id,
  64. target: 'center',
  65. shape: 'can-running'
  66. });
  67. }
  68.  
  69. for (var i = 0; i < 6; i++) {
  70. var _id = 'right' + i;
  71. nodes.push({
  72. id: _id,
  73. x: 750,
  74. label: _id,
  75. y: i * 100 + 50,
  76. shape: 'leaf-node'
  77. });
  78. edges.push({
  79. label: "test",
  80. source: 'center',
  81. target: _id,
  82. shape: 'can-running'
  83. });
  84. }
  85. G6.registerNode('leaf-node', {
  86. afterDraw: function afterDraw(cfg, group) {
  87. group.addShape('circle', {
  88. attrs: {
  89. x: 0,
  90. y: 0,
  91. r: 5,
  92. fill: cfg.color || '#666'
  93. }
  94. });
  95. },
  96. getAnchorPoints: function getAnchorPoints() {
  97. return [[0, 0.5], [1, 0.5]];
  98. }
  99. }, 'circle');
  100. // 右侧添加 6 个节点
  101. G6.registerNode('center-node', {
  102. afterDraw: function afterDraw(cfg, group) {
  103. var r = cfg.size / 2;
  104. var back1 = group.addShape('circle', {
  105. zIndex: -3,
  106. attrs: {
  107. x: 0,
  108. y: 0,
  109. r: r + 10,
  110. fill: 'gray',
  111. opacity: 0.4
  112. }
  113. });
  114. var back2 = group.addShape('circle', {
  115. zIndex: -2,
  116. attrs: {
  117. x: 0,
  118. y: 0,
  119. r: r + 20,
  120. fill: 'gray', // 为了显示清晰,随意设置了颜色
  121. opacity: 0.2
  122. }
  123. });
  124. group.sort();
  125. },
  126. getAnchorPoints: function getAnchorPoints() {
  127. return [[0, 0.5], [1, 0.5]];
  128. }
  129. }, 'circle');
  130. // lineDash 的差值,可以在后面提供 util 方法自动计算
  131. var dashArray = [[0, 1], [0, 2], [1, 2], [0, 1, 1, 2], [0, 2, 1, 2], [1, 2, 1, 2], [2, 2, 1, 2], [3, 2, 1, 2], [4, 2, 1, 2]];
  132.  
  133. var lineDash = [4, 2, 1, 2];
  134. var interval = 9;
  135. G6.registerEdge('can-running', {
  136. setState: function setState(name, value, item) {
  137. var shape = item.get('keyShape');
  138. if (name === 'running') {
  139. if (value) {
  140. var length = shape.getTotalLength(); // 后续 G 增加 totalLength 的接口
  141. var totalArray = [];
  142. for (var i = 0; i < length; i += interval) {
  143. totalArray = totalArray.concat(lineDash);
  144. }
  145. var index = 0;
  146. shape.animate({
  147. onFrame: function onFrame(ratio) {
  148. var cfg = {
  149. lineDash: dashArray[index].concat(totalArray)
  150. };
  151. index = (index + 1) % interval;
  152. return cfg;
  153. },
  154.  
  155. repeat: true
  156. }, 3000);
  157. } else {
  158. shape.stopAnimate();
  159. shape.attr('lineDash', null);
  160. }
  161. }
  162. }
  163. }, 'cubic-horizontal');
  164. var graph = new G6.Graph({
  165. container: 'mountNode',
  166. width: window.innerWidth,
  167. height: window.innerHeight,
  168. edgeStyle: {
  169. default: {
  170. stroke: '#b5b5b5'
  171. }
  172. }
  173. });
  174. graph.data({
  175. nodes: nodes,
  176. edges: edges
  177. });
  178. graph.render();
  179. graph.fitView();
  180. graph.on('node:mouseenter', function(ev) {
  181. var node = ev.item;
  182. var edges = node.getEdges();
  183. edges.forEach(function(edge) {
  184. return graph.setItemState(edge, 'running', true);
  185. });
  186. });
  187.  
  188. graph.on('node:mouseleave', function(ev) {
  189. var node = ev.item;
  190. var edges = node.getEdges();
  191. edges.forEach(function(edge) {
  192. return graph.setItemState(edge, 'running', false);
  193. });
  194. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement