Graph.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. import * as zrUtil from 'zrender/lib/core/util.js';
  41. // id may be function name of Object, add a prefix to avoid this problem.
  42. function generateNodeKey(id) {
  43. return '_EC_' + id;
  44. }
  45. var Graph = /** @class */function () {
  46. function Graph(directed) {
  47. this.type = 'graph';
  48. this.nodes = [];
  49. this.edges = [];
  50. this._nodesMap = {};
  51. /**
  52. * @type {Object.<string, module:echarts/data/Graph.Edge>}
  53. * @private
  54. */
  55. this._edgesMap = {};
  56. this._directed = directed || false;
  57. }
  58. /**
  59. * If is directed graph
  60. */
  61. Graph.prototype.isDirected = function () {
  62. return this._directed;
  63. };
  64. ;
  65. /**
  66. * Add a new node
  67. */
  68. Graph.prototype.addNode = function (id, dataIndex) {
  69. id = id == null ? '' + dataIndex : '' + id;
  70. var nodesMap = this._nodesMap;
  71. if (nodesMap[generateNodeKey(id)]) {
  72. if (process.env.NODE_ENV !== 'production') {
  73. console.error('Graph nodes have duplicate name or id');
  74. }
  75. return;
  76. }
  77. var node = new GraphNode(id, dataIndex);
  78. node.hostGraph = this;
  79. this.nodes.push(node);
  80. nodesMap[generateNodeKey(id)] = node;
  81. return node;
  82. };
  83. ;
  84. /**
  85. * Get node by data index
  86. */
  87. Graph.prototype.getNodeByIndex = function (dataIndex) {
  88. var rawIdx = this.data.getRawIndex(dataIndex);
  89. return this.nodes[rawIdx];
  90. };
  91. ;
  92. /**
  93. * Get node by id
  94. */
  95. Graph.prototype.getNodeById = function (id) {
  96. return this._nodesMap[generateNodeKey(id)];
  97. };
  98. ;
  99. /**
  100. * Add a new edge
  101. */
  102. Graph.prototype.addEdge = function (n1, n2, dataIndex) {
  103. var nodesMap = this._nodesMap;
  104. var edgesMap = this._edgesMap;
  105. // PENDING
  106. if (zrUtil.isNumber(n1)) {
  107. n1 = this.nodes[n1];
  108. }
  109. if (zrUtil.isNumber(n2)) {
  110. n2 = this.nodes[n2];
  111. }
  112. if (!(n1 instanceof GraphNode)) {
  113. n1 = nodesMap[generateNodeKey(n1)];
  114. }
  115. if (!(n2 instanceof GraphNode)) {
  116. n2 = nodesMap[generateNodeKey(n2)];
  117. }
  118. if (!n1 || !n2) {
  119. return;
  120. }
  121. var key = n1.id + '-' + n2.id;
  122. var edge = new GraphEdge(n1, n2, dataIndex);
  123. edge.hostGraph = this;
  124. if (this._directed) {
  125. n1.outEdges.push(edge);
  126. n2.inEdges.push(edge);
  127. }
  128. n1.edges.push(edge);
  129. if (n1 !== n2) {
  130. n2.edges.push(edge);
  131. }
  132. this.edges.push(edge);
  133. edgesMap[key] = edge;
  134. return edge;
  135. };
  136. ;
  137. /**
  138. * Get edge by data index
  139. */
  140. Graph.prototype.getEdgeByIndex = function (dataIndex) {
  141. var rawIdx = this.edgeData.getRawIndex(dataIndex);
  142. return this.edges[rawIdx];
  143. };
  144. ;
  145. /**
  146. * Get edge by two linked nodes
  147. */
  148. Graph.prototype.getEdge = function (n1, n2) {
  149. if (n1 instanceof GraphNode) {
  150. n1 = n1.id;
  151. }
  152. if (n2 instanceof GraphNode) {
  153. n2 = n2.id;
  154. }
  155. var edgesMap = this._edgesMap;
  156. if (this._directed) {
  157. return edgesMap[n1 + '-' + n2];
  158. } else {
  159. return edgesMap[n1 + '-' + n2] || edgesMap[n2 + '-' + n1];
  160. }
  161. };
  162. ;
  163. /**
  164. * Iterate all nodes
  165. */
  166. Graph.prototype.eachNode = function (cb, context) {
  167. var nodes = this.nodes;
  168. var len = nodes.length;
  169. for (var i = 0; i < len; i++) {
  170. if (nodes[i].dataIndex >= 0) {
  171. cb.call(context, nodes[i], i);
  172. }
  173. }
  174. };
  175. ;
  176. /**
  177. * Iterate all edges
  178. */
  179. Graph.prototype.eachEdge = function (cb, context) {
  180. var edges = this.edges;
  181. var len = edges.length;
  182. for (var i = 0; i < len; i++) {
  183. if (edges[i].dataIndex >= 0 && edges[i].node1.dataIndex >= 0 && edges[i].node2.dataIndex >= 0) {
  184. cb.call(context, edges[i], i);
  185. }
  186. }
  187. };
  188. ;
  189. /**
  190. * Breadth first traverse
  191. * Return true to stop traversing
  192. */
  193. Graph.prototype.breadthFirstTraverse = function (cb, startNode, direction, context) {
  194. if (!(startNode instanceof GraphNode)) {
  195. startNode = this._nodesMap[generateNodeKey(startNode)];
  196. }
  197. if (!startNode) {
  198. return;
  199. }
  200. var edgeType = direction === 'out' ? 'outEdges' : direction === 'in' ? 'inEdges' : 'edges';
  201. for (var i = 0; i < this.nodes.length; i++) {
  202. this.nodes[i].__visited = false;
  203. }
  204. if (cb.call(context, startNode, null)) {
  205. return;
  206. }
  207. var queue = [startNode];
  208. while (queue.length) {
  209. var currentNode = queue.shift();
  210. var edges = currentNode[edgeType];
  211. for (var i = 0; i < edges.length; i++) {
  212. var e = edges[i];
  213. var otherNode = e.node1 === currentNode ? e.node2 : e.node1;
  214. if (!otherNode.__visited) {
  215. if (cb.call(context, otherNode, currentNode)) {
  216. // Stop traversing
  217. return;
  218. }
  219. queue.push(otherNode);
  220. otherNode.__visited = true;
  221. }
  222. }
  223. }
  224. };
  225. ;
  226. // TODO
  227. // depthFirstTraverse(
  228. // cb, startNode, direction, context
  229. // ) {
  230. // };
  231. // Filter update
  232. Graph.prototype.update = function () {
  233. var data = this.data;
  234. var edgeData = this.edgeData;
  235. var nodes = this.nodes;
  236. var edges = this.edges;
  237. for (var i = 0, len = nodes.length; i < len; i++) {
  238. nodes[i].dataIndex = -1;
  239. }
  240. for (var i = 0, len = data.count(); i < len; i++) {
  241. nodes[data.getRawIndex(i)].dataIndex = i;
  242. }
  243. edgeData.filterSelf(function (idx) {
  244. var edge = edges[edgeData.getRawIndex(idx)];
  245. return edge.node1.dataIndex >= 0 && edge.node2.dataIndex >= 0;
  246. });
  247. // Update edge
  248. for (var i = 0, len = edges.length; i < len; i++) {
  249. edges[i].dataIndex = -1;
  250. }
  251. for (var i = 0, len = edgeData.count(); i < len; i++) {
  252. edges[edgeData.getRawIndex(i)].dataIndex = i;
  253. }
  254. };
  255. ;
  256. /**
  257. * @return {module:echarts/data/Graph}
  258. */
  259. Graph.prototype.clone = function () {
  260. var graph = new Graph(this._directed);
  261. var nodes = this.nodes;
  262. var edges = this.edges;
  263. for (var i = 0; i < nodes.length; i++) {
  264. graph.addNode(nodes[i].id, nodes[i].dataIndex);
  265. }
  266. for (var i = 0; i < edges.length; i++) {
  267. var e = edges[i];
  268. graph.addEdge(e.node1.id, e.node2.id, e.dataIndex);
  269. }
  270. return graph;
  271. };
  272. ;
  273. return Graph;
  274. }();
  275. var GraphNode = /** @class */function () {
  276. function GraphNode(id, dataIndex) {
  277. this.inEdges = [];
  278. this.outEdges = [];
  279. this.edges = [];
  280. this.dataIndex = -1;
  281. this.id = id == null ? '' : id;
  282. this.dataIndex = dataIndex == null ? -1 : dataIndex;
  283. }
  284. /**
  285. * @return {number}
  286. */
  287. GraphNode.prototype.degree = function () {
  288. return this.edges.length;
  289. };
  290. /**
  291. * @return {number}
  292. */
  293. GraphNode.prototype.inDegree = function () {
  294. return this.inEdges.length;
  295. };
  296. /**
  297. * @return {number}
  298. */
  299. GraphNode.prototype.outDegree = function () {
  300. return this.outEdges.length;
  301. };
  302. GraphNode.prototype.getModel = function (path) {
  303. if (this.dataIndex < 0) {
  304. return;
  305. }
  306. var graph = this.hostGraph;
  307. var itemModel = graph.data.getItemModel(this.dataIndex);
  308. return itemModel.getModel(path);
  309. };
  310. GraphNode.prototype.getAdjacentDataIndices = function () {
  311. var dataIndices = {
  312. edge: [],
  313. node: []
  314. };
  315. for (var i = 0; i < this.edges.length; i++) {
  316. var adjacentEdge = this.edges[i];
  317. if (adjacentEdge.dataIndex < 0) {
  318. continue;
  319. }
  320. dataIndices.edge.push(adjacentEdge.dataIndex);
  321. dataIndices.node.push(adjacentEdge.node1.dataIndex, adjacentEdge.node2.dataIndex);
  322. }
  323. return dataIndices;
  324. };
  325. GraphNode.prototype.getTrajectoryDataIndices = function () {
  326. var connectedEdgesMap = zrUtil.createHashMap();
  327. var connectedNodesMap = zrUtil.createHashMap();
  328. for (var i = 0; i < this.edges.length; i++) {
  329. var adjacentEdge = this.edges[i];
  330. if (adjacentEdge.dataIndex < 0) {
  331. continue;
  332. }
  333. connectedEdgesMap.set(adjacentEdge.dataIndex, true);
  334. var sourceNodesQueue = [adjacentEdge.node1];
  335. var targetNodesQueue = [adjacentEdge.node2];
  336. var nodeIteratorIndex = 0;
  337. while (nodeIteratorIndex < sourceNodesQueue.length) {
  338. var sourceNode = sourceNodesQueue[nodeIteratorIndex];
  339. nodeIteratorIndex++;
  340. connectedNodesMap.set(sourceNode.dataIndex, true);
  341. for (var j = 0; j < sourceNode.inEdges.length; j++) {
  342. connectedEdgesMap.set(sourceNode.inEdges[j].dataIndex, true);
  343. sourceNodesQueue.push(sourceNode.inEdges[j].node1);
  344. }
  345. }
  346. nodeIteratorIndex = 0;
  347. while (nodeIteratorIndex < targetNodesQueue.length) {
  348. var targetNode = targetNodesQueue[nodeIteratorIndex];
  349. nodeIteratorIndex++;
  350. connectedNodesMap.set(targetNode.dataIndex, true);
  351. for (var j = 0; j < targetNode.outEdges.length; j++) {
  352. connectedEdgesMap.set(targetNode.outEdges[j].dataIndex, true);
  353. targetNodesQueue.push(targetNode.outEdges[j].node2);
  354. }
  355. }
  356. }
  357. return {
  358. edge: connectedEdgesMap.keys(),
  359. node: connectedNodesMap.keys()
  360. };
  361. };
  362. return GraphNode;
  363. }();
  364. var GraphEdge = /** @class */function () {
  365. function GraphEdge(n1, n2, dataIndex) {
  366. this.dataIndex = -1;
  367. this.node1 = n1;
  368. this.node2 = n2;
  369. this.dataIndex = dataIndex == null ? -1 : dataIndex;
  370. }
  371. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  372. GraphEdge.prototype.getModel = function (path) {
  373. if (this.dataIndex < 0) {
  374. return;
  375. }
  376. var graph = this.hostGraph;
  377. var itemModel = graph.edgeData.getItemModel(this.dataIndex);
  378. return itemModel.getModel(path);
  379. };
  380. GraphEdge.prototype.getAdjacentDataIndices = function () {
  381. return {
  382. edge: [this.dataIndex],
  383. node: [this.node1.dataIndex, this.node2.dataIndex]
  384. };
  385. };
  386. GraphEdge.prototype.getTrajectoryDataIndices = function () {
  387. var connectedEdgesMap = zrUtil.createHashMap();
  388. var connectedNodesMap = zrUtil.createHashMap();
  389. connectedEdgesMap.set(this.dataIndex, true);
  390. var sourceNodes = [this.node1];
  391. var targetNodes = [this.node2];
  392. var nodeIteratorIndex = 0;
  393. while (nodeIteratorIndex < sourceNodes.length) {
  394. var sourceNode = sourceNodes[nodeIteratorIndex];
  395. nodeIteratorIndex++;
  396. connectedNodesMap.set(sourceNode.dataIndex, true);
  397. for (var j = 0; j < sourceNode.inEdges.length; j++) {
  398. connectedEdgesMap.set(sourceNode.inEdges[j].dataIndex, true);
  399. sourceNodes.push(sourceNode.inEdges[j].node1);
  400. }
  401. }
  402. nodeIteratorIndex = 0;
  403. while (nodeIteratorIndex < targetNodes.length) {
  404. var targetNode = targetNodes[nodeIteratorIndex];
  405. nodeIteratorIndex++;
  406. connectedNodesMap.set(targetNode.dataIndex, true);
  407. for (var j = 0; j < targetNode.outEdges.length; j++) {
  408. connectedEdgesMap.set(targetNode.outEdges[j].dataIndex, true);
  409. targetNodes.push(targetNode.outEdges[j].node2);
  410. }
  411. }
  412. return {
  413. edge: connectedEdgesMap.keys(),
  414. node: connectedNodesMap.keys()
  415. };
  416. };
  417. return GraphEdge;
  418. }();
  419. function createGraphDataProxyMixin(hostName, dataName) {
  420. return {
  421. /**
  422. * @param Default 'value'. can be 'a', 'b', 'c', 'd', 'e'.
  423. */
  424. getValue: function (dimension) {
  425. var data = this[hostName][dataName];
  426. return data.getStore().get(data.getDimensionIndex(dimension || 'value'), this.dataIndex);
  427. },
  428. // TODO: TYPE stricter type.
  429. setVisual: function (key, value) {
  430. this.dataIndex >= 0 && this[hostName][dataName].setItemVisual(this.dataIndex, key, value);
  431. },
  432. getVisual: function (key) {
  433. return this[hostName][dataName].getItemVisual(this.dataIndex, key);
  434. },
  435. setLayout: function (layout, merge) {
  436. this.dataIndex >= 0 && this[hostName][dataName].setItemLayout(this.dataIndex, layout, merge);
  437. },
  438. getLayout: function () {
  439. return this[hostName][dataName].getItemLayout(this.dataIndex);
  440. },
  441. getGraphicEl: function () {
  442. return this[hostName][dataName].getItemGraphicEl(this.dataIndex);
  443. },
  444. getRawIndex: function () {
  445. return this[hostName][dataName].getRawIndex(this.dataIndex);
  446. }
  447. };
  448. }
  449. ;
  450. ;
  451. ;
  452. zrUtil.mixin(GraphNode, createGraphDataProxyMixin('hostGraph', 'data'));
  453. zrUtil.mixin(GraphEdge, createGraphDataProxyMixin('hostGraph', 'edgeData'));
  454. export default Graph;
  455. export { GraphNode, GraphEdge };