Graph.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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'; // id may be function name of Object, add a prefix to avoid this problem.
  41. function generateNodeKey(id) {
  42. return '_EC_' + id;
  43. }
  44. var Graph =
  45. /** @class */
  46. function () {
  47. function Graph(directed) {
  48. this.type = 'graph';
  49. this.nodes = [];
  50. this.edges = [];
  51. this._nodesMap = {};
  52. /**
  53. * @type {Object.<string, module:echarts/data/Graph.Edge>}
  54. * @private
  55. */
  56. this._edgesMap = {};
  57. this._directed = directed || false;
  58. }
  59. /**
  60. * If is directed graph
  61. */
  62. Graph.prototype.isDirected = function () {
  63. return this._directed;
  64. };
  65. ;
  66. /**
  67. * Add a new node
  68. */
  69. Graph.prototype.addNode = function (id, dataIndex) {
  70. id = id == null ? '' + dataIndex : '' + id;
  71. var nodesMap = this._nodesMap;
  72. if (nodesMap[generateNodeKey(id)]) {
  73. if (process.env.NODE_ENV !== 'production') {
  74. console.error('Graph nodes have duplicate name or id');
  75. }
  76. return;
  77. }
  78. var node = new GraphNode(id, dataIndex);
  79. node.hostGraph = this;
  80. this.nodes.push(node);
  81. nodesMap[generateNodeKey(id)] = node;
  82. return node;
  83. };
  84. ;
  85. /**
  86. * Get node by data index
  87. */
  88. Graph.prototype.getNodeByIndex = function (dataIndex) {
  89. var rawIdx = this.data.getRawIndex(dataIndex);
  90. return this.nodes[rawIdx];
  91. };
  92. ;
  93. /**
  94. * Get node by id
  95. */
  96. Graph.prototype.getNodeById = function (id) {
  97. return this._nodesMap[generateNodeKey(id)];
  98. };
  99. ;
  100. /**
  101. * Add a new edge
  102. */
  103. Graph.prototype.addEdge = function (n1, n2, dataIndex) {
  104. var nodesMap = this._nodesMap;
  105. var edgesMap = this._edgesMap; // 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. ; // TODO
  226. // depthFirstTraverse(
  227. // cb, startNode, direction, context
  228. // ) {
  229. // };
  230. // Filter update
  231. Graph.prototype.update = function () {
  232. var data = this.data;
  233. var edgeData = this.edgeData;
  234. var nodes = this.nodes;
  235. var edges = this.edges;
  236. for (var i = 0, len = nodes.length; i < len; i++) {
  237. nodes[i].dataIndex = -1;
  238. }
  239. for (var i = 0, len = data.count(); i < len; i++) {
  240. nodes[data.getRawIndex(i)].dataIndex = i;
  241. }
  242. edgeData.filterSelf(function (idx) {
  243. var edge = edges[edgeData.getRawIndex(idx)];
  244. return edge.node1.dataIndex >= 0 && edge.node2.dataIndex >= 0;
  245. }); // Update edge
  246. for (var i = 0, len = edges.length; i < len; i++) {
  247. edges[i].dataIndex = -1;
  248. }
  249. for (var i = 0, len = edgeData.count(); i < len; i++) {
  250. edges[edgeData.getRawIndex(i)].dataIndex = i;
  251. }
  252. };
  253. ;
  254. /**
  255. * @return {module:echarts/data/Graph}
  256. */
  257. Graph.prototype.clone = function () {
  258. var graph = new Graph(this._directed);
  259. var nodes = this.nodes;
  260. var edges = this.edges;
  261. for (var i = 0; i < nodes.length; i++) {
  262. graph.addNode(nodes[i].id, nodes[i].dataIndex);
  263. }
  264. for (var i = 0; i < edges.length; i++) {
  265. var e = edges[i];
  266. graph.addEdge(e.node1.id, e.node2.id, e.dataIndex);
  267. }
  268. return graph;
  269. };
  270. ;
  271. return Graph;
  272. }();
  273. var GraphNode =
  274. /** @class */
  275. 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 =
  365. /** @class */
  366. function () {
  367. function GraphEdge(n1, n2, dataIndex) {
  368. this.dataIndex = -1;
  369. this.node1 = n1;
  370. this.node2 = n2;
  371. this.dataIndex = dataIndex == null ? -1 : dataIndex;
  372. } // eslint-disable-next-line @typescript-eslint/no-unused-vars
  373. GraphEdge.prototype.getModel = function (path) {
  374. if (this.dataIndex < 0) {
  375. return;
  376. }
  377. var graph = this.hostGraph;
  378. var itemModel = graph.edgeData.getItemModel(this.dataIndex);
  379. return itemModel.getModel(path);
  380. };
  381. GraphEdge.prototype.getAdjacentDataIndices = function () {
  382. return {
  383. edge: [this.dataIndex],
  384. node: [this.node1.dataIndex, this.node2.dataIndex]
  385. };
  386. };
  387. GraphEdge.prototype.getTrajectoryDataIndices = function () {
  388. var connectedEdgesMap = zrUtil.createHashMap();
  389. var connectedNodesMap = zrUtil.createHashMap();
  390. connectedEdgesMap.set(this.dataIndex, true);
  391. var sourceNodes = [this.node1];
  392. var targetNodes = [this.node2];
  393. var nodeIteratorIndex = 0;
  394. while (nodeIteratorIndex < sourceNodes.length) {
  395. var sourceNode = sourceNodes[nodeIteratorIndex];
  396. nodeIteratorIndex++;
  397. connectedNodesMap.set(sourceNode.dataIndex, true);
  398. for (var j = 0; j < sourceNode.inEdges.length; j++) {
  399. connectedEdgesMap.set(sourceNode.inEdges[j].dataIndex, true);
  400. sourceNodes.push(sourceNode.inEdges[j].node1);
  401. }
  402. }
  403. nodeIteratorIndex = 0;
  404. while (nodeIteratorIndex < targetNodes.length) {
  405. var targetNode = targetNodes[nodeIteratorIndex];
  406. nodeIteratorIndex++;
  407. connectedNodesMap.set(targetNode.dataIndex, true);
  408. for (var j = 0; j < targetNode.outEdges.length; j++) {
  409. connectedEdgesMap.set(targetNode.outEdges[j].dataIndex, true);
  410. targetNodes.push(targetNode.outEdges[j].node2);
  411. }
  412. }
  413. return {
  414. edge: connectedEdgesMap.keys(),
  415. node: connectedNodesMap.keys()
  416. };
  417. };
  418. return GraphEdge;
  419. }();
  420. function createGraphDataProxyMixin(hostName, dataName) {
  421. return {
  422. /**
  423. * @param Default 'value'. can be 'a', 'b', 'c', 'd', 'e'.
  424. */
  425. getValue: function (dimension) {
  426. var data = this[hostName][dataName];
  427. return data.getStore().get(data.getDimensionIndex(dimension || 'value'), this.dataIndex);
  428. },
  429. // TODO: TYPE stricter type.
  430. setVisual: function (key, value) {
  431. this.dataIndex >= 0 && this[hostName][dataName].setItemVisual(this.dataIndex, key, value);
  432. },
  433. getVisual: function (key) {
  434. return this[hostName][dataName].getItemVisual(this.dataIndex, key);
  435. },
  436. setLayout: function (layout, merge) {
  437. this.dataIndex >= 0 && this[hostName][dataName].setItemLayout(this.dataIndex, layout, merge);
  438. },
  439. getLayout: function () {
  440. return this[hostName][dataName].getItemLayout(this.dataIndex);
  441. },
  442. getGraphicEl: function () {
  443. return this[hostName][dataName].getItemGraphicEl(this.dataIndex);
  444. },
  445. getRawIndex: function () {
  446. return this[hostName][dataName].getRawIndex(this.dataIndex);
  447. }
  448. };
  449. }
  450. ;
  451. ;
  452. ;
  453. zrUtil.mixin(GraphNode, createGraphDataProxyMixin('hostGraph', 'data'));
  454. zrUtil.mixin(GraphEdge, createGraphDataProxyMixin('hostGraph', 'edgeData'));
  455. export default Graph;
  456. export { GraphNode, GraphEdge };