multipleGraphEdgeHelper.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. // @ts-nocheck
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. var KEY_DELIMITER = '-->';
  43. /**
  44. * params handler
  45. * @param {module:echarts/model/SeriesModel} seriesModel
  46. * @returns {*}
  47. */
  48. var getAutoCurvenessParams = function (seriesModel) {
  49. return seriesModel.get('autoCurveness') || null;
  50. };
  51. /**
  52. * Generate a list of edge curvatures, 20 is the default
  53. * @param {module:echarts/model/SeriesModel} seriesModel
  54. * @param {number} appendLength
  55. * @return 20 => [0, -0.2, 0.2, -0.4, 0.4, -0.6, 0.6, -0.8, 0.8, -1, 1, -1.2, 1.2, -1.4, 1.4, -1.6, 1.6, -1.8, 1.8, -2]
  56. */
  57. var createCurveness = function (seriesModel, appendLength) {
  58. var autoCurvenessParmas = getAutoCurvenessParams(seriesModel);
  59. var length = 20;
  60. var curvenessList = [];
  61. // handler the function set
  62. if (zrUtil.isNumber(autoCurvenessParmas)) {
  63. length = autoCurvenessParmas;
  64. } else if (zrUtil.isArray(autoCurvenessParmas)) {
  65. seriesModel.__curvenessList = autoCurvenessParmas;
  66. return;
  67. }
  68. // append length
  69. if (appendLength > length) {
  70. length = appendLength;
  71. }
  72. // make sure the length is even
  73. var len = length % 2 ? length + 2 : length + 3;
  74. curvenessList = [];
  75. for (var i = 0; i < len; i++) {
  76. curvenessList.push((i % 2 ? i + 1 : i) / 10 * (i % 2 ? -1 : 1));
  77. }
  78. seriesModel.__curvenessList = curvenessList;
  79. };
  80. /**
  81. * Create different cache key data in the positive and negative directions, in order to set the curvature later
  82. * @param {number|string|module:echarts/data/Graph.Node} n1
  83. * @param {number|string|module:echarts/data/Graph.Node} n2
  84. * @param {module:echarts/model/SeriesModel} seriesModel
  85. * @returns {string} key
  86. */
  87. var getKeyOfEdges = function (n1, n2, seriesModel) {
  88. var source = [n1.id, n1.dataIndex].join('.');
  89. var target = [n2.id, n2.dataIndex].join('.');
  90. return [seriesModel.uid, source, target].join(KEY_DELIMITER);
  91. };
  92. /**
  93. * get opposite key
  94. * @param {string} key
  95. * @returns {string}
  96. */
  97. var getOppositeKey = function (key) {
  98. var keys = key.split(KEY_DELIMITER);
  99. return [keys[0], keys[2], keys[1]].join(KEY_DELIMITER);
  100. };
  101. /**
  102. * get edgeMap with key
  103. * @param edge
  104. * @param {module:echarts/model/SeriesModel} seriesModel
  105. */
  106. var getEdgeFromMap = function (edge, seriesModel) {
  107. var key = getKeyOfEdges(edge.node1, edge.node2, seriesModel);
  108. return seriesModel.__edgeMap[key];
  109. };
  110. /**
  111. * calculate all cases total length
  112. * @param edge
  113. * @param seriesModel
  114. * @returns {number}
  115. */
  116. var getTotalLengthBetweenNodes = function (edge, seriesModel) {
  117. var len = getEdgeMapLengthWithKey(getKeyOfEdges(edge.node1, edge.node2, seriesModel), seriesModel);
  118. var lenV = getEdgeMapLengthWithKey(getKeyOfEdges(edge.node2, edge.node1, seriesModel), seriesModel);
  119. return len + lenV;
  120. };
  121. /**
  122. *
  123. * @param key
  124. */
  125. var getEdgeMapLengthWithKey = function (key, seriesModel) {
  126. var edgeMap = seriesModel.__edgeMap;
  127. return edgeMap[key] ? edgeMap[key].length : 0;
  128. };
  129. /**
  130. * Count the number of edges between the same two points, used to obtain the curvature table and the parity of the edge
  131. * @see /graph/GraphSeries.js@getInitialData
  132. * @param {module:echarts/model/SeriesModel} seriesModel
  133. */
  134. export function initCurvenessList(seriesModel) {
  135. if (!getAutoCurvenessParams(seriesModel)) {
  136. return;
  137. }
  138. seriesModel.__curvenessList = [];
  139. seriesModel.__edgeMap = {};
  140. // calc the array of curveness List
  141. createCurveness(seriesModel);
  142. }
  143. /**
  144. * set edgeMap with key
  145. * @param {number|string|module:echarts/data/Graph.Node} n1
  146. * @param {number|string|module:echarts/data/Graph.Node} n2
  147. * @param {module:echarts/model/SeriesModel} seriesModel
  148. * @param {number} index
  149. */
  150. export function createEdgeMapForCurveness(n1, n2, seriesModel, index) {
  151. if (!getAutoCurvenessParams(seriesModel)) {
  152. return;
  153. }
  154. var key = getKeyOfEdges(n1, n2, seriesModel);
  155. var edgeMap = seriesModel.__edgeMap;
  156. var oppositeEdges = edgeMap[getOppositeKey(key)];
  157. // set direction
  158. if (edgeMap[key] && !oppositeEdges) {
  159. edgeMap[key].isForward = true;
  160. } else if (oppositeEdges && edgeMap[key]) {
  161. oppositeEdges.isForward = true;
  162. edgeMap[key].isForward = false;
  163. }
  164. edgeMap[key] = edgeMap[key] || [];
  165. edgeMap[key].push(index);
  166. }
  167. /**
  168. * get curvature for edge
  169. * @param edge
  170. * @param {module:echarts/model/SeriesModel} seriesModel
  171. * @param index
  172. */
  173. export function getCurvenessForEdge(edge, seriesModel, index, needReverse) {
  174. var autoCurvenessParams = getAutoCurvenessParams(seriesModel);
  175. var isArrayParam = zrUtil.isArray(autoCurvenessParams);
  176. if (!autoCurvenessParams) {
  177. return null;
  178. }
  179. var edgeArray = getEdgeFromMap(edge, seriesModel);
  180. if (!edgeArray) {
  181. return null;
  182. }
  183. var edgeIndex = -1;
  184. for (var i = 0; i < edgeArray.length; i++) {
  185. if (edgeArray[i] === index) {
  186. edgeIndex = i;
  187. break;
  188. }
  189. }
  190. // if totalLen is Longer createCurveness
  191. var totalLen = getTotalLengthBetweenNodes(edge, seriesModel);
  192. createCurveness(seriesModel, totalLen);
  193. edge.lineStyle = edge.lineStyle || {};
  194. // if is opposite edge, must set curvenss to opposite number
  195. var curKey = getKeyOfEdges(edge.node1, edge.node2, seriesModel);
  196. var curvenessList = seriesModel.__curvenessList;
  197. // if pass array no need parity
  198. var parityCorrection = isArrayParam ? 0 : totalLen % 2 ? 0 : 1;
  199. if (!edgeArray.isForward) {
  200. // the opposite edge show outside
  201. var oppositeKey = getOppositeKey(curKey);
  202. var len = getEdgeMapLengthWithKey(oppositeKey, seriesModel);
  203. var resValue = curvenessList[edgeIndex + len + parityCorrection];
  204. // isNeedReverse, simple, force type need reverse the curveness in the junction of the forword and the opposite
  205. if (needReverse) {
  206. // set as array may make the parity handle with the len of opposite
  207. if (isArrayParam) {
  208. if (autoCurvenessParams && autoCurvenessParams[0] === 0) {
  209. return (len + parityCorrection) % 2 ? resValue : -resValue;
  210. } else {
  211. return ((len % 2 ? 0 : 1) + parityCorrection) % 2 ? resValue : -resValue;
  212. }
  213. } else {
  214. return (len + parityCorrection) % 2 ? resValue : -resValue;
  215. }
  216. } else {
  217. return curvenessList[edgeIndex + len + parityCorrection];
  218. }
  219. } else {
  220. return curvenessList[parityCorrection + edgeIndex];
  221. }
  222. }