multipleGraphEdgeHelper.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. var zrUtil = require("zrender/lib/core/util");
  20. /*
  21. * Licensed to the Apache Software Foundation (ASF) under one
  22. * or more contributor license agreements. See the NOTICE file
  23. * distributed with this work for additional information
  24. * regarding copyright ownership. The ASF licenses this file
  25. * to you under the Apache License, Version 2.0 (the
  26. * "License"); you may not use this file except in compliance
  27. * with the License. You may obtain a copy of the License at
  28. *
  29. * http://www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing,
  32. * software distributed under the License is distributed on an
  33. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  34. * KIND, either express or implied. See the License for the
  35. * specific language governing permissions and limitations
  36. * under the License.
  37. */
  38. var KEY_DELIMITER = '-->';
  39. /**
  40. * params handler
  41. * @param {module:echarts/model/SeriesModel} seriesModel
  42. * @returns {*}
  43. */
  44. var getAutoCurvenessParams = function (seriesModel) {
  45. return seriesModel.get('autoCurveness') || null;
  46. };
  47. /**
  48. * Generate a list of edge curvatures, 20 is the default
  49. * @param {module:echarts/model/SeriesModel} seriesModel
  50. * @param {number} appendLength
  51. * @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]
  52. */
  53. var createCurveness = function (seriesModel, appendLength) {
  54. var autoCurvenessParmas = getAutoCurvenessParams(seriesModel);
  55. var length = 20;
  56. var curvenessList = []; // handler the function set
  57. if (typeof autoCurvenessParmas === 'number') {
  58. length = autoCurvenessParmas;
  59. } else if (zrUtil.isArray(autoCurvenessParmas)) {
  60. seriesModel.__curvenessList = autoCurvenessParmas;
  61. return;
  62. } // append length
  63. if (appendLength > length) {
  64. length = appendLength;
  65. } // make sure the length is even
  66. var len = length % 2 ? length + 2 : length + 3;
  67. curvenessList = [];
  68. for (var i = 0; i < len; i++) {
  69. curvenessList.push((i % 2 ? i + 1 : i) / 10 * (i % 2 ? -1 : 1));
  70. }
  71. seriesModel.__curvenessList = curvenessList;
  72. };
  73. /**
  74. * Create different cache key data in the positive and negative directions, in order to set the curvature later
  75. * @param {number|string|module:echarts/data/Graph.Node} n1
  76. * @param {number|string|module:echarts/data/Graph.Node} n2
  77. * @param {module:echarts/model/SeriesModel} seriesModel
  78. * @returns {string} key
  79. */
  80. var getKeyOfEdges = function (n1, n2, seriesModel) {
  81. var source = [n1.id, n1.dataIndex].join('.');
  82. var target = [n2.id, n2.dataIndex].join('.');
  83. return [seriesModel.uid, source, target].join(KEY_DELIMITER);
  84. };
  85. /**
  86. * get opposite key
  87. * @param {string} key
  88. * @returns {string}
  89. */
  90. var getOppositeKey = function (key) {
  91. var keys = key.split(KEY_DELIMITER);
  92. return [keys[0], keys[2], keys[1]].join(KEY_DELIMITER);
  93. };
  94. /**
  95. * get edgeMap with key
  96. * @param edge
  97. * @param {module:echarts/model/SeriesModel} seriesModel
  98. */
  99. var getEdgeFromMap = function (edge, seriesModel) {
  100. var key = getKeyOfEdges(edge.node1, edge.node2, seriesModel);
  101. return seriesModel.__edgeMap[key];
  102. };
  103. /**
  104. * calculate all cases total length
  105. * @param edge
  106. * @param seriesModel
  107. * @returns {number}
  108. */
  109. var getTotalLengthBetweenNodes = function (edge, seriesModel) {
  110. var len = getEdgeMapLengthWithKey(getKeyOfEdges(edge.node1, edge.node2, seriesModel), seriesModel);
  111. var lenV = getEdgeMapLengthWithKey(getKeyOfEdges(edge.node2, edge.node1, seriesModel), seriesModel);
  112. return len + lenV;
  113. };
  114. /**
  115. *
  116. * @param key
  117. */
  118. var getEdgeMapLengthWithKey = function (key, seriesModel) {
  119. var edgeMap = seriesModel.__edgeMap;
  120. return edgeMap[key] ? edgeMap[key].length : 0;
  121. };
  122. /**
  123. * Count the number of edges between the same two points, used to obtain the curvature table and the parity of the edge
  124. * @see /graph/GraphSeries.js@getInitialData
  125. * @param {module:echarts/model/SeriesModel} seriesModel
  126. */
  127. function initCurvenessList(seriesModel) {
  128. if (!getAutoCurvenessParams(seriesModel)) {
  129. return;
  130. }
  131. seriesModel.__curvenessList = [];
  132. seriesModel.__edgeMap = {}; // calc the array of curveness List
  133. createCurveness(seriesModel);
  134. }
  135. /**
  136. * set edgeMap with key
  137. * @param {number|string|module:echarts/data/Graph.Node} n1
  138. * @param {number|string|module:echarts/data/Graph.Node} n2
  139. * @param {module:echarts/model/SeriesModel} seriesModel
  140. * @param {number} index
  141. */
  142. function createEdgeMapForCurveness(n1, n2, seriesModel, index) {
  143. if (!getAutoCurvenessParams(seriesModel)) {
  144. return;
  145. }
  146. var key = getKeyOfEdges(n1, n2, seriesModel);
  147. var edgeMap = seriesModel.__edgeMap;
  148. var oppositeEdges = edgeMap[getOppositeKey(key)]; // set direction
  149. if (edgeMap[key] && !oppositeEdges) {
  150. edgeMap[key].isForward = true;
  151. } else if (oppositeEdges && edgeMap[key]) {
  152. oppositeEdges.isForward = true;
  153. edgeMap[key].isForward = false;
  154. }
  155. edgeMap[key] = edgeMap[key] || [];
  156. edgeMap[key].push(index);
  157. }
  158. /**
  159. * get curvature for edge
  160. * @param edge
  161. * @param {module:echarts/model/SeriesModel} seriesModel
  162. * @param index
  163. */
  164. function getCurvenessForEdge(edge, seriesModel, index, needReverse) {
  165. var autoCurvenessParams = getAutoCurvenessParams(seriesModel);
  166. var isArrayParam = zrUtil.isArray(autoCurvenessParams);
  167. if (!autoCurvenessParams) {
  168. return null;
  169. }
  170. var edgeArray = getEdgeFromMap(edge, seriesModel);
  171. if (!edgeArray) {
  172. return null;
  173. }
  174. var edgeIndex = -1;
  175. for (var i = 0; i < edgeArray.length; i++) {
  176. if (edgeArray[i] === index) {
  177. edgeIndex = i;
  178. break;
  179. }
  180. } // if totalLen is Longer createCurveness
  181. var totalLen = getTotalLengthBetweenNodes(edge, seriesModel);
  182. createCurveness(seriesModel, totalLen);
  183. edge.lineStyle = edge.lineStyle || {}; // if is opposite edge, must set curvenss to opposite number
  184. var curKey = getKeyOfEdges(edge.node1, edge.node2, seriesModel);
  185. var curvenessList = seriesModel.__curvenessList; // if pass array no need parity
  186. var parityCorrection = isArrayParam ? 0 : totalLen % 2 ? 0 : 1;
  187. if (!edgeArray.isForward) {
  188. // the opposite edge show outside
  189. var oppositeKey = getOppositeKey(curKey);
  190. var len = getEdgeMapLengthWithKey(oppositeKey, seriesModel);
  191. var resValue = curvenessList[edgeIndex + len + parityCorrection]; // isNeedReverse, simple, force type need reverse the curveness in the junction of the forword and the opposite
  192. if (needReverse) {
  193. // set as array may make the parity handle with the len of opposite
  194. if (isArrayParam) {
  195. if (autoCurvenessParams && autoCurvenessParams[0] === 0) {
  196. return (len + parityCorrection) % 2 ? resValue : -resValue;
  197. } else {
  198. return ((len % 2 ? 0 : 1) + parityCorrection) % 2 ? resValue : -resValue;
  199. }
  200. } else {
  201. return (len + parityCorrection) % 2 ? resValue : -resValue;
  202. }
  203. } else {
  204. return curvenessList[edgeIndex + len + parityCorrection];
  205. }
  206. } else {
  207. return curvenessList[parityCorrection + edgeIndex];
  208. }
  209. }
  210. exports.initCurvenessList = initCurvenessList;
  211. exports.createEdgeMapForCurveness = createEdgeMapForCurveness;
  212. exports.getCurvenessForEdge = getCurvenessForEdge;