multipleGraphEdgeHelper.js 7.7 KB

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