forceHelper.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. /*
  41. * A third-party license is embedded for some of the code in this file:
  42. * Some formulas were originally copied from "d3.js" with some
  43. * modifications made for this project.
  44. * (See more details in the comment of the method "step" below.)
  45. * The use of the source code of this file is also subject to the terms
  46. * and consitions of the license of "d3.js" (BSD-3Clause, see
  47. * </licenses/LICENSE-d3>).
  48. */
  49. import * as vec2 from 'zrender/lib/core/vector.js';
  50. var scaleAndAdd = vec2.scaleAndAdd; // function adjacentNode(n, e) {
  51. // return e.n1 === n ? e.n2 : e.n1;
  52. // }
  53. export function forceLayout(inNodes, inEdges, opts) {
  54. var nodes = inNodes;
  55. var edges = inEdges;
  56. var rect = opts.rect;
  57. var width = rect.width;
  58. var height = rect.height;
  59. var center = [rect.x + width / 2, rect.y + height / 2]; // let scale = opts.scale || 1;
  60. var gravity = opts.gravity == null ? 0.1 : opts.gravity; // for (let i = 0; i < edges.length; i++) {
  61. // let e = edges[i];
  62. // let n1 = e.n1;
  63. // let n2 = e.n2;
  64. // n1.edges = n1.edges || [];
  65. // n2.edges = n2.edges || [];
  66. // n1.edges.push(e);
  67. // n2.edges.push(e);
  68. // }
  69. // Init position
  70. for (var i = 0; i < nodes.length; i++) {
  71. var n = nodes[i];
  72. if (!n.p) {
  73. n.p = vec2.create(width * (Math.random() - 0.5) + center[0], height * (Math.random() - 0.5) + center[1]);
  74. }
  75. n.pp = vec2.clone(n.p);
  76. n.edges = null;
  77. } // Formula in 'Graph Drawing by Force-directed Placement'
  78. // let k = scale * Math.sqrt(width * height / nodes.length);
  79. // let k2 = k * k;
  80. var initialFriction = opts.friction == null ? 0.6 : opts.friction;
  81. var friction = initialFriction;
  82. var beforeStepCallback;
  83. var afterStepCallback;
  84. return {
  85. warmUp: function () {
  86. friction = initialFriction * 0.8;
  87. },
  88. setFixed: function (idx) {
  89. nodes[idx].fixed = true;
  90. },
  91. setUnfixed: function (idx) {
  92. nodes[idx].fixed = false;
  93. },
  94. /**
  95. * Before step hook
  96. */
  97. beforeStep: function (cb) {
  98. beforeStepCallback = cb;
  99. },
  100. /**
  101. * After step hook
  102. */
  103. afterStep: function (cb) {
  104. afterStepCallback = cb;
  105. },
  106. /**
  107. * Some formulas were originally copied from "d3.js"
  108. * https://github.com/d3/d3/blob/b516d77fb8566b576088e73410437494717ada26/src/layout/force.js
  109. * with some modifications made for this project.
  110. * See the license statement at the head of this file.
  111. */
  112. step: function (cb) {
  113. beforeStepCallback && beforeStepCallback(nodes, edges);
  114. var v12 = [];
  115. var nLen = nodes.length;
  116. for (var i = 0; i < edges.length; i++) {
  117. var e = edges[i];
  118. if (e.ignoreForceLayout) {
  119. continue;
  120. }
  121. var n1 = e.n1;
  122. var n2 = e.n2;
  123. vec2.sub(v12, n2.p, n1.p);
  124. var d = vec2.len(v12) - e.d;
  125. var w = n2.w / (n1.w + n2.w);
  126. if (isNaN(w)) {
  127. w = 0;
  128. }
  129. vec2.normalize(v12, v12);
  130. !n1.fixed && scaleAndAdd(n1.p, n1.p, v12, w * d * friction);
  131. !n2.fixed && scaleAndAdd(n2.p, n2.p, v12, -(1 - w) * d * friction);
  132. } // Gravity
  133. for (var i = 0; i < nLen; i++) {
  134. var n = nodes[i];
  135. if (!n.fixed) {
  136. vec2.sub(v12, center, n.p); // let d = vec2.len(v12);
  137. // vec2.scale(v12, v12, 1 / d);
  138. // let gravityFactor = gravity;
  139. scaleAndAdd(n.p, n.p, v12, gravity * friction);
  140. }
  141. } // Repulsive
  142. // PENDING
  143. for (var i = 0; i < nLen; i++) {
  144. var n1 = nodes[i];
  145. for (var j = i + 1; j < nLen; j++) {
  146. var n2 = nodes[j];
  147. vec2.sub(v12, n2.p, n1.p);
  148. var d = vec2.len(v12);
  149. if (d === 0) {
  150. // Random repulse
  151. vec2.set(v12, Math.random() - 0.5, Math.random() - 0.5);
  152. d = 1;
  153. }
  154. var repFact = (n1.rep + n2.rep) / d / d;
  155. !n1.fixed && scaleAndAdd(n1.pp, n1.pp, v12, repFact);
  156. !n2.fixed && scaleAndAdd(n2.pp, n2.pp, v12, -repFact);
  157. }
  158. }
  159. var v = [];
  160. for (var i = 0; i < nLen; i++) {
  161. var n = nodes[i];
  162. if (!n.fixed) {
  163. vec2.sub(v, n.p, n.pp);
  164. scaleAndAdd(n.p, n.p, v, friction);
  165. vec2.copy(n.pp, n.p);
  166. }
  167. }
  168. friction = friction * 0.992;
  169. var finished = friction < 0.01;
  170. afterStepCallback && afterStepCallback(nodes, edges, finished);
  171. cb && cb(finished);
  172. }
  173. };
  174. }