Graph.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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;
  161. var edge = new Edge(n1, n2, dataIndex);
  162. edge.hostGraph = this;
  163. if (this._directed) {
  164. n1.outEdges.push(edge);
  165. n2.inEdges.push(edge);
  166. }
  167. n1.edges.push(edge);
  168. if (n1 !== n2) {
  169. n2.edges.push(edge);
  170. }
  171. this.edges.push(edge);
  172. edgesMap[key] = edge;
  173. return edge;
  174. };
  175. /**
  176. * Get edge by data index
  177. * @param {number} dataIndex
  178. * @return {module:echarts/data/Graph~Node}
  179. */
  180. graphProto.getEdgeByIndex = function (dataIndex) {
  181. var rawIdx = this.edgeData.getRawIndex(dataIndex);
  182. return this.edges[rawIdx];
  183. };
  184. /**
  185. * Get edge by two linked nodes
  186. * @param {module:echarts/data/Graph.Node|string} n1
  187. * @param {module:echarts/data/Graph.Node|string} n2
  188. * @return {module:echarts/data/Graph.Edge}
  189. */
  190. graphProto.getEdge = function (n1, n2) {
  191. if (Node.isInstance(n1)) {
  192. n1 = n1.id;
  193. }
  194. if (Node.isInstance(n2)) {
  195. n2 = n2.id;
  196. }
  197. var edgesMap = this._edgesMap;
  198. if (this._directed) {
  199. return edgesMap[n1 + '-' + n2];
  200. } else {
  201. return edgesMap[n1 + '-' + n2] || edgesMap[n2 + '-' + n1];
  202. }
  203. };
  204. /**
  205. * Iterate all nodes
  206. * @param {Function} cb
  207. * @param {*} [context]
  208. */
  209. graphProto.eachNode = function (cb, context) {
  210. var nodes = this.nodes;
  211. var len = nodes.length;
  212. for (var i = 0; i < len; i++) {
  213. if (nodes[i].dataIndex >= 0) {
  214. cb.call(context, nodes[i], i);
  215. }
  216. }
  217. };
  218. /**
  219. * Iterate all edges
  220. * @param {Function} cb
  221. * @param {*} [context]
  222. */
  223. graphProto.eachEdge = function (cb, context) {
  224. var edges = this.edges;
  225. var len = edges.length;
  226. for (var i = 0; i < len; i++) {
  227. if (edges[i].dataIndex >= 0 && edges[i].node1.dataIndex >= 0 && edges[i].node2.dataIndex >= 0) {
  228. cb.call(context, edges[i], i);
  229. }
  230. }
  231. };
  232. /**
  233. * Breadth first traverse
  234. * @param {Function} cb
  235. * @param {module:echarts/data/Graph.Node} startNode
  236. * @param {string} [direction='none'] 'none'|'in'|'out'
  237. * @param {*} [context]
  238. */
  239. graphProto.breadthFirstTraverse = function (cb, startNode, direction, context) {
  240. if (!Node.isInstance(startNode)) {
  241. startNode = this._nodesMap[generateNodeKey(startNode)];
  242. }
  243. if (!startNode) {
  244. return;
  245. }
  246. var edgeType = direction === 'out' ? 'outEdges' : direction === 'in' ? 'inEdges' : 'edges';
  247. for (var i = 0; i < this.nodes.length; i++) {
  248. this.nodes[i].__visited = false;
  249. }
  250. if (cb.call(context, startNode, null)) {
  251. return;
  252. }
  253. var queue = [startNode];
  254. while (queue.length) {
  255. var currentNode = queue.shift();
  256. var edges = currentNode[edgeType];
  257. for (var i = 0; i < edges.length; i++) {
  258. var e = edges[i];
  259. var otherNode = e.node1 === currentNode ? e.node2 : e.node1;
  260. if (!otherNode.__visited) {
  261. if (cb.call(context, otherNode, currentNode)) {
  262. // Stop traversing
  263. return;
  264. }
  265. queue.push(otherNode);
  266. otherNode.__visited = true;
  267. }
  268. }
  269. }
  270. }; // TODO
  271. // graphProto.depthFirstTraverse = function (
  272. // cb, startNode, direction, context
  273. // ) {
  274. // };
  275. // Filter update
  276. graphProto.update = function () {
  277. var data = this.data;
  278. var edgeData = this.edgeData;
  279. var nodes = this.nodes;
  280. var edges = this.edges;
  281. for (var i = 0, len = nodes.length; i < len; i++) {
  282. nodes[i].dataIndex = -1;
  283. }
  284. for (var i = 0, len = data.count(); i < len; i++) {
  285. nodes[data.getRawIndex(i)].dataIndex = i;
  286. }
  287. edgeData.filterSelf(function (idx) {
  288. var edge = edges[edgeData.getRawIndex(idx)];
  289. return edge.node1.dataIndex >= 0 && edge.node2.dataIndex >= 0;
  290. }); // Update edge
  291. for (var i = 0, len = edges.length; i < len; i++) {
  292. edges[i].dataIndex = -1;
  293. }
  294. for (var i = 0, len = edgeData.count(); i < len; i++) {
  295. edges[edgeData.getRawIndex(i)].dataIndex = i;
  296. }
  297. };
  298. /**
  299. * @return {module:echarts/data/Graph}
  300. */
  301. graphProto.clone = function () {
  302. var graph = new Graph(this._directed);
  303. var nodes = this.nodes;
  304. var edges = this.edges;
  305. for (var i = 0; i < nodes.length; i++) {
  306. graph.addNode(nodes[i].id, nodes[i].dataIndex);
  307. }
  308. for (var i = 0; i < edges.length; i++) {
  309. var e = edges[i];
  310. graph.addEdge(e.node1.id, e.node2.id, e.dataIndex);
  311. }
  312. return graph;
  313. };
  314. /**
  315. * @alias module:echarts/data/Graph.Node
  316. */
  317. function Node(id, dataIndex) {
  318. /**
  319. * @type {string}
  320. */
  321. this.id = id == null ? '' : id;
  322. /**
  323. * @type {Array.<module:echarts/data/Graph.Edge>}
  324. */
  325. this.inEdges = [];
  326. /**
  327. * @type {Array.<module:echarts/data/Graph.Edge>}
  328. */
  329. this.outEdges = [];
  330. /**
  331. * @type {Array.<module:echarts/data/Graph.Edge>}
  332. */
  333. this.edges = [];
  334. /**
  335. * @type {module:echarts/data/Graph}
  336. */
  337. this.hostGraph;
  338. /**
  339. * @type {number}
  340. */
  341. this.dataIndex = dataIndex == null ? -1 : dataIndex;
  342. }
  343. Node.prototype = {
  344. constructor: Node,
  345. /**
  346. * @return {number}
  347. */
  348. degree: function () {
  349. return this.edges.length;
  350. },
  351. /**
  352. * @return {number}
  353. */
  354. inDegree: function () {
  355. return this.inEdges.length;
  356. },
  357. /**
  358. * @return {number}
  359. */
  360. outDegree: function () {
  361. return this.outEdges.length;
  362. },
  363. /**
  364. * @param {string} [path]
  365. * @return {module:echarts/model/Model}
  366. */
  367. getModel: function (path) {
  368. if (this.dataIndex < 0) {
  369. return;
  370. }
  371. var graph = this.hostGraph;
  372. var itemModel = graph.data.getItemModel(this.dataIndex);
  373. return itemModel.getModel(path);
  374. }
  375. };
  376. /**
  377. * 图边
  378. * @alias module:echarts/data/Graph.Edge
  379. * @param {module:echarts/data/Graph.Node} n1
  380. * @param {module:echarts/data/Graph.Node} n2
  381. * @param {number} [dataIndex=-1]
  382. */
  383. function Edge(n1, n2, dataIndex) {
  384. /**
  385. * 节点1,如果是有向图则为源节点
  386. * @type {module:echarts/data/Graph.Node}
  387. */
  388. this.node1 = n1;
  389. /**
  390. * 节点2,如果是有向图则为目标节点
  391. * @type {module:echarts/data/Graph.Node}
  392. */
  393. this.node2 = n2;
  394. this.dataIndex = dataIndex == null ? -1 : dataIndex;
  395. }
  396. /**
  397. * @param {string} [path]
  398. * @return {module:echarts/model/Model}
  399. */
  400. Edge.prototype.getModel = function (path) {
  401. if (this.dataIndex < 0) {
  402. return;
  403. }
  404. var graph = this.hostGraph;
  405. var itemModel = graph.edgeData.getItemModel(this.dataIndex);
  406. return itemModel.getModel(path);
  407. };
  408. var createGraphDataProxyMixin = function (hostName, dataName) {
  409. return {
  410. /**
  411. * @param {string=} [dimension='value'] Default 'value'. can be 'a', 'b', 'c', 'd', 'e'.
  412. * @return {number}
  413. */
  414. getValue: function (dimension) {
  415. var data = this[hostName][dataName];
  416. return data.get(data.getDimension(dimension || 'value'), this.dataIndex);
  417. },
  418. /**
  419. * @param {Object|string} key
  420. * @param {*} [value]
  421. */
  422. setVisual: function (key, value) {
  423. this.dataIndex >= 0 && this[hostName][dataName].setItemVisual(this.dataIndex, key, value);
  424. },
  425. /**
  426. * @param {string} key
  427. * @return {boolean}
  428. */
  429. getVisual: function (key, ignoreParent) {
  430. return this[hostName][dataName].getItemVisual(this.dataIndex, key, ignoreParent);
  431. },
  432. /**
  433. * @param {Object} layout
  434. * @return {boolean} [merge=false]
  435. */
  436. setLayout: function (layout, merge) {
  437. this.dataIndex >= 0 && this[hostName][dataName].setItemLayout(this.dataIndex, layout, merge);
  438. },
  439. /**
  440. * @return {Object}
  441. */
  442. getLayout: function () {
  443. return this[hostName][dataName].getItemLayout(this.dataIndex);
  444. },
  445. /**
  446. * @return {module:zrender/Element}
  447. */
  448. getGraphicEl: function () {
  449. return this[hostName][dataName].getItemGraphicEl(this.dataIndex);
  450. },
  451. /**
  452. * @return {number}
  453. */
  454. getRawIndex: function () {
  455. return this[hostName][dataName].getRawIndex(this.dataIndex);
  456. }
  457. };
  458. };
  459. zrUtil.mixin(Node, createGraphDataProxyMixin('hostGraph', 'data'));
  460. zrUtil.mixin(Edge, createGraphDataProxyMixin('hostGraph', 'edgeData'));
  461. Graph.Node = Node;
  462. Graph.Edge = Edge;
  463. enableClassCheck(Node);
  464. enableClassCheck(Edge);
  465. var _default = Graph;
  466. module.exports = _default;