Graph.js 11 KB

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