ccba49236dc68ce54e6675e544b1e51dc1492543.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**
  2. * Graph data structure
  3. *
  4. * @module echarts/data/Graph
  5. * @author Yi Shen(https://www.github.com/pissang)
  6. */
  7. define(function(require) {
  8. var util = require('zrender/tool/util');
  9. 'use strict';
  10. /**
  11. * @alias module:echarts/data/Graph
  12. * @constructor
  13. * @param {boolean} directed
  14. */
  15. var Graph = function(directed) {
  16. /**
  17. * 是否是有向图
  18. * @type {boolean}
  19. * @private
  20. */
  21. this._directed = directed || false;
  22. /**
  23. * @type {Array}
  24. */
  25. this.nodes = [];
  26. this.edges = [];
  27. this._nodesMap = {};
  28. this._edgesMap = {};
  29. };
  30. /**
  31. * 是否是有向图
  32. * @return {boolean}
  33. */
  34. Graph.prototype.isDirected = function () {
  35. return this._directed;
  36. };
  37. /**
  38. * 添加一个新的节点
  39. * @param {string} id 节点名称
  40. * @param {*} [data] 存储的数据
  41. */
  42. Graph.prototype.addNode = function (id, data) {
  43. if (this._nodesMap[id]) {
  44. return this._nodesMap[id];
  45. }
  46. var node = new Graph.Node(id, data);
  47. this.nodes.push(node);
  48. this._nodesMap[id] = node;
  49. return node;
  50. };
  51. /**
  52. * 获取节点
  53. * @param {string} id
  54. * @return {module:echarts/data/Graph~Node}
  55. */
  56. Graph.prototype.getNodeById = function (id) {
  57. return this._nodesMap[id];
  58. };
  59. /**
  60. * 添加边
  61. * @param {string|module:echarts/data/Graph~Node} n1
  62. * @param {string|module:echarts/data/Graph~Node} n2
  63. * @param {*} data
  64. * @return {module:echarts/data/Graph~Edge}
  65. */
  66. Graph.prototype.addEdge = function (n1, n2, data) {
  67. if (typeof(n1) == 'string') {
  68. n1 = this._nodesMap[n1];
  69. }
  70. if (typeof(n2) == 'string') {
  71. n2 = this._nodesMap[n2];
  72. }
  73. if (!n1 || !n2) {
  74. return;
  75. }
  76. var key = n1.id + '-' + n2.id;
  77. if (this._edgesMap[key]) {
  78. return this._edgesMap[key];
  79. }
  80. var edge = new Graph.Edge(n1, n2, data);
  81. if (this._directed) {
  82. n1.outEdges.push(edge);
  83. n2.inEdges.push(edge);
  84. }
  85. n1.edges.push(edge);
  86. if (n1 !== n2) {
  87. n2.edges.push(edge);
  88. }
  89. this.edges.push(edge);
  90. this._edgesMap[key] = edge;
  91. return edge;
  92. };
  93. /**
  94. * 移除边
  95. * @param {module:echarts/data/Graph~Edge} edge
  96. */
  97. Graph.prototype.removeEdge = function (edge) {
  98. var n1 = edge.node1;
  99. var n2 = edge.node2;
  100. var key = n1.id + '-' + n2.id;
  101. if (this._directed) {
  102. n1.outEdges.splice(util.indexOf(n1.outEdges, edge), 1);
  103. n2.inEdges.splice(util.indexOf(n2.inEdges, edge), 1);
  104. }
  105. n1.edges.splice(util.indexOf(n1.edges, edge), 1);
  106. if (n1 !== n2) {
  107. n2.edges.splice(util.indexOf(n2.edges, edge), 1);
  108. }
  109. delete this._edgesMap[key];
  110. this.edges.splice(util.indexOf(this.edges, edge), 1);
  111. };
  112. /**
  113. * 获取边
  114. * @param {module:echarts/data/Graph~Node|string} n1
  115. * @param {module:echarts/data/Graph~Node|string} n2
  116. * @return {module:echarts/data/Graph~Edge}
  117. */
  118. Graph.prototype.getEdge = function (n1, n2) {
  119. if (typeof(n1) !== 'string') {
  120. n1 = n1.id;
  121. }
  122. if (typeof(n2) !== 'string') {
  123. n2 = n2.id;
  124. }
  125. if (this._directed) {
  126. return this._edgesMap[n1 + '-' + n2];
  127. } else {
  128. return this._edgesMap[n1 + '-' + n2]
  129. || this._edgesMap[n2 + '-' + n1];
  130. }
  131. };
  132. /**
  133. * 移除节点(及其邻接边)
  134. * @param {module:echarts/data/Graph~Node|string} node
  135. */
  136. Graph.prototype.removeNode = function (node) {
  137. if (typeof(node) === 'string') {
  138. node = this._nodesMap[node];
  139. if (!node) {
  140. return;
  141. }
  142. }
  143. delete this._nodesMap[node.id];
  144. this.nodes.splice(util.indexOf(this.nodes, node), 1);
  145. for (var i = 0; i < this.edges.length;) {
  146. var edge = this.edges[i];
  147. if (edge.node1 === node || edge.node2 === node) {
  148. this.removeEdge(edge);
  149. } else {
  150. i++;
  151. }
  152. }
  153. };
  154. /**
  155. * 遍历并且过滤指定的节点
  156. * @param {Function} cb
  157. * @param {*} [context]
  158. */
  159. Graph.prototype.filterNode = function (cb, context) {
  160. var len = this.nodes.length;
  161. for (var i = 0; i < len;) {
  162. if (cb.call(context, this.nodes[i], i)) {
  163. i++;
  164. } else {
  165. this.removeNode(this.nodes[i]);
  166. len--;
  167. }
  168. }
  169. };
  170. /**
  171. * 遍历并且过滤指定的边
  172. * @param {Function} cb
  173. * @param {*} [context]
  174. */
  175. Graph.prototype.filterEdge = function (cb, context) {
  176. var len = this.edges.length;
  177. for (var i = 0; i < len;) {
  178. if (cb.call(context, this.edges[i], i)) {
  179. i++;
  180. } else {
  181. this.removeEdge(this.edges[i]);
  182. len--;
  183. }
  184. }
  185. };
  186. /**
  187. * 线性遍历所有节点
  188. * @param {Function} cb
  189. * @param {*} [context]
  190. */
  191. Graph.prototype.eachNode = function (cb, context) {
  192. var len = this.nodes.length;
  193. for (var i = 0; i < len; i++) {
  194. if (this.nodes[i]) { // 可能在遍历过程中存在节点删除
  195. cb.call(context, this.nodes[i], i);
  196. }
  197. }
  198. };
  199. /**
  200. * 线性遍历所有边
  201. * @param {Function} cb
  202. * @param {*} [context]
  203. */
  204. Graph.prototype.eachEdge = function (cb, context) {
  205. var len = this.edges.length;
  206. for (var i = 0; i < len; i++) {
  207. if (this.edges[i]) { // 可能在遍历过程中存在边删除
  208. cb.call(context, this.edges[i], i);
  209. }
  210. }
  211. };
  212. /**
  213. * 清空图
  214. */
  215. Graph.prototype.clear = function() {
  216. this.nodes.length = 0;
  217. this.edges.length = 0;
  218. this._nodesMap = {};
  219. this._edgesMap = {};
  220. };
  221. /**
  222. * 广度优先遍历
  223. * @param {Function} cb
  224. * @param {module:echarts/data/Graph~Node} startNode 遍历起始节点
  225. * @param {string} [direction=none] none, in, out 指定遍历边
  226. * @param {*} [context] 回调函数调用context
  227. */
  228. Graph.prototype.breadthFirstTraverse = function (
  229. cb, startNode, direction, context
  230. ) {
  231. if (typeof(startNode) === 'string') {
  232. startNode = this._nodesMap[startNode];
  233. }
  234. if (!startNode) {
  235. return;
  236. }
  237. var edgeType = 'edges';
  238. if (direction === 'out') {
  239. edgeType = 'outEdges';
  240. } else if (direction === 'in') {
  241. edgeType = 'inEdges';
  242. }
  243. for (var i = 0; i < this.nodes.length; i++) {
  244. this.nodes[i].__visited = false;
  245. }
  246. if (cb.call(context, startNode, null)) {
  247. return;
  248. }
  249. var queue = [startNode];
  250. while (queue.length) {
  251. var currentNode = queue.shift();
  252. var edges = currentNode[edgeType];
  253. for (var i = 0; i < edges.length; i++) {
  254. var e = edges[i];
  255. var otherNode = e.node1 === currentNode
  256. ? e.node2 : e.node1;
  257. if (!otherNode.__visited) {
  258. if (cb.call(otherNode, otherNode, currentNode)) {
  259. // Stop traversing
  260. return;
  261. }
  262. queue.push(otherNode);
  263. otherNode.__visited = true;
  264. }
  265. }
  266. }
  267. };
  268. /**
  269. * 复制图
  270. */
  271. Graph.prototype.clone = function () {
  272. var graph = new Graph(this._directed);
  273. for (var i = 0; i < this.nodes.length; i++) {
  274. graph.addNode(this.nodes[i].id, this.nodes[i].data);
  275. }
  276. for (var i = 0; i < this.edges.length; i++) {
  277. var e = this.edges[i];
  278. graph.addEdge(e.node1.id, e.node2.id, e.data);
  279. }
  280. return graph;
  281. };
  282. /**
  283. * 图节点
  284. * @alias module:echarts/data/Graph~Node
  285. * @param {string} id
  286. * @param {*} [data]
  287. */
  288. var Node = function(id, data) {
  289. /**
  290. * 节点名称
  291. * @type {string}
  292. */
  293. this.id = id;
  294. /**
  295. * 节点存储的数据
  296. * @type {*}
  297. */
  298. this.data = data || null;
  299. /**
  300. * 入边,只在有向图上有效
  301. * @type {Array.<module:echarts/data/Graph~Edge>}
  302. */
  303. this.inEdges = [];
  304. /**
  305. * 出边,只在有向图上有效
  306. * @type {Array.<module:echarts/data/Graph~Edge>}
  307. */
  308. this.outEdges = [];
  309. /**
  310. * 邻接边
  311. * @type {Array.<module:echarts/data/Graph~Edge>}
  312. */
  313. this.edges = [];
  314. };
  315. /**
  316. * 度
  317. * @return {number}
  318. */
  319. Node.prototype.degree = function() {
  320. return this.edges.length;
  321. };
  322. /**
  323. * 入度,只在有向图上有效
  324. * @return {number}
  325. */
  326. Node.prototype.inDegree = function() {
  327. return this.inEdges.length;
  328. };
  329. /**
  330. * 出度,只在有向图上有效
  331. * @return {number}
  332. */
  333. Node.prototype.outDegree = function() {
  334. return this.outEdges.length;
  335. };
  336. /**
  337. * 图边
  338. * @alias module:echarts/data/Graph~Edge
  339. * @param {module:echarts/data/Graph~Node} node1
  340. * @param {module:echarts/data/Graph~Node} node2
  341. * @param {extra} data
  342. */
  343. var Edge = function(node1, node2, data) {
  344. /**
  345. * 节点1,如果是有向图则为源节点
  346. * @type {module:echarts/data/Graph~Node}
  347. */
  348. this.node1 = node1;
  349. /**
  350. * 节点2,如果是有向图则为目标节点
  351. * @type {module:echarts/data/Graph~Node}
  352. */
  353. this.node2 = node2;
  354. /**
  355. * 边存储的数据
  356. * @type {*}
  357. */
  358. this.data = data || null;
  359. };
  360. Graph.Node = Node;
  361. Graph.Edge = Edge;
  362. /**
  363. * 从邻接矩阵生成
  364. * ```
  365. * TARGET
  366. * -1--2--3--4--5-
  367. * 1| x x x x x
  368. * 2| x x x x x
  369. * 3| x x x x x SOURCE
  370. * 4| x x x x x
  371. * 5| x x x x x
  372. * ```
  373. * 节点的行列总和会被写到`node.data.value`
  374. * 对于有向图会计算每一行的和写到`node.data.outValue`,
  375. * 计算每一列的和写到`node.data.inValue`。
  376. * 边的权重会被然后写到`edge.data.weight`。
  377. *
  378. * @method module:echarts/data/Graph.fromMatrix
  379. * @param {Array.<Object>} nodesData 节点信息,必须有`id`属性, 会保存到`node.data`中
  380. * @param {Array} matrix 邻接矩阵
  381. * @param {boolean} directed 是否是有向图
  382. * @return {module:echarts/data/Graph}
  383. */
  384. Graph.fromMatrix = function(nodesData, matrix, directed) {
  385. if (
  386. !matrix || !matrix.length
  387. || (matrix[0].length !== matrix.length)
  388. || (nodesData.length !== matrix.length)
  389. ) {
  390. // Not a valid data
  391. return;
  392. }
  393. var size = matrix.length;
  394. var graph = new Graph(directed);
  395. for (var i = 0; i < size; i++) {
  396. var node = graph.addNode(nodesData[i].id, nodesData[i]);
  397. // TODO
  398. // node.data已经有value的情况
  399. node.data.value = 0;
  400. if (directed) {
  401. node.data.outValue = node.data.inValue = 0;
  402. }
  403. }
  404. for (var i = 0; i < size; i++) {
  405. for (var j = 0; j < size; j++) {
  406. var item = matrix[i][j];
  407. if (directed) {
  408. graph.nodes[i].data.outValue += item;
  409. graph.nodes[j].data.inValue += item;
  410. }
  411. graph.nodes[i].data.value += item;
  412. graph.nodes[j].data.value += item;
  413. }
  414. }
  415. for (var i = 0; i < size; i++) {
  416. for (var j = i; j < size; j++) {
  417. var item = matrix[i][j];
  418. if (item === 0) {
  419. continue;
  420. }
  421. var n1 = graph.nodes[i];
  422. var n2 = graph.nodes[j];
  423. var edge = graph.addEdge(n1, n2, {});
  424. edge.data.weight = item;
  425. if (i !== j) {
  426. if (directed && matrix[j][i]) {
  427. var inEdge = graph.addEdge(n2, n1, {});
  428. inEdge.data.weight = matrix[j][i];
  429. }
  430. }
  431. }
  432. }
  433. // console.log(graph.edges.map(function (e) {return e.node1.id + '-' + e.node2.id;}))
  434. return graph;
  435. };
  436. return Graph;
  437. });