force.js 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  1. define('echarts/chart/force', [
  2. 'require',
  3. './base',
  4. '../data/Graph',
  5. '../layout/Force',
  6. 'zrender/shape/Line',
  7. 'zrender/shape/BezierCurve',
  8. 'zrender/shape/Image',
  9. '../util/shape/Icon',
  10. '../config',
  11. '../util/ecData',
  12. 'zrender/tool/util',
  13. 'zrender/config',
  14. 'zrender/tool/vector',
  15. '../chart'
  16. ], function (require) {
  17. 'use strict';
  18. var ChartBase = require('./base');
  19. var Graph = require('../data/Graph');
  20. var ForceLayout = require('../layout/Force');
  21. var LineShape = require('zrender/shape/Line');
  22. var BezierCurveShape = require('zrender/shape/BezierCurve');
  23. var ImageShape = require('zrender/shape/Image');
  24. var IconShape = require('../util/shape/Icon');
  25. var ecConfig = require('../config');
  26. ecConfig.force = {
  27. zlevel: 1,
  28. z: 2,
  29. center: [
  30. '50%',
  31. '50%'
  32. ],
  33. size: '100%',
  34. preventOverlap: false,
  35. coolDown: 0.99,
  36. minRadius: 10,
  37. maxRadius: 20,
  38. ratioScaling: false,
  39. large: false,
  40. useWorker: false,
  41. steps: 1,
  42. scaling: 1,
  43. gravity: 1,
  44. symbol: 'circle',
  45. symbolSize: 0,
  46. linkSymbol: null,
  47. linkSymbolSize: [
  48. 10,
  49. 15
  50. ],
  51. draggable: true,
  52. clickable: true,
  53. roam: false,
  54. itemStyle: {
  55. normal: {
  56. label: {
  57. show: false,
  58. position: 'inside'
  59. },
  60. nodeStyle: {
  61. brushType: 'both',
  62. borderColor: '#5182ab',
  63. borderWidth: 1
  64. },
  65. linkStyle: {
  66. color: '#5182ab',
  67. width: 1,
  68. type: 'line'
  69. }
  70. },
  71. emphasis: {
  72. label: { show: false },
  73. nodeStyle: {},
  74. linkStyle: { opacity: 0 }
  75. }
  76. }
  77. };
  78. var ecData = require('../util/ecData');
  79. var zrUtil = require('zrender/tool/util');
  80. var zrConfig = require('zrender/config');
  81. var vec2 = require('zrender/tool/vector');
  82. function Force(ecTheme, messageCenter, zr, option, myChart) {
  83. var self = this;
  84. ChartBase.call(this, ecTheme, messageCenter, zr, option, myChart);
  85. this.__nodePositionMap = {};
  86. this._graph = new Graph(true);
  87. this._layout = new ForceLayout();
  88. this._layout.onupdate = function () {
  89. self._step();
  90. };
  91. this._steps = 1;
  92. this.ondragstart = function () {
  93. ondragstart.apply(self, arguments);
  94. };
  95. this.ondragend = function () {
  96. ondragend.apply(self, arguments);
  97. };
  98. this.ondrop = function () {
  99. };
  100. this.shapeHandler.ondragstart = function () {
  101. self.isDragstart = true;
  102. };
  103. this.onmousemove = function () {
  104. onmousemove.apply(self, arguments);
  105. };
  106. this.refresh(option);
  107. }
  108. Force.prototype = {
  109. constructor: Force,
  110. type: ecConfig.CHART_TYPE_FORCE,
  111. _init: function () {
  112. var legend = this.component.legend;
  113. var series = this.series;
  114. var serieName;
  115. this.clear();
  116. for (var i = 0, l = series.length; i < l; i++) {
  117. var serie = series[i];
  118. if (serie.type === ecConfig.CHART_TYPE_FORCE) {
  119. series[i] = this.reformOption(series[i]);
  120. serieName = series[i].name || '';
  121. this.selectedMap[serieName] = legend ? legend.isSelected(serieName) : true;
  122. if (!this.selectedMap[serieName]) {
  123. continue;
  124. }
  125. this.buildMark(i);
  126. this._initSerie(serie, i);
  127. break;
  128. }
  129. }
  130. this.animationEffect();
  131. },
  132. _getNodeCategory: function (serie, node) {
  133. return serie.categories && serie.categories[node.category || 0];
  134. },
  135. _getNodeQueryTarget: function (serie, node, type) {
  136. type = type || 'normal';
  137. var category = this._getNodeCategory(serie, node) || {};
  138. return [
  139. node.itemStyle && node.itemStyle[type],
  140. category && category.itemStyle && category.itemStyle[type],
  141. serie.itemStyle[type].nodeStyle
  142. ];
  143. },
  144. _getEdgeQueryTarget: function (serie, edge, type) {
  145. type = type || 'normal';
  146. return [
  147. edge.itemStyle && edge.itemStyle[type],
  148. serie.itemStyle[type].linkStyle
  149. ];
  150. },
  151. _initSerie: function (serie, serieIdx) {
  152. this._temperature = 1;
  153. if (serie.data) {
  154. this._graph = this._getSerieGraphFromDataMatrix(serie);
  155. } else {
  156. this._graph = this._getSerieGraphFromNodeLinks(serie);
  157. }
  158. this._buildLinkShapes(serie, serieIdx);
  159. this._buildNodeShapes(serie, serieIdx);
  160. var panable = serie.roam === true || serie.roam === 'move';
  161. var zoomable = serie.roam === true || serie.roam === 'scale';
  162. this.zr.modLayer(this.getZlevelBase(), {
  163. panable: panable,
  164. zoomable: zoomable
  165. });
  166. if (this.query('markPoint.effect.show') || this.query('markLine.effect.show')) {
  167. this.zr.modLayer(ecConfig.EFFECT_ZLEVEL, {
  168. panable: panable,
  169. zoomable: zoomable
  170. });
  171. }
  172. this._initLayout(serie);
  173. this._step();
  174. },
  175. _getSerieGraphFromDataMatrix: function (serie) {
  176. var nodesData = [];
  177. var count = 0;
  178. var matrix = [];
  179. for (var i = 0; i < serie.matrix.length; i++) {
  180. matrix[i] = serie.matrix[i].slice();
  181. }
  182. var data = serie.data || serie.nodes;
  183. for (var i = 0; i < data.length; i++) {
  184. var node = {};
  185. var group = data[i];
  186. for (var key in group) {
  187. if (key === 'name') {
  188. node['id'] = group['name'];
  189. } else {
  190. node[key] = group[key];
  191. }
  192. }
  193. var category = this._getNodeCategory(serie, group);
  194. var name = category ? category.name : group.name;
  195. this.selectedMap[name] = this.isSelected(name);
  196. if (this.selectedMap[name]) {
  197. nodesData.push(node);
  198. count++;
  199. } else {
  200. matrix.splice(count, 1);
  201. for (var j = 0; j < matrix.length; j++) {
  202. matrix[j].splice(count, 1);
  203. }
  204. }
  205. }
  206. var graph = Graph.fromMatrix(nodesData, matrix, true);
  207. graph.eachNode(function (n, idx) {
  208. n.layout = {
  209. size: n.data.value,
  210. mass: 0
  211. };
  212. n.rawIndex = idx;
  213. });
  214. graph.eachEdge(function (e) {
  215. e.layout = { weight: e.data.weight };
  216. });
  217. return graph;
  218. },
  219. _getSerieGraphFromNodeLinks: function (serie) {
  220. var graph = new Graph(true);
  221. var nodes = serie.data || serie.nodes;
  222. for (var i = 0, len = nodes.length; i < len; i++) {
  223. var n = nodes[i];
  224. if (!n || n.ignore) {
  225. continue;
  226. }
  227. var category = this._getNodeCategory(serie, n);
  228. var name = category ? category.name : n.name;
  229. this.selectedMap[name] = this.isSelected(name);
  230. if (this.selectedMap[name]) {
  231. var node = graph.addNode(n.name, n);
  232. node.rawIndex = i;
  233. }
  234. }
  235. for (var i = 0, len = serie.links.length; i < len; i++) {
  236. var e = serie.links[i];
  237. var n1 = e.source;
  238. var n2 = e.target;
  239. if (typeof n1 === 'number') {
  240. n1 = nodes[n1];
  241. if (n1) {
  242. n1 = n1.name;
  243. }
  244. }
  245. if (typeof n2 === 'number') {
  246. n2 = nodes[n2];
  247. if (n2) {
  248. n2 = n2.name;
  249. }
  250. }
  251. var edge = graph.addEdge(n1, n2, e);
  252. if (edge) {
  253. edge.rawIndex = i;
  254. }
  255. }
  256. graph.eachNode(function (n) {
  257. var value = n.data.value;
  258. if (value == null) {
  259. value = 0;
  260. for (var i = 0; i < n.edges.length; i++) {
  261. value += n.edges[i].data.weight || 0;
  262. }
  263. }
  264. n.layout = {
  265. size: value,
  266. mass: 0
  267. };
  268. });
  269. graph.eachEdge(function (e) {
  270. e.layout = { weight: e.data.weight == null ? 1 : e.data.weight };
  271. });
  272. return graph;
  273. },
  274. _initLayout: function (serie) {
  275. var graph = this._graph;
  276. var len = graph.nodes.length;
  277. var minRadius = this.query(serie, 'minRadius');
  278. var maxRadius = this.query(serie, 'maxRadius');
  279. this._steps = serie.steps || 1;
  280. this._layout.center = this.parseCenter(this.zr, serie.center);
  281. this._layout.width = this.parsePercent(serie.size, this.zr.getWidth());
  282. this._layout.height = this.parsePercent(serie.size, this.zr.getHeight());
  283. this._layout.large = serie.large;
  284. this._layout.scaling = serie.scaling;
  285. this._layout.ratioScaling = serie.ratioScaling;
  286. this._layout.gravity = serie.gravity;
  287. this._layout.temperature = 1;
  288. this._layout.coolDown = serie.coolDown;
  289. this._layout.preventNodeEdgeOverlap = serie.preventOverlap;
  290. this._layout.preventNodeOverlap = serie.preventOverlap;
  291. var min = Infinity;
  292. var max = -Infinity;
  293. for (var i = 0; i < len; i++) {
  294. var gNode = graph.nodes[i];
  295. max = Math.max(gNode.layout.size, max);
  296. min = Math.min(gNode.layout.size, min);
  297. }
  298. var divider = max - min;
  299. for (var i = 0; i < len; i++) {
  300. var gNode = graph.nodes[i];
  301. if (divider > 0) {
  302. gNode.layout.size = (gNode.layout.size - min) * (maxRadius - minRadius) / divider + minRadius;
  303. gNode.layout.mass = gNode.layout.size / maxRadius;
  304. } else {
  305. gNode.layout.size = (maxRadius - minRadius) / 2;
  306. gNode.layout.mass = 0.5;
  307. }
  308. }
  309. for (var i = 0; i < len; i++) {
  310. var gNode = graph.nodes[i];
  311. if (typeof this.__nodePositionMap[gNode.id] !== 'undefined') {
  312. gNode.layout.position = vec2.create();
  313. vec2.copy(gNode.layout.position, this.__nodePositionMap[gNode.id]);
  314. } else if (typeof gNode.data.initial !== 'undefined') {
  315. gNode.layout.position = vec2.create();
  316. vec2.copy(gNode.layout.position, gNode.data.initial);
  317. } else {
  318. var center = this._layout.center;
  319. var size = Math.min(this._layout.width, this._layout.height);
  320. gNode.layout.position = _randomInSquare(center[0], center[1], size * 0.8);
  321. }
  322. var style = gNode.shape.style;
  323. var radius = gNode.layout.size;
  324. style.width = style.width || radius * 2;
  325. style.height = style.height || radius * 2;
  326. style.x = -style.width / 2;
  327. style.y = -style.height / 2;
  328. vec2.copy(gNode.shape.position, gNode.layout.position);
  329. }
  330. len = graph.edges.length;
  331. max = -Infinity;
  332. for (var i = 0; i < len; i++) {
  333. var e = graph.edges[i];
  334. if (e.layout.weight > max) {
  335. max = e.layout.weight;
  336. }
  337. }
  338. for (var i = 0; i < len; i++) {
  339. var e = graph.edges[i];
  340. e.layout.weight /= max;
  341. }
  342. this._layout.init(graph, serie.useWorker);
  343. },
  344. _buildNodeShapes: function (serie, serieIdx) {
  345. var graph = this._graph;
  346. var categories = this.query(serie, 'categories');
  347. graph.eachNode(function (node) {
  348. var category = this._getNodeCategory(serie, node.data);
  349. var queryTarget = [
  350. node.data,
  351. category,
  352. serie
  353. ];
  354. var styleQueryTarget = this._getNodeQueryTarget(serie, node.data);
  355. var emphasisStyleQueryTarget = this._getNodeQueryTarget(serie, node.data, 'emphasis');
  356. var shape = new IconShape({
  357. style: {
  358. x: 0,
  359. y: 0,
  360. color: this.deepQuery(styleQueryTarget, 'color'),
  361. brushType: 'both',
  362. strokeColor: this.deepQuery(styleQueryTarget, 'strokeColor') || this.deepQuery(styleQueryTarget, 'borderColor'),
  363. lineWidth: this.deepQuery(styleQueryTarget, 'lineWidth') || this.deepQuery(styleQueryTarget, 'borderWidth')
  364. },
  365. highlightStyle: {
  366. color: this.deepQuery(emphasisStyleQueryTarget, 'color'),
  367. strokeColor: this.deepQuery(emphasisStyleQueryTarget, 'strokeColor') || this.deepQuery(emphasisStyleQueryTarget, 'borderColor'),
  368. lineWidth: this.deepQuery(emphasisStyleQueryTarget, 'lineWidth') || this.deepQuery(emphasisStyleQueryTarget, 'borderWidth')
  369. },
  370. clickable: serie.clickable,
  371. zlevel: this.getZlevelBase(),
  372. z: this.getZBase()
  373. });
  374. if (!shape.style.color) {
  375. shape.style.color = category ? this.getColor(category.name) : this.getColor(node.id);
  376. }
  377. shape.style.iconType = this.deepQuery(queryTarget, 'symbol');
  378. shape.style.width = shape.style.height = (this.deepQuery(queryTarget, 'symbolSize') || 0) * 2;
  379. if (shape.style.iconType.match('image')) {
  380. shape.style.image = shape.style.iconType.replace(new RegExp('^image:\\/\\/'), '');
  381. shape = new ImageShape({
  382. style: shape.style,
  383. highlightStyle: shape.highlightStyle,
  384. clickable: shape.clickable,
  385. zlevel: this.getZlevelBase(),
  386. z: this.getZBase()
  387. });
  388. }
  389. if (this.deepQuery(queryTarget, 'itemStyle.normal.label.show')) {
  390. shape.style.text = node.data.label == null ? node.id : node.data.label;
  391. shape.style.textPosition = this.deepQuery(queryTarget, 'itemStyle.normal.label.position');
  392. shape.style.textColor = this.deepQuery(queryTarget, 'itemStyle.normal.label.textStyle.color');
  393. shape.style.textFont = this.getFont(this.deepQuery(queryTarget, 'itemStyle.normal.label.textStyle') || {});
  394. }
  395. if (this.deepQuery(queryTarget, 'itemStyle.emphasis.label.show')) {
  396. shape.highlightStyle.textPosition = this.deepQuery(queryTarget, 'itemStyle.emphasis.label.position');
  397. shape.highlightStyle.textColor = this.deepQuery(queryTarget, 'itemStyle.emphasis.label.textStyle.color');
  398. shape.highlightStyle.textFont = this.getFont(this.deepQuery(queryTarget, 'itemStyle.emphasis.label.textStyle') || {});
  399. }
  400. if (this.deepQuery(queryTarget, 'draggable')) {
  401. this.setCalculable(shape);
  402. shape.dragEnableTime = 0;
  403. shape.draggable = true;
  404. shape.ondragstart = this.shapeHandler.ondragstart;
  405. shape.ondragover = null;
  406. }
  407. var categoryName = '';
  408. if (typeof node.category !== 'undefined') {
  409. var category = categories[node.category];
  410. categoryName = category && category.name || '';
  411. }
  412. ecData.pack(shape, serie, serieIdx, node.data, node.rawIndex, node.data.name || '', node.category);
  413. this.shapeList.push(shape);
  414. this.zr.addShape(shape);
  415. node.shape = shape;
  416. }, this);
  417. },
  418. _buildLinkShapes: function (serie, serieIdx) {
  419. var graph = this._graph;
  420. var len = graph.edges.length;
  421. for (var i = 0; i < len; i++) {
  422. var gEdge = graph.edges[i];
  423. var link = gEdge.data;
  424. var source = gEdge.node1;
  425. var target = gEdge.node2;
  426. var otherEdge = graph.getEdge(target, source);
  427. var queryTarget = this._getEdgeQueryTarget(serie, link);
  428. var linkType = this.deepQuery(queryTarget, 'type');
  429. if (serie.linkSymbol && serie.linkSymbol !== 'none') {
  430. linkType = 'line';
  431. }
  432. var LinkShapeCtor = linkType === 'line' ? LineShape : BezierCurveShape;
  433. var linkShape = new LinkShapeCtor({
  434. style: {
  435. xStart: 0,
  436. yStart: 0,
  437. xEnd: 0,
  438. yEnd: 0
  439. },
  440. clickable: this.query(serie, 'clickable'),
  441. highlightStyle: {},
  442. zlevel: this.getZlevelBase(),
  443. z: this.getZBase()
  444. });
  445. if (otherEdge && otherEdge.shape) {
  446. linkShape.style.offset = 4;
  447. otherEdge.shape.style.offset = 4;
  448. }
  449. zrUtil.merge(linkShape.style, this.query(serie, 'itemStyle.normal.linkStyle'), true);
  450. zrUtil.merge(linkShape.highlightStyle, this.query(serie, 'itemStyle.emphasis.linkStyle'), true);
  451. if (typeof link.itemStyle !== 'undefined') {
  452. if (link.itemStyle.normal) {
  453. zrUtil.merge(linkShape.style, link.itemStyle.normal, true);
  454. }
  455. if (link.itemStyle.emphasis) {
  456. zrUtil.merge(linkShape.highlightStyle, link.itemStyle.emphasis, true);
  457. }
  458. }
  459. linkShape.style.lineWidth = linkShape.style.lineWidth || linkShape.style.width;
  460. linkShape.style.strokeColor = linkShape.style.strokeColor || linkShape.style.color;
  461. linkShape.highlightStyle.lineWidth = linkShape.highlightStyle.lineWidth || linkShape.highlightStyle.width;
  462. linkShape.highlightStyle.strokeColor = linkShape.highlightStyle.strokeColor || linkShape.highlightStyle.color;
  463. ecData.pack(linkShape, serie, serieIdx, gEdge.data, gEdge.rawIndex == null ? i : gEdge.rawIndex, gEdge.data.name || source.id + ' - ' + target.id, source.id, target.id);
  464. this.shapeList.push(linkShape);
  465. this.zr.addShape(linkShape);
  466. gEdge.shape = linkShape;
  467. if (serie.linkSymbol && serie.linkSymbol !== 'none') {
  468. var symbolShape = new IconShape({
  469. style: {
  470. x: -5,
  471. y: 0,
  472. width: serie.linkSymbolSize[0],
  473. height: serie.linkSymbolSize[1],
  474. iconType: serie.linkSymbol,
  475. brushType: 'fill',
  476. color: linkShape.style.strokeColor
  477. },
  478. highlightStyle: { brushType: 'fill' },
  479. position: [
  480. 0,
  481. 0
  482. ],
  483. rotation: 0
  484. });
  485. linkShape._symbolShape = symbolShape;
  486. this.shapeList.push(symbolShape);
  487. this.zr.addShape(symbolShape);
  488. }
  489. }
  490. },
  491. _updateLinkShapes: function () {
  492. var v = vec2.create();
  493. var n = vec2.create();
  494. var p1 = vec2.create();
  495. var p2 = vec2.create();
  496. var edges = this._graph.edges;
  497. for (var i = 0, len = edges.length; i < len; i++) {
  498. var edge = edges[i];
  499. var sourceShape = edge.node1.shape;
  500. var targetShape = edge.node2.shape;
  501. vec2.copy(p1, sourceShape.position);
  502. vec2.copy(p2, targetShape.position);
  503. var edgeShapeStyle = edge.shape.style;
  504. vec2.sub(v, p1, p2);
  505. vec2.normalize(v, v);
  506. if (edgeShapeStyle.offset) {
  507. n[0] = v[1];
  508. n[1] = -v[0];
  509. vec2.scaleAndAdd(p1, p1, n, edgeShapeStyle.offset);
  510. vec2.scaleAndAdd(p2, p2, n, edgeShapeStyle.offset);
  511. } else if (edge.shape.type === 'bezier-curve') {
  512. edgeShapeStyle.cpX1 = (p1[0] + p2[0]) / 2 - (p2[1] - p1[1]) / 4;
  513. edgeShapeStyle.cpY1 = (p1[1] + p2[1]) / 2 - (p1[0] - p2[0]) / 4;
  514. }
  515. edgeShapeStyle.xStart = p1[0];
  516. edgeShapeStyle.yStart = p1[1];
  517. edgeShapeStyle.xEnd = p2[0];
  518. edgeShapeStyle.yEnd = p2[1];
  519. edge.shape.modSelf();
  520. if (edge.shape._symbolShape) {
  521. var symbolShape = edge.shape._symbolShape;
  522. vec2.copy(symbolShape.position, p2);
  523. vec2.scaleAndAdd(symbolShape.position, symbolShape.position, v, targetShape.style.width / 2 + 2);
  524. var angle = Math.atan2(v[1], v[0]);
  525. symbolShape.rotation = Math.PI / 2 - angle;
  526. symbolShape.modSelf();
  527. }
  528. }
  529. },
  530. _syncNodePositions: function () {
  531. var graph = this._graph;
  532. for (var i = 0; i < graph.nodes.length; i++) {
  533. var gNode = graph.nodes[i];
  534. var position = gNode.layout.position;
  535. var node = gNode.data;
  536. var shape = gNode.shape;
  537. var fixX = shape.fixed || node.fixX;
  538. var fixY = shape.fixed || node.fixY;
  539. if (fixX === true) {
  540. fixX = 1;
  541. } else if (isNaN(fixX)) {
  542. fixX = 0;
  543. }
  544. if (fixY === true) {
  545. fixY = 1;
  546. } else if (isNaN(fixY)) {
  547. fixY = 0;
  548. }
  549. shape.position[0] += (position[0] - shape.position[0]) * (1 - fixX);
  550. shape.position[1] += (position[1] - shape.position[1]) * (1 - fixY);
  551. vec2.copy(position, shape.position);
  552. var nodeName = node.name;
  553. if (nodeName) {
  554. var gPos = this.__nodePositionMap[nodeName];
  555. if (!gPos) {
  556. gPos = this.__nodePositionMap[nodeName] = vec2.create();
  557. }
  558. vec2.copy(gPos, position);
  559. }
  560. shape.modSelf();
  561. }
  562. },
  563. _step: function (e) {
  564. this._syncNodePositions();
  565. this._updateLinkShapes();
  566. this.zr.refreshNextFrame();
  567. if (this._layout.temperature > 0.01) {
  568. this._layout.step(this._steps);
  569. } else {
  570. this.messageCenter.dispatch(ecConfig.EVENT.FORCE_LAYOUT_END, {}, {}, this.myChart);
  571. }
  572. },
  573. refresh: function (newOption) {
  574. if (newOption) {
  575. this.option = newOption;
  576. this.series = this.option.series;
  577. }
  578. this.legend = this.component.legend;
  579. if (this.legend) {
  580. this.getColor = function (param) {
  581. return this.legend.getColor(param);
  582. };
  583. this.isSelected = function (param) {
  584. return this.legend.isSelected(param);
  585. };
  586. } else {
  587. var colorMap = {};
  588. var count = 0;
  589. this.getColor = function (key) {
  590. if (colorMap[key]) {
  591. return colorMap[key];
  592. }
  593. if (!colorMap[key]) {
  594. colorMap[key] = this.zr.getColor(count++);
  595. }
  596. return colorMap[key];
  597. };
  598. this.isSelected = function () {
  599. return true;
  600. };
  601. }
  602. this._init();
  603. },
  604. dispose: function () {
  605. this.clear();
  606. this.shapeList = null;
  607. this.effectList = null;
  608. this._layout.dispose();
  609. this._layout = null;
  610. this.__nodePositionMap = {};
  611. },
  612. getPosition: function () {
  613. var position = [];
  614. this._graph.eachNode(function (n) {
  615. if (n.layout) {
  616. position.push({
  617. name: n.data.name,
  618. position: Array.prototype.slice.call(n.layout.position)
  619. });
  620. }
  621. });
  622. return position;
  623. }
  624. };
  625. function ondragstart(param) {
  626. if (!this.isDragstart || !param.target) {
  627. return;
  628. }
  629. var shape = param.target;
  630. shape.fixed = true;
  631. this.isDragstart = false;
  632. this.zr.on(zrConfig.EVENT.MOUSEMOVE, this.onmousemove);
  633. }
  634. function onmousemove() {
  635. this._layout.temperature = 0.8;
  636. this._step();
  637. }
  638. function ondragend(param, status) {
  639. if (!this.isDragend || !param.target) {
  640. return;
  641. }
  642. var shape = param.target;
  643. shape.fixed = false;
  644. status.dragIn = true;
  645. status.needRefresh = false;
  646. this.isDragend = false;
  647. this.zr.un(zrConfig.EVENT.MOUSEMOVE, this.onmousemove);
  648. }
  649. function _randomInSquare(x, y, size) {
  650. var v = vec2.create();
  651. v[0] = (Math.random() - 0.5) * size + x;
  652. v[1] = (Math.random() - 0.5) * size + y;
  653. return v;
  654. }
  655. zrUtil.inherits(Force, ChartBase);
  656. require('../chart').define('force', Force);
  657. return Force;
  658. });define('echarts/data/Graph', [
  659. 'require',
  660. 'zrender/tool/util'
  661. ], function (require) {
  662. var util = require('zrender/tool/util');
  663. 'use strict';
  664. var Graph = function (directed) {
  665. this._directed = directed || false;
  666. this.nodes = [];
  667. this.edges = [];
  668. this._nodesMap = {};
  669. this._edgesMap = {};
  670. };
  671. Graph.prototype.isDirected = function () {
  672. return this._directed;
  673. };
  674. Graph.prototype.addNode = function (id, data) {
  675. if (this._nodesMap[id]) {
  676. return this._nodesMap[id];
  677. }
  678. var node = new Graph.Node(id, data);
  679. this.nodes.push(node);
  680. this._nodesMap[id] = node;
  681. return node;
  682. };
  683. Graph.prototype.getNodeById = function (id) {
  684. return this._nodesMap[id];
  685. };
  686. Graph.prototype.addEdge = function (n1, n2, data) {
  687. if (typeof n1 == 'string') {
  688. n1 = this._nodesMap[n1];
  689. }
  690. if (typeof n2 == 'string') {
  691. n2 = this._nodesMap[n2];
  692. }
  693. if (!n1 || !n2) {
  694. return;
  695. }
  696. var key = n1.id + '-' + n2.id;
  697. if (this._edgesMap[key]) {
  698. return this._edgesMap[key];
  699. }
  700. var edge = new Graph.Edge(n1, n2, data);
  701. if (this._directed) {
  702. n1.outEdges.push(edge);
  703. n2.inEdges.push(edge);
  704. }
  705. n1.edges.push(edge);
  706. if (n1 !== n2) {
  707. n2.edges.push(edge);
  708. }
  709. this.edges.push(edge);
  710. this._edgesMap[key] = edge;
  711. return edge;
  712. };
  713. Graph.prototype.removeEdge = function (edge) {
  714. var n1 = edge.node1;
  715. var n2 = edge.node2;
  716. var key = n1.id + '-' + n2.id;
  717. if (this._directed) {
  718. n1.outEdges.splice(util.indexOf(n1.outEdges, edge), 1);
  719. n2.inEdges.splice(util.indexOf(n2.inEdges, edge), 1);
  720. }
  721. n1.edges.splice(util.indexOf(n1.edges, edge), 1);
  722. if (n1 !== n2) {
  723. n2.edges.splice(util.indexOf(n2.edges, edge), 1);
  724. }
  725. delete this._edgesMap[key];
  726. this.edges.splice(util.indexOf(this.edges, edge), 1);
  727. };
  728. Graph.prototype.getEdge = function (n1, n2) {
  729. if (typeof n1 !== 'string') {
  730. n1 = n1.id;
  731. }
  732. if (typeof n2 !== 'string') {
  733. n2 = n2.id;
  734. }
  735. if (this._directed) {
  736. return this._edgesMap[n1 + '-' + n2];
  737. } else {
  738. return this._edgesMap[n1 + '-' + n2] || this._edgesMap[n2 + '-' + n1];
  739. }
  740. };
  741. Graph.prototype.removeNode = function (node) {
  742. if (typeof node === 'string') {
  743. node = this._nodesMap[node];
  744. if (!node) {
  745. return;
  746. }
  747. }
  748. delete this._nodesMap[node.id];
  749. this.nodes.splice(util.indexOf(this.nodes, node), 1);
  750. for (var i = 0; i < this.edges.length;) {
  751. var edge = this.edges[i];
  752. if (edge.node1 === node || edge.node2 === node) {
  753. this.removeEdge(edge);
  754. } else {
  755. i++;
  756. }
  757. }
  758. };
  759. Graph.prototype.filterNode = function (cb, context) {
  760. var len = this.nodes.length;
  761. for (var i = 0; i < len;) {
  762. if (cb.call(context, this.nodes[i], i)) {
  763. i++;
  764. } else {
  765. this.removeNode(this.nodes[i]);
  766. len--;
  767. }
  768. }
  769. };
  770. Graph.prototype.filterEdge = function (cb, context) {
  771. var len = this.edges.length;
  772. for (var i = 0; i < len;) {
  773. if (cb.call(context, this.edges[i], i)) {
  774. i++;
  775. } else {
  776. this.removeEdge(this.edges[i]);
  777. len--;
  778. }
  779. }
  780. };
  781. Graph.prototype.eachNode = function (cb, context) {
  782. var len = this.nodes.length;
  783. for (var i = 0; i < len; i++) {
  784. if (this.nodes[i]) {
  785. cb.call(context, this.nodes[i], i);
  786. }
  787. }
  788. };
  789. Graph.prototype.eachEdge = function (cb, context) {
  790. var len = this.edges.length;
  791. for (var i = 0; i < len; i++) {
  792. if (this.edges[i]) {
  793. cb.call(context, this.edges[i], i);
  794. }
  795. }
  796. };
  797. Graph.prototype.clear = function () {
  798. this.nodes.length = 0;
  799. this.edges.length = 0;
  800. this._nodesMap = {};
  801. this._edgesMap = {};
  802. };
  803. Graph.prototype.breadthFirstTraverse = function (cb, startNode, direction, context) {
  804. if (typeof startNode === 'string') {
  805. startNode = this._nodesMap[startNode];
  806. }
  807. if (!startNode) {
  808. return;
  809. }
  810. var edgeType = 'edges';
  811. if (direction === 'out') {
  812. edgeType = 'outEdges';
  813. } else if (direction === 'in') {
  814. edgeType = 'inEdges';
  815. }
  816. for (var i = 0; i < this.nodes.length; i++) {
  817. this.nodes[i].__visited = false;
  818. }
  819. if (cb.call(context, startNode, null)) {
  820. return;
  821. }
  822. var queue = [startNode];
  823. while (queue.length) {
  824. var currentNode = queue.shift();
  825. var edges = currentNode[edgeType];
  826. for (var i = 0; i < edges.length; i++) {
  827. var e = edges[i];
  828. var otherNode = e.node1 === currentNode ? e.node2 : e.node1;
  829. if (!otherNode.__visited) {
  830. if (cb.call(otherNode, otherNode, currentNode)) {
  831. return;
  832. }
  833. queue.push(otherNode);
  834. otherNode.__visited = true;
  835. }
  836. }
  837. }
  838. };
  839. Graph.prototype.clone = function () {
  840. var graph = new Graph(this._directed);
  841. for (var i = 0; i < this.nodes.length; i++) {
  842. graph.addNode(this.nodes[i].id, this.nodes[i].data);
  843. }
  844. for (var i = 0; i < this.edges.length; i++) {
  845. var e = this.edges[i];
  846. graph.addEdge(e.node1.id, e.node2.id, e.data);
  847. }
  848. return graph;
  849. };
  850. var Node = function (id, data) {
  851. this.id = id;
  852. this.data = data || null;
  853. this.inEdges = [];
  854. this.outEdges = [];
  855. this.edges = [];
  856. };
  857. Node.prototype.degree = function () {
  858. return this.edges.length;
  859. };
  860. Node.prototype.inDegree = function () {
  861. return this.inEdges.length;
  862. };
  863. Node.prototype.outDegree = function () {
  864. return this.outEdges.length;
  865. };
  866. var Edge = function (node1, node2, data) {
  867. this.node1 = node1;
  868. this.node2 = node2;
  869. this.data = data || null;
  870. };
  871. Graph.Node = Node;
  872. Graph.Edge = Edge;
  873. Graph.fromMatrix = function (nodesData, matrix, directed) {
  874. if (!matrix || !matrix.length || matrix[0].length !== matrix.length || nodesData.length !== matrix.length) {
  875. return;
  876. }
  877. var size = matrix.length;
  878. var graph = new Graph(directed);
  879. for (var i = 0; i < size; i++) {
  880. var node = graph.addNode(nodesData[i].id, nodesData[i]);
  881. node.data.value = 0;
  882. if (directed) {
  883. node.data.outValue = node.data.inValue = 0;
  884. }
  885. }
  886. for (var i = 0; i < size; i++) {
  887. for (var j = 0; j < size; j++) {
  888. var item = matrix[i][j];
  889. if (directed) {
  890. graph.nodes[i].data.outValue += item;
  891. graph.nodes[j].data.inValue += item;
  892. }
  893. graph.nodes[i].data.value += item;
  894. graph.nodes[j].data.value += item;
  895. }
  896. }
  897. for (var i = 0; i < size; i++) {
  898. for (var j = i; j < size; j++) {
  899. var item = matrix[i][j];
  900. if (item === 0) {
  901. continue;
  902. }
  903. var n1 = graph.nodes[i];
  904. var n2 = graph.nodes[j];
  905. var edge = graph.addEdge(n1, n2, {});
  906. edge.data.weight = item;
  907. if (i !== j) {
  908. if (directed && matrix[j][i]) {
  909. var inEdge = graph.addEdge(n2, n1, {});
  910. inEdge.data.weight = matrix[j][i];
  911. }
  912. }
  913. }
  914. }
  915. return graph;
  916. };
  917. return Graph;
  918. });define('echarts/layout/Force', [
  919. 'require',
  920. './forceLayoutWorker',
  921. 'zrender/tool/vector'
  922. ], function (require) {
  923. var ForceLayoutWorker = require('./forceLayoutWorker');
  924. var vec2 = require('zrender/tool/vector');
  925. var requestAnimationFrame = window.requestAnimationFrame || window.msRequestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || function (func) {
  926. setTimeout(func, 16);
  927. };
  928. var ArrayCtor = typeof Float32Array == 'undefined' ? Array : Float32Array;
  929. var workerUrl;
  930. function createWorkerUrl() {
  931. if (typeof Worker !== 'undefined' && typeof Blob !== 'undefined') {
  932. try {
  933. var blob = new Blob([ForceLayoutWorker.getWorkerCode()]);
  934. workerUrl = window.URL.createObjectURL(blob);
  935. } catch (e) {
  936. workerUrl = '';
  937. }
  938. }
  939. return workerUrl;
  940. }
  941. var ForceLayout = function (opts) {
  942. if (typeof workerUrl === 'undefined') {
  943. createWorkerUrl();
  944. }
  945. opts = opts || {};
  946. this.width = opts.width || 500;
  947. this.height = opts.height || 500;
  948. this.center = opts.center || [
  949. this.width / 2,
  950. this.height / 2
  951. ];
  952. this.ratioScaling = opts.ratioScaling || false;
  953. this.scaling = opts.scaling || 1;
  954. this.gravity = typeof opts.gravity !== 'undefined' ? opts.gravity : 1;
  955. this.large = opts.large || false;
  956. this.preventNodeOverlap = opts.preventNodeOverlap || false;
  957. this.preventNodeEdgeOverlap = opts.preventNodeEdgeOverlap || false;
  958. this.maxSpeedIncrease = opts.maxSpeedIncrease || 1;
  959. this.onupdate = opts.onupdate || function () {
  960. };
  961. this.temperature = opts.temperature || 1;
  962. this.coolDown = opts.coolDown || 0.99;
  963. this._layout = null;
  964. this._layoutWorker = null;
  965. var self = this;
  966. var _$onupdate = this._$onupdate;
  967. this._$onupdate = function (e) {
  968. _$onupdate.call(self, e);
  969. };
  970. };
  971. ForceLayout.prototype.updateConfig = function () {
  972. var width = this.width;
  973. var height = this.height;
  974. var size = Math.min(width, height);
  975. var config = {
  976. center: this.center,
  977. width: this.ratioScaling ? width : size,
  978. height: this.ratioScaling ? height : size,
  979. scaling: this.scaling || 1,
  980. gravity: this.gravity || 1,
  981. barnesHutOptimize: this.large,
  982. preventNodeOverlap: this.preventNodeOverlap,
  983. preventNodeEdgeOverlap: this.preventNodeEdgeOverlap,
  984. maxSpeedIncrease: this.maxSpeedIncrease
  985. };
  986. if (this._layoutWorker) {
  987. this._layoutWorker.postMessage({
  988. cmd: 'updateConfig',
  989. config: config
  990. });
  991. } else {
  992. for (var name in config) {
  993. this._layout[name] = config[name];
  994. }
  995. }
  996. };
  997. ForceLayout.prototype.init = function (graph, useWorker) {
  998. if (this._layoutWorker) {
  999. this._layoutWorker.terminate();
  1000. this._layoutWorker = null;
  1001. }
  1002. if (workerUrl && useWorker) {
  1003. try {
  1004. if (!this._layoutWorker) {
  1005. this._layoutWorker = new Worker(workerUrl);
  1006. this._layoutWorker.onmessage = this._$onupdate;
  1007. }
  1008. this._layout = null;
  1009. } catch (e) {
  1010. this._layoutWorker = null;
  1011. if (!this._layout) {
  1012. this._layout = new ForceLayoutWorker();
  1013. }
  1014. }
  1015. } else {
  1016. if (!this._layout) {
  1017. this._layout = new ForceLayoutWorker();
  1018. }
  1019. }
  1020. this.temperature = 1;
  1021. this.graph = graph;
  1022. var len = graph.nodes.length;
  1023. var positionArr = new ArrayCtor(len * 2);
  1024. var massArr = new ArrayCtor(len);
  1025. var sizeArr = new ArrayCtor(len);
  1026. for (var i = 0; i < len; i++) {
  1027. var n = graph.nodes[i];
  1028. positionArr[i * 2] = n.layout.position[0];
  1029. positionArr[i * 2 + 1] = n.layout.position[1];
  1030. massArr[i] = typeof n.layout.mass === 'undefined' ? 1 : n.layout.mass;
  1031. sizeArr[i] = typeof n.layout.size === 'undefined' ? 1 : n.layout.size;
  1032. n.layout.__index = i;
  1033. }
  1034. len = graph.edges.length;
  1035. var edgeArr = new ArrayCtor(len * 2);
  1036. var edgeWeightArr = new ArrayCtor(len);
  1037. for (var i = 0; i < len; i++) {
  1038. var edge = graph.edges[i];
  1039. edgeArr[i * 2] = edge.node1.layout.__index;
  1040. edgeArr[i * 2 + 1] = edge.node2.layout.__index;
  1041. edgeWeightArr[i] = edge.layout.weight || 1;
  1042. }
  1043. if (this._layoutWorker) {
  1044. this._layoutWorker.postMessage({
  1045. cmd: 'init',
  1046. nodesPosition: positionArr,
  1047. nodesMass: massArr,
  1048. nodesSize: sizeArr,
  1049. edges: edgeArr,
  1050. edgesWeight: edgeWeightArr
  1051. });
  1052. } else {
  1053. this._layout.initNodes(positionArr, massArr, sizeArr);
  1054. this._layout.initEdges(edgeArr, edgeWeightArr);
  1055. }
  1056. this.updateConfig();
  1057. };
  1058. ForceLayout.prototype.step = function (steps) {
  1059. var nodes = this.graph.nodes;
  1060. if (this._layoutWorker) {
  1061. var positionArr = new ArrayCtor(nodes.length * 2);
  1062. for (var i = 0; i < nodes.length; i++) {
  1063. var n = nodes[i];
  1064. positionArr[i * 2] = n.layout.position[0];
  1065. positionArr[i * 2 + 1] = n.layout.position[1];
  1066. }
  1067. this._layoutWorker.postMessage(positionArr.buffer, [positionArr.buffer]);
  1068. this._layoutWorker.postMessage({
  1069. cmd: 'update',
  1070. steps: steps,
  1071. temperature: this.temperature,
  1072. coolDown: this.coolDown
  1073. });
  1074. for (var i = 0; i < steps; i++) {
  1075. this.temperature *= this.coolDown;
  1076. }
  1077. } else {
  1078. requestAnimationFrame(this._$onupdate);
  1079. for (var i = 0; i < nodes.length; i++) {
  1080. var n = nodes[i];
  1081. vec2.copy(this._layout.nodes[i].position, n.layout.position);
  1082. }
  1083. for (var i = 0; i < steps; i++) {
  1084. this._layout.temperature = this.temperature;
  1085. this._layout.update();
  1086. this.temperature *= this.coolDown;
  1087. }
  1088. }
  1089. };
  1090. ForceLayout.prototype._$onupdate = function (e) {
  1091. if (this._layoutWorker) {
  1092. var positionArr = new Float32Array(e.data);
  1093. for (var i = 0; i < this.graph.nodes.length; i++) {
  1094. var n = this.graph.nodes[i];
  1095. n.layout.position[0] = positionArr[i * 2];
  1096. n.layout.position[1] = positionArr[i * 2 + 1];
  1097. }
  1098. this.onupdate && this.onupdate();
  1099. } else if (this._layout) {
  1100. for (var i = 0; i < this.graph.nodes.length; i++) {
  1101. var n = this.graph.nodes[i];
  1102. vec2.copy(n.layout.position, this._layout.nodes[i].position);
  1103. }
  1104. this.onupdate && this.onupdate();
  1105. }
  1106. };
  1107. ForceLayout.prototype.dispose = function () {
  1108. if (this._layoutWorker) {
  1109. this._layoutWorker.terminate();
  1110. }
  1111. this._layoutWorker = null;
  1112. this._layout = null;
  1113. };
  1114. return ForceLayout;
  1115. });define('echarts/layout/forceLayoutWorker', [
  1116. 'require',
  1117. 'zrender/tool/vector'
  1118. ], function __echartsForceLayoutWorker(require) {
  1119. 'use strict';
  1120. var vec2;
  1121. var inWorker = typeof window === 'undefined' && typeof require === 'undefined';
  1122. if (inWorker) {
  1123. vec2 = {
  1124. create: function (x, y) {
  1125. var out = new Float32Array(2);
  1126. out[0] = x || 0;
  1127. out[1] = y || 0;
  1128. return out;
  1129. },
  1130. dist: function (a, b) {
  1131. var x = b[0] - a[0];
  1132. var y = b[1] - a[1];
  1133. return Math.sqrt(x * x + y * y);
  1134. },
  1135. len: function (a) {
  1136. var x = a[0];
  1137. var y = a[1];
  1138. return Math.sqrt(x * x + y * y);
  1139. },
  1140. scaleAndAdd: function (out, a, b, scale) {
  1141. out[0] = a[0] + b[0] * scale;
  1142. out[1] = a[1] + b[1] * scale;
  1143. return out;
  1144. },
  1145. scale: function (out, a, b) {
  1146. out[0] = a[0] * b;
  1147. out[1] = a[1] * b;
  1148. return out;
  1149. },
  1150. add: function (out, a, b) {
  1151. out[0] = a[0] + b[0];
  1152. out[1] = a[1] + b[1];
  1153. return out;
  1154. },
  1155. sub: function (out, a, b) {
  1156. out[0] = a[0] - b[0];
  1157. out[1] = a[1] - b[1];
  1158. return out;
  1159. },
  1160. dot: function (v1, v2) {
  1161. return v1[0] * v2[0] + v1[1] * v2[1];
  1162. },
  1163. normalize: function (out, a) {
  1164. var x = a[0];
  1165. var y = a[1];
  1166. var len = x * x + y * y;
  1167. if (len > 0) {
  1168. len = 1 / Math.sqrt(len);
  1169. out[0] = a[0] * len;
  1170. out[1] = a[1] * len;
  1171. }
  1172. return out;
  1173. },
  1174. negate: function (out, a) {
  1175. out[0] = -a[0];
  1176. out[1] = -a[1];
  1177. return out;
  1178. },
  1179. copy: function (out, a) {
  1180. out[0] = a[0];
  1181. out[1] = a[1];
  1182. return out;
  1183. },
  1184. set: function (out, x, y) {
  1185. out[0] = x;
  1186. out[1] = y;
  1187. return out;
  1188. }
  1189. };
  1190. } else {
  1191. vec2 = require('zrender/tool/vector');
  1192. }
  1193. var ArrayCtor = typeof Float32Array == 'undefined' ? Array : Float32Array;
  1194. function Region() {
  1195. this.subRegions = [];
  1196. this.nSubRegions = 0;
  1197. this.node = null;
  1198. this.mass = 0;
  1199. this.centerOfMass = null;
  1200. this.bbox = new ArrayCtor(4);
  1201. this.size = 0;
  1202. }
  1203. Region.prototype.beforeUpdate = function () {
  1204. for (var i = 0; i < this.nSubRegions; i++) {
  1205. this.subRegions[i].beforeUpdate();
  1206. }
  1207. this.mass = 0;
  1208. if (this.centerOfMass) {
  1209. this.centerOfMass[0] = 0;
  1210. this.centerOfMass[1] = 0;
  1211. }
  1212. this.nSubRegions = 0;
  1213. this.node = null;
  1214. };
  1215. Region.prototype.afterUpdate = function () {
  1216. this.subRegions.length = this.nSubRegions;
  1217. for (var i = 0; i < this.nSubRegions; i++) {
  1218. this.subRegions[i].afterUpdate();
  1219. }
  1220. };
  1221. Region.prototype.addNode = function (node) {
  1222. if (this.nSubRegions === 0) {
  1223. if (this.node == null) {
  1224. this.node = node;
  1225. return;
  1226. } else {
  1227. this._addNodeToSubRegion(this.node);
  1228. this.node = null;
  1229. }
  1230. }
  1231. this._addNodeToSubRegion(node);
  1232. this._updateCenterOfMass(node);
  1233. };
  1234. Region.prototype.findSubRegion = function (x, y) {
  1235. for (var i = 0; i < this.nSubRegions; i++) {
  1236. var region = this.subRegions[i];
  1237. if (region.contain(x, y)) {
  1238. return region;
  1239. }
  1240. }
  1241. };
  1242. Region.prototype.contain = function (x, y) {
  1243. return this.bbox[0] <= x && this.bbox[2] >= x && this.bbox[1] <= y && this.bbox[3] >= y;
  1244. };
  1245. Region.prototype.setBBox = function (minX, minY, maxX, maxY) {
  1246. this.bbox[0] = minX;
  1247. this.bbox[1] = minY;
  1248. this.bbox[2] = maxX;
  1249. this.bbox[3] = maxY;
  1250. this.size = (maxX - minX + maxY - minY) / 2;
  1251. };
  1252. Region.prototype._newSubRegion = function () {
  1253. var subRegion = this.subRegions[this.nSubRegions];
  1254. if (!subRegion) {
  1255. subRegion = new Region();
  1256. this.subRegions[this.nSubRegions] = subRegion;
  1257. }
  1258. this.nSubRegions++;
  1259. return subRegion;
  1260. };
  1261. Region.prototype._addNodeToSubRegion = function (node) {
  1262. var subRegion = this.findSubRegion(node.position[0], node.position[1]);
  1263. var bbox = this.bbox;
  1264. if (!subRegion) {
  1265. var cx = (bbox[0] + bbox[2]) / 2;
  1266. var cy = (bbox[1] + bbox[3]) / 2;
  1267. var w = (bbox[2] - bbox[0]) / 2;
  1268. var h = (bbox[3] - bbox[1]) / 2;
  1269. var xi = node.position[0] >= cx ? 1 : 0;
  1270. var yi = node.position[1] >= cy ? 1 : 0;
  1271. var subRegion = this._newSubRegion();
  1272. subRegion.setBBox(xi * w + bbox[0], yi * h + bbox[1], (xi + 1) * w + bbox[0], (yi + 1) * h + bbox[1]);
  1273. }
  1274. subRegion.addNode(node);
  1275. };
  1276. Region.prototype._updateCenterOfMass = function (node) {
  1277. if (this.centerOfMass == null) {
  1278. this.centerOfMass = vec2.create();
  1279. }
  1280. var x = this.centerOfMass[0] * this.mass;
  1281. var y = this.centerOfMass[1] * this.mass;
  1282. x += node.position[0] * node.mass;
  1283. y += node.position[1] * node.mass;
  1284. this.mass += node.mass;
  1285. this.centerOfMass[0] = x / this.mass;
  1286. this.centerOfMass[1] = y / this.mass;
  1287. };
  1288. function GraphNode() {
  1289. this.position = vec2.create();
  1290. this.force = vec2.create();
  1291. this.forcePrev = vec2.create();
  1292. this.speed = vec2.create();
  1293. this.speedPrev = vec2.create();
  1294. this.mass = 1;
  1295. this.inDegree = 0;
  1296. this.outDegree = 0;
  1297. }
  1298. function GraphEdge(node1, node2) {
  1299. this.node1 = node1;
  1300. this.node2 = node2;
  1301. this.weight = 1;
  1302. }
  1303. function ForceLayout() {
  1304. this.barnesHutOptimize = false;
  1305. this.barnesHutTheta = 1.5;
  1306. this.repulsionByDegree = false;
  1307. this.preventNodeOverlap = false;
  1308. this.preventNodeEdgeOverlap = false;
  1309. this.strongGravity = true;
  1310. this.gravity = 1;
  1311. this.scaling = 1;
  1312. this.edgeWeightInfluence = 1;
  1313. this.center = [
  1314. 0,
  1315. 0
  1316. ];
  1317. this.width = 500;
  1318. this.height = 500;
  1319. this.maxSpeedIncrease = 1;
  1320. this.nodes = [];
  1321. this.edges = [];
  1322. this.bbox = new ArrayCtor(4);
  1323. this._rootRegion = new Region();
  1324. this._rootRegion.centerOfMass = vec2.create();
  1325. this._massArr = null;
  1326. this._k = 0;
  1327. }
  1328. ForceLayout.prototype.nodeToNodeRepulsionFactor = function (mass, d, k) {
  1329. return k * k * mass / d;
  1330. };
  1331. ForceLayout.prototype.edgeToNodeRepulsionFactor = function (mass, d, k) {
  1332. return k * mass / d;
  1333. };
  1334. ForceLayout.prototype.attractionFactor = function (w, d, k) {
  1335. return w * d / k;
  1336. };
  1337. ForceLayout.prototype.initNodes = function (positionArr, massArr, sizeArr) {
  1338. this.temperature = 1;
  1339. var nNodes = positionArr.length / 2;
  1340. this.nodes.length = 0;
  1341. var haveSize = typeof sizeArr !== 'undefined';
  1342. for (var i = 0; i < nNodes; i++) {
  1343. var node = new GraphNode();
  1344. node.position[0] = positionArr[i * 2];
  1345. node.position[1] = positionArr[i * 2 + 1];
  1346. node.mass = massArr[i];
  1347. if (haveSize) {
  1348. node.size = sizeArr[i];
  1349. }
  1350. this.nodes.push(node);
  1351. }
  1352. this._massArr = massArr;
  1353. if (haveSize) {
  1354. this._sizeArr = sizeArr;
  1355. }
  1356. };
  1357. ForceLayout.prototype.initEdges = function (edgeArr, edgeWeightArr) {
  1358. var nEdges = edgeArr.length / 2;
  1359. this.edges.length = 0;
  1360. var edgeHaveWeight = typeof edgeWeightArr !== 'undefined';
  1361. for (var i = 0; i < nEdges; i++) {
  1362. var sIdx = edgeArr[i * 2];
  1363. var tIdx = edgeArr[i * 2 + 1];
  1364. var sNode = this.nodes[sIdx];
  1365. var tNode = this.nodes[tIdx];
  1366. if (!sNode || !tNode) {
  1367. continue;
  1368. }
  1369. sNode.outDegree++;
  1370. tNode.inDegree++;
  1371. var edge = new GraphEdge(sNode, tNode);
  1372. if (edgeHaveWeight) {
  1373. edge.weight = edgeWeightArr[i];
  1374. }
  1375. this.edges.push(edge);
  1376. }
  1377. };
  1378. ForceLayout.prototype.update = function () {
  1379. var nNodes = this.nodes.length;
  1380. this.updateBBox();
  1381. this._k = 0.4 * this.scaling * Math.sqrt(this.width * this.height / nNodes);
  1382. if (this.barnesHutOptimize) {
  1383. this._rootRegion.setBBox(this.bbox[0], this.bbox[1], this.bbox[2], this.bbox[3]);
  1384. this._rootRegion.beforeUpdate();
  1385. for (var i = 0; i < nNodes; i++) {
  1386. this._rootRegion.addNode(this.nodes[i]);
  1387. }
  1388. this._rootRegion.afterUpdate();
  1389. } else {
  1390. var mass = 0;
  1391. var centerOfMass = this._rootRegion.centerOfMass;
  1392. vec2.set(centerOfMass, 0, 0);
  1393. for (var i = 0; i < nNodes; i++) {
  1394. var node = this.nodes[i];
  1395. mass += node.mass;
  1396. vec2.scaleAndAdd(centerOfMass, centerOfMass, node.position, node.mass);
  1397. }
  1398. if (mass > 0) {
  1399. vec2.scale(centerOfMass, centerOfMass, 1 / mass);
  1400. }
  1401. }
  1402. this.updateForce();
  1403. this.updatePosition();
  1404. };
  1405. ForceLayout.prototype.updateForce = function () {
  1406. var nNodes = this.nodes.length;
  1407. for (var i = 0; i < nNodes; i++) {
  1408. var node = this.nodes[i];
  1409. vec2.copy(node.forcePrev, node.force);
  1410. vec2.copy(node.speedPrev, node.speed);
  1411. vec2.set(node.force, 0, 0);
  1412. }
  1413. this.updateNodeNodeForce();
  1414. if (this.gravity > 0) {
  1415. this.updateGravityForce();
  1416. }
  1417. this.updateEdgeForce();
  1418. if (this.preventNodeEdgeOverlap) {
  1419. this.updateNodeEdgeForce();
  1420. }
  1421. };
  1422. ForceLayout.prototype.updatePosition = function () {
  1423. var nNodes = this.nodes.length;
  1424. var v = vec2.create();
  1425. for (var i = 0; i < nNodes; i++) {
  1426. var node = this.nodes[i];
  1427. var speed = node.speed;
  1428. vec2.scale(node.force, node.force, 1 / 30);
  1429. var df = vec2.len(node.force) + 0.1;
  1430. var scale = Math.min(df, 500) / df;
  1431. vec2.scale(node.force, node.force, scale);
  1432. vec2.add(speed, speed, node.force);
  1433. vec2.scale(speed, speed, this.temperature);
  1434. vec2.sub(v, speed, node.speedPrev);
  1435. var swing = vec2.len(v);
  1436. if (swing > 0) {
  1437. vec2.scale(v, v, 1 / swing);
  1438. var base = vec2.len(node.speedPrev);
  1439. if (base > 0) {
  1440. swing = Math.min(swing / base, this.maxSpeedIncrease) * base;
  1441. vec2.scaleAndAdd(speed, node.speedPrev, v, swing);
  1442. }
  1443. }
  1444. var ds = vec2.len(speed);
  1445. var scale = Math.min(ds, 100) / (ds + 0.1);
  1446. vec2.scale(speed, speed, scale);
  1447. vec2.add(node.position, node.position, speed);
  1448. }
  1449. };
  1450. ForceLayout.prototype.updateNodeNodeForce = function () {
  1451. var nNodes = this.nodes.length;
  1452. for (var i = 0; i < nNodes; i++) {
  1453. var na = this.nodes[i];
  1454. if (this.barnesHutOptimize) {
  1455. this.applyRegionToNodeRepulsion(this._rootRegion, na);
  1456. } else {
  1457. for (var j = i + 1; j < nNodes; j++) {
  1458. var nb = this.nodes[j];
  1459. this.applyNodeToNodeRepulsion(na, nb, false);
  1460. }
  1461. }
  1462. }
  1463. };
  1464. ForceLayout.prototype.updateGravityForce = function () {
  1465. for (var i = 0; i < this.nodes.length; i++) {
  1466. this.applyNodeGravity(this.nodes[i]);
  1467. }
  1468. };
  1469. ForceLayout.prototype.updateEdgeForce = function () {
  1470. for (var i = 0; i < this.edges.length; i++) {
  1471. this.applyEdgeAttraction(this.edges[i]);
  1472. }
  1473. };
  1474. ForceLayout.prototype.updateNodeEdgeForce = function () {
  1475. for (var i = 0; i < this.nodes.length; i++) {
  1476. for (var j = 0; j < this.edges.length; j++) {
  1477. this.applyEdgeToNodeRepulsion(this.edges[j], this.nodes[i]);
  1478. }
  1479. }
  1480. };
  1481. ForceLayout.prototype.applyRegionToNodeRepulsion = function () {
  1482. var v = vec2.create();
  1483. return function applyRegionToNodeRepulsion(region, node) {
  1484. if (region.node) {
  1485. this.applyNodeToNodeRepulsion(region.node, node, true);
  1486. } else {
  1487. if (region.mass === 0 && node.mass === 0) {
  1488. return;
  1489. }
  1490. vec2.sub(v, node.position, region.centerOfMass);
  1491. var d2 = v[0] * v[0] + v[1] * v[1];
  1492. if (d2 > this.barnesHutTheta * region.size * region.size) {
  1493. var factor = this._k * this._k * (node.mass + region.mass) / (d2 + 1);
  1494. vec2.scaleAndAdd(node.force, node.force, v, factor * 2);
  1495. } else {
  1496. for (var i = 0; i < region.nSubRegions; i++) {
  1497. this.applyRegionToNodeRepulsion(region.subRegions[i], node);
  1498. }
  1499. }
  1500. }
  1501. };
  1502. }();
  1503. ForceLayout.prototype.applyNodeToNodeRepulsion = function () {
  1504. var v = vec2.create();
  1505. return function applyNodeToNodeRepulsion(na, nb, oneWay) {
  1506. if (na === nb) {
  1507. return;
  1508. }
  1509. if (na.mass === 0 && nb.mass === 0) {
  1510. return;
  1511. }
  1512. vec2.sub(v, na.position, nb.position);
  1513. var d2 = v[0] * v[0] + v[1] * v[1];
  1514. if (d2 === 0) {
  1515. return;
  1516. }
  1517. var factor;
  1518. var mass = na.mass + nb.mass;
  1519. var d = Math.sqrt(d2);
  1520. vec2.scale(v, v, 1 / d);
  1521. if (this.preventNodeOverlap) {
  1522. d = d - na.size - nb.size;
  1523. if (d > 0) {
  1524. factor = this.nodeToNodeRepulsionFactor(mass, d, this._k);
  1525. } else if (d <= 0) {
  1526. factor = this._k * this._k * 10 * mass;
  1527. }
  1528. } else {
  1529. factor = this.nodeToNodeRepulsionFactor(mass, d, this._k);
  1530. }
  1531. if (!oneWay) {
  1532. vec2.scaleAndAdd(na.force, na.force, v, factor * 2);
  1533. }
  1534. vec2.scaleAndAdd(nb.force, nb.force, v, -factor * 2);
  1535. };
  1536. }();
  1537. ForceLayout.prototype.applyEdgeAttraction = function () {
  1538. var v = vec2.create();
  1539. return function applyEdgeAttraction(edge) {
  1540. var na = edge.node1;
  1541. var nb = edge.node2;
  1542. vec2.sub(v, na.position, nb.position);
  1543. var d = vec2.len(v);
  1544. var w;
  1545. if (this.edgeWeightInfluence === 0) {
  1546. w = 1;
  1547. } else if (this.edgeWeightInfluence == 1) {
  1548. w = edge.weight;
  1549. } else {
  1550. w = Math.pow(edge.weight, this.edgeWeightInfluence);
  1551. }
  1552. var factor;
  1553. if (this.preventOverlap) {
  1554. d = d - na.size - nb.size;
  1555. if (d <= 0) {
  1556. return;
  1557. }
  1558. }
  1559. var factor = this.attractionFactor(w, d, this._k);
  1560. vec2.scaleAndAdd(na.force, na.force, v, -factor);
  1561. vec2.scaleAndAdd(nb.force, nb.force, v, factor);
  1562. };
  1563. }();
  1564. ForceLayout.prototype.applyNodeGravity = function () {
  1565. var v = vec2.create();
  1566. return function (node) {
  1567. vec2.sub(v, this.center, node.position);
  1568. if (this.width > this.height) {
  1569. v[1] *= this.width / this.height;
  1570. } else {
  1571. v[0] *= this.height / this.width;
  1572. }
  1573. var d = vec2.len(v) / 100;
  1574. if (this.strongGravity) {
  1575. vec2.scaleAndAdd(node.force, node.force, v, d * this.gravity * node.mass);
  1576. } else {
  1577. vec2.scaleAndAdd(node.force, node.force, v, this.gravity * node.mass / (d + 1));
  1578. }
  1579. };
  1580. }();
  1581. ForceLayout.prototype.applyEdgeToNodeRepulsion = function () {
  1582. var v12 = vec2.create();
  1583. var v13 = vec2.create();
  1584. var p = vec2.create();
  1585. return function (e, n3) {
  1586. var n1 = e.node1;
  1587. var n2 = e.node2;
  1588. if (n1 === n3 || n2 === n3) {
  1589. return;
  1590. }
  1591. vec2.sub(v12, n2.position, n1.position);
  1592. vec2.sub(v13, n3.position, n1.position);
  1593. var len12 = vec2.len(v12);
  1594. vec2.scale(v12, v12, 1 / len12);
  1595. var len = vec2.dot(v12, v13);
  1596. if (len < 0 || len > len12) {
  1597. return;
  1598. }
  1599. vec2.scaleAndAdd(p, n1.position, v12, len);
  1600. var dist = vec2.dist(p, n3.position) - n3.size;
  1601. var factor = this.edgeToNodeRepulsionFactor(n3.mass, Math.max(dist, 0.1), 100);
  1602. vec2.sub(v12, n3.position, p);
  1603. vec2.normalize(v12, v12);
  1604. vec2.scaleAndAdd(n3.force, n3.force, v12, factor);
  1605. vec2.scaleAndAdd(n1.force, n1.force, v12, -factor);
  1606. vec2.scaleAndAdd(n2.force, n2.force, v12, -factor);
  1607. };
  1608. }();
  1609. ForceLayout.prototype.updateBBox = function () {
  1610. var minX = Infinity;
  1611. var minY = Infinity;
  1612. var maxX = -Infinity;
  1613. var maxY = -Infinity;
  1614. for (var i = 0; i < this.nodes.length; i++) {
  1615. var pos = this.nodes[i].position;
  1616. minX = Math.min(minX, pos[0]);
  1617. minY = Math.min(minY, pos[1]);
  1618. maxX = Math.max(maxX, pos[0]);
  1619. maxY = Math.max(maxY, pos[1]);
  1620. }
  1621. this.bbox[0] = minX;
  1622. this.bbox[1] = minY;
  1623. this.bbox[2] = maxX;
  1624. this.bbox[3] = maxY;
  1625. };
  1626. ForceLayout.getWorkerCode = function () {
  1627. var str = __echartsForceLayoutWorker.toString();
  1628. return str.slice(str.indexOf('{') + 1, str.lastIndexOf('return'));
  1629. };
  1630. if (inWorker) {
  1631. var forceLayout = null;
  1632. self.onmessage = function (e) {
  1633. if (e.data instanceof ArrayBuffer) {
  1634. if (!forceLayout)
  1635. return;
  1636. var positionArr = new Float32Array(e.data);
  1637. var nNodes = positionArr.length / 2;
  1638. for (var i = 0; i < nNodes; i++) {
  1639. var node = forceLayout.nodes[i];
  1640. node.position[0] = positionArr[i * 2];
  1641. node.position[1] = positionArr[i * 2 + 1];
  1642. }
  1643. return;
  1644. }
  1645. switch (e.data.cmd) {
  1646. case 'init':
  1647. if (!forceLayout) {
  1648. forceLayout = new ForceLayout();
  1649. }
  1650. forceLayout.initNodes(e.data.nodesPosition, e.data.nodesMass, e.data.nodesSize);
  1651. forceLayout.initEdges(e.data.edges, e.data.edgesWeight);
  1652. break;
  1653. case 'updateConfig':
  1654. if (forceLayout) {
  1655. for (var name in e.data.config) {
  1656. forceLayout[name] = e.data.config[name];
  1657. }
  1658. }
  1659. break;
  1660. case 'update':
  1661. var steps = e.data.steps;
  1662. if (forceLayout) {
  1663. var nNodes = forceLayout.nodes.length;
  1664. var positionArr = new Float32Array(nNodes * 2);
  1665. forceLayout.temperature = e.data.temperature;
  1666. for (var i = 0; i < steps; i++) {
  1667. forceLayout.update();
  1668. forceLayout.temperature *= e.data.coolDown;
  1669. }
  1670. for (var i = 0; i < nNodes; i++) {
  1671. var node = forceLayout.nodes[i];
  1672. positionArr[i * 2] = node.position[0];
  1673. positionArr[i * 2 + 1] = node.position[1];
  1674. }
  1675. self.postMessage(positionArr.buffer, [positionArr.buffer]);
  1676. } else {
  1677. var emptyArr = new Float32Array();
  1678. self.postMessage(emptyArr.buffer, [emptyArr.buffer]);
  1679. }
  1680. break;
  1681. }
  1682. };
  1683. }
  1684. return ForceLayout;
  1685. });