Graph.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 _config = require("../config");
  20. var __DEV__ = _config.__DEV__;
  21. var zrUtil = require("zrender/lib/core/util");
  22. var _clazz = require("../util/clazz");
  23. var enableClassCheck = _clazz.enableClassCheck;
  24. /*
  25. * Licensed to the Apache Software Foundation (ASF) under one
  26. * or more contributor license agreements. See the NOTICE file
  27. * distributed with this work for additional information
  28. * regarding copyright ownership. The ASF licenses this file
  29. * to you under the Apache License, Version 2.0 (the
  30. * "License"); you may not use this file except in compliance
  31. * with the License. You may obtain a copy of the License at
  32. *
  33. * http://www.apache.org/licenses/LICENSE-2.0
  34. *
  35. * Unless required by applicable law or agreed to in writing,
  36. * software distributed under the License is distributed on an
  37. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  38. * KIND, either express or implied. See the License for the
  39. * specific language governing permissions and limitations
  40. * under the License.
  41. */
  42. // id may be function name of Object, add a prefix to avoid this problem.
  43. function generateNodeKey(id) {
  44. return '_EC_' + id;
  45. }
  46. /**
  47. * @alias module:echarts/data/Graph
  48. * @constructor
  49. * @param {boolean} directed
  50. */
  51. var Graph = function (directed) {
  52. /**
  53. * 是否是有向图
  54. * @type {boolean}
  55. * @private
  56. */
  57. this._directed = directed || false;
  58. /**
  59. * @type {Array.<module:echarts/data/Graph.Node>}
  60. * @readOnly
  61. */
  62. this.nodes = [];
  63. /**
  64. * @type {Array.<module:echarts/data/Graph.Edge>}
  65. * @readOnly
  66. */
  67. this.edges = [];
  68. /**
  69. * @type {Object.<string, module:echarts/data/Graph.Node>}
  70. * @private
  71. */
  72. this._nodesMap = {};
  73. /**
  74. * @type {Object.<string, module:echarts/data/Graph.Edge>}
  75. * @private
  76. */
  77. this._edgesMap = {};
  78. /**
  79. * @type {module:echarts/data/List}
  80. * @readOnly
  81. */
  82. this.data;
  83. /**
  84. * @type {module:echarts/data/List}
  85. * @readOnly
  86. */
  87. this.edgeData;
  88. };
  89. var graphProto = Graph.prototype;
  90. /**
  91. * @type {string}
  92. */
  93. graphProto.type = 'graph';
  94. /**
  95. * If is directed graph
  96. * @return {boolean}
  97. */
  98. graphProto.isDirected = function () {
  99. return this._directed;
  100. };
  101. /**
  102. * Add a new node
  103. * @param {string} id
  104. * @param {number} [dataIndex]
  105. */
  106. graphProto.addNode = function (id, dataIndex) {
  107. id = id == null ? '' + dataIndex : '' + id;
  108. var nodesMap = this._nodesMap;
  109. if (nodesMap[generateNodeKey(id)]) {
  110. return;
  111. }
  112. var node = new Node(id, dataIndex);
  113. node.hostGraph = this;
  114. this.nodes.push(node);
  115. nodesMap[generateNodeKey(id)] = node;
  116. return node;
  117. };
  118. /**
  119. * Get node by data index
  120. * @param {number} dataIndex
  121. * @return {module:echarts/data/Graph~Node}
  122. */
  123. graphProto.getNodeByIndex = function (dataIndex) {
  124. var rawIdx = this.data.getRawIndex(dataIndex);
  125. return this.nodes[rawIdx];
  126. };
  127. /**
  128. * Get node by id
  129. * @param {string} id
  130. * @return {module:echarts/data/Graph.Node}
  131. */
  132. graphProto.getNodeById = function (id) {
  133. return this._nodesMap[generateNodeKey(id)];
  134. };
  135. /**
  136. * Add a new edge
  137. * @param {number|string|module:echarts/data/Graph.Node} n1
  138. * @param {number|string|module:echarts/data/Graph.Node} n2
  139. * @param {number} [dataIndex=-1]
  140. * @return {module:echarts/data/Graph.Edge}
  141. */
  142. graphProto.addEdge = function (n1, n2, dataIndex) {
  143. var nodesMap = this._nodesMap;
  144. var edgesMap = this._edgesMap; // PNEDING
  145. if (typeof n1 === 'number') {
  146. n1 = this.nodes[n1];
  147. }
  148. if (typeof n2 === 'number') {
  149. n2 = this.nodes[n2];
  150. }
  151. if (!Node.isInstance(n1)) {
  152. n1 = nodesMap[generateNodeKey(n1)];
  153. }
  154. if (!Node.isInstance(n2)) {
  155. n2 = nodesMap[generateNodeKey(n2)];
  156. }
  157. if (!n1 || !n2) {
  158. return;
  159. }
  160. var key = n1.id + '-' + n2.id; // PENDING
  161. if (edgesMap[key]) {
  162. return;
  163. }
  164. var edge = new Edge(n1, n2, dataIndex);
  165. edge.hostGraph = this;
  166. if (this._directed) {
  167. n1.outEdges.push(edge);
  168. n2.inEdges.push(edge);
  169. }
  170. n1.edges.push(edge);
  171. if (n1 !== n2) {
  172. n2.edges.push(edge);
  173. }
  174. this.edges.push(edge);
  175. edgesMap[key] = edge;
  176. return edge;
  177. };
  178. /**
  179. * Get edge by data index
  180. * @param {number} dataIndex
  181. * @return {module:echarts/data/Graph~Node}
  182. */
  183. graphProto.getEdgeByIndex = function (dataIndex) {
  184. var rawIdx = this.edgeData.getRawIndex(dataIndex);
  185. return this.edges[rawIdx];
  186. };
  187. /**
  188. * Get edge by two linked nodes
  189. * @param {module:echarts/data/Graph.Node|string} n1
  190. * @param {module:echarts/data/Graph.Node|string} n2
  191. * @return {module:echarts/data/Graph.Edge}
  192. */
  193. graphProto.getEdge = function (n1, n2) {
  194. if (Node.isInstance(n1)) {
  195. n1 = n1.id;
  196. }
  197. if (Node.isInstance(n2)) {
  198. n2 = n2.id;
  199. }
  200. var edgesMap = this._edgesMap;
  201. if (this._directed) {
  202. return edgesMap[n1 + '-' + n2];
  203. } else {
  204. return edgesMap[n1 + '-' + n2] || edgesMap[n2 + '-' + n1];
  205. }
  206. };
  207. /**
  208. * Iterate all nodes
  209. * @param {Function} cb
  210. * @param {*} [context]
  211. */
  212. graphProto.eachNode = function (cb, context) {
  213. var nodes = this.nodes;
  214. var len = nodes.length;
  215. for (var i = 0; i < len; i++) {
  216. if (nodes[i].dataIndex >= 0) {
  217. cb.call(context, nodes[i], i);
  218. }
  219. }
  220. };
  221. /**
  222. * Iterate all edges
  223. * @param {Function} cb
  224. * @param {*} [context]
  225. */
  226. graphProto.eachEdge = function (cb, context) {
  227. var edges = this.edges;
  228. var len = edges.length;
  229. for (var i = 0; i < len; i++) {
  230. if (edges[i].dataIndex >= 0 && edges[i].node1.dataIndex >= 0 && edges[i].node2.dataIndex >= 0) {
  231. cb.call(context, edges[i], i);
  232. }
  233. }
  234. };
  235. /**
  236. * Breadth first traverse
  237. * @param {Function} cb
  238. * @param {module:echarts/data/Graph.Node} startNode
  239. * @param {string} [direction='none'] 'none'|'in'|'out'
  240. * @param {*} [context]
  241. */
  242. graphProto.breadthFirstTraverse = function (cb, startNode, direction, context) {
  243. if (!Node.isInstance(startNode)) {
  244. startNode = this._nodesMap[generateNodeKey(startNode)];
  245. }
  246. if (!startNode) {
  247. return;
  248. }
  249. var edgeType = direction === 'out' ? 'outEdges' : direction === 'in' ? 'inEdges' : 'edges';
  250. for (var i = 0; i < this.nodes.length; i++) {
  251. this.nodes[i].__visited = false;
  252. }
  253. if (cb.call(context, startNode, null)) {
  254. return;
  255. }
  256. var queue = [startNode];
  257. while (queue.length) {
  258. var currentNode = queue.shift();
  259. var edges = currentNode[edgeType];
  260. for (var i = 0; i < edges.length; i++) {
  261. var e = edges[i];
  262. var otherNode = e.node1 === currentNode ? e.node2 : e.node1;
  263. if (!otherNode.__visited) {
  264. if (cb.call(context, otherNode, currentNode)) {
  265. // Stop traversing
  266. return;
  267. }
  268. queue.push(otherNode);
  269. otherNode.__visited = true;
  270. }
  271. }
  272. }
  273. }; // TODO
  274. // graphProto.depthFirstTraverse = function (
  275. // cb, startNode, direction, context
  276. // ) {
  277. // };
  278. // Filter update
  279. graphProto.update = function () {
  280. var data = this.data;
  281. var edgeData = this.edgeData;
  282. var nodes = this.nodes;
  283. var edges = this.edges;
  284. for (var i = 0, len = nodes.length; i < len; i++) {
  285. nodes[i].dataIndex = -1;
  286. }
  287. for (var i = 0, len = data.count(); i < len; i++) {
  288. nodes[data.getRawIndex(i)].dataIndex = i;
  289. }
  290. edgeData.filterSelf(function (idx) {
  291. var edge = edges[edgeData.getRawIndex(idx)];
  292. return edge.node1.dataIndex >= 0 && edge.node2.dataIndex >= 0;
  293. }); // Update edge
  294. for (var i = 0, len = edges.length; i < len; i++) {
  295. edges[i].dataIndex = -1;
  296. }
  297. for (var i = 0, len = edgeData.count(); i < len; i++) {
  298. edges[edgeData.getRawIndex(i)].dataIndex = i;
  299. }
  300. };
  301. /**
  302. * @return {module:echarts/data/Graph}
  303. */
  304. graphProto.clone = function () {
  305. var graph = new Graph(this._directed);
  306. var nodes = this.nodes;
  307. var edges = this.edges;
  308. for (var i = 0; i < nodes.length; i++) {
  309. graph.addNode(nodes[i].id, nodes[i].dataIndex);
  310. }
  311. for (var i = 0; i < edges.length; i++) {
  312. var e = edges[i];
  313. graph.addEdge(e.node1.id, e.node2.id, e.dataIndex);
  314. }
  315. return graph;
  316. };
  317. /**
  318. * @alias module:echarts/data/Graph.Node
  319. */
  320. function Node(id, dataIndex) {
  321. /**
  322. * @type {string}
  323. */
  324. this.id = id == null ? '' : id;
  325. /**
  326. * @type {Array.<module:echarts/data/Graph.Edge>}
  327. */
  328. this.inEdges = [];
  329. /**
  330. * @type {Array.<module:echarts/data/Graph.Edge>}
  331. */
  332. this.outEdges = [];
  333. /**
  334. * @type {Array.<module:echarts/data/Graph.Edge>}
  335. */
  336. this.edges = [];
  337. /**
  338. * @type {module:echarts/data/Graph}
  339. */
  340. this.hostGraph;
  341. /**
  342. * @type {number}
  343. */
  344. this.dataIndex = dataIndex == null ? -1 : dataIndex;
  345. }
  346. Node.prototype = {
  347. constructor: Node,
  348. /**
  349. * @return {number}
  350. */
  351. degree: function () {
  352. return this.edges.length;
  353. },
  354. /**
  355. * @return {number}
  356. */
  357. inDegree: function () {
  358. return this.inEdges.length;
  359. },
  360. /**
  361. * @return {number}
  362. */
  363. outDegree: function () {
  364. return this.outEdges.length;
  365. },
  366. /**
  367. * @param {string} [path]
  368. * @return {module:echarts/model/Model}
  369. */
  370. getModel: function (path) {
  371. if (this.dataIndex < 0) {
  372. return;
  373. }
  374. var graph = this.hostGraph;
  375. var itemModel = graph.data.getItemModel(this.dataIndex);
  376. return itemModel.getModel(path);
  377. }
  378. };
  379. /**
  380. * 图边
  381. * @alias module:echarts/data/Graph.Edge
  382. * @param {module:echarts/data/Graph.Node} n1
  383. * @param {module:echarts/data/Graph.Node} n2
  384. * @param {number} [dataIndex=-1]
  385. */
  386. function Edge(n1, n2, dataIndex) {
  387. /**
  388. * 节点1,如果是有向图则为源节点
  389. * @type {module:echarts/data/Graph.Node}
  390. */
  391. this.node1 = n1;
  392. /**
  393. * 节点2,如果是有向图则为目标节点
  394. * @type {module:echarts/data/Graph.Node}
  395. */
  396. this.node2 = n2;
  397. this.dataIndex = dataIndex == null ? -1 : dataIndex;
  398. }
  399. /**
  400. * @param {string} [path]
  401. * @return {module:echarts/model/Model}
  402. */
  403. Edge.prototype.getModel = function (path) {
  404. if (this.dataIndex < 0) {
  405. return;
  406. }
  407. var graph = this.hostGraph;
  408. var itemModel = graph.edgeData.getItemModel(this.dataIndex);
  409. return itemModel.getModel(path);
  410. };
  411. var createGraphDataProxyMixin = function (hostName, dataName) {
  412. return {
  413. /**
  414. * @param {string=} [dimension='value'] Default 'value'. can be 'a', 'b', 'c', 'd', 'e'.
  415. * @return {number}
  416. */
  417. getValue: function (dimension) {
  418. var data = this[hostName][dataName];
  419. return data.get(data.getDimension(dimension || 'value'), this.dataIndex);
  420. },
  421. /**
  422. * @param {Object|string} key
  423. * @param {*} [value]
  424. */
  425. setVisual: function (key, value) {
  426. this.dataIndex >= 0 && this[hostName][dataName].setItemVisual(this.dataIndex, key, value);
  427. },
  428. /**
  429. * @param {string} key
  430. * @return {boolean}
  431. */
  432. getVisual: function (key, ignoreParent) {
  433. return this[hostName][dataName].getItemVisual(this.dataIndex, key, ignoreParent);
  434. },
  435. /**
  436. * @param {Object} layout
  437. * @return {boolean} [merge=false]
  438. */
  439. setLayout: function (layout, merge) {
  440. this.dataIndex >= 0 && this[hostName][dataName].setItemLayout(this.dataIndex, layout, merge);
  441. },
  442. /**
  443. * @return {Object}
  444. */
  445. getLayout: function () {
  446. return this[hostName][dataName].getItemLayout(this.dataIndex);
  447. },
  448. /**
  449. * @return {module:zrender/Element}
  450. */
  451. getGraphicEl: function () {
  452. return this[hostName][dataName].getItemGraphicEl(this.dataIndex);
  453. },
  454. /**
  455. * @return {number}
  456. */
  457. getRawIndex: function () {
  458. return this[hostName][dataName].getRawIndex(this.dataIndex);
  459. }
  460. };
  461. };
  462. zrUtil.mixin(Node, createGraphDataProxyMixin('hostGraph', 'data'));
  463. zrUtil.mixin(Edge, createGraphDataProxyMixin('hostGraph', 'edgeData'));
  464. Graph.Node = Node;
  465. Graph.Edge = Edge;
  466. enableClassCheck(Node);
  467. enableClassCheck(Edge);
  468. var _default = Graph;
  469. module.exports = _default;