GraphView.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. var echarts = require("../../echarts");
  2. var zrUtil = require("zrender/lib/core/util");
  3. var SymbolDraw = require("../helper/SymbolDraw");
  4. var LineDraw = require("../helper/LineDraw");
  5. var RoamController = require("../../component/helper/RoamController");
  6. var roamHelper = require("../../component/helper/roamHelper");
  7. var _cursorHelper = require("../../component/helper/cursorHelper");
  8. var onIrrelevantElement = _cursorHelper.onIrrelevantElement;
  9. var graphic = require("../../util/graphic");
  10. var adjustEdge = require("./adjustEdge");
  11. var nodeOpacityPath = ['itemStyle', 'normal', 'opacity'];
  12. var lineOpacityPath = ['lineStyle', 'normal', 'opacity'];
  13. function getItemOpacity(item, opacityPath) {
  14. return item.getVisual('opacity') || item.getModel().get(opacityPath);
  15. }
  16. function fadeOutItem(item, opacityPath, opacityRatio) {
  17. var el = item.getGraphicEl();
  18. var opacity = getItemOpacity(item, opacityPath);
  19. if (opacityRatio != null) {
  20. opacity == null && (opacity = 1);
  21. opacity *= opacityRatio;
  22. }
  23. el.downplay && el.downplay();
  24. el.traverse(function (child) {
  25. if (child.type !== 'group') {
  26. child.setStyle('opacity', opacity);
  27. }
  28. });
  29. }
  30. function fadeInItem(item, opacityPath) {
  31. var opacity = getItemOpacity(item, opacityPath);
  32. var el = item.getGraphicEl();
  33. el.highlight && el.highlight();
  34. el.traverse(function (child) {
  35. if (child.type !== 'group') {
  36. child.setStyle('opacity', opacity);
  37. }
  38. });
  39. }
  40. var _default = echarts.extendChartView({
  41. type: 'graph',
  42. init: function (ecModel, api) {
  43. var symbolDraw = new SymbolDraw();
  44. var lineDraw = new LineDraw();
  45. var group = this.group;
  46. this._controller = new RoamController(api.getZr());
  47. this._controllerHost = {
  48. target: group
  49. };
  50. group.add(symbolDraw.group);
  51. group.add(lineDraw.group);
  52. this._symbolDraw = symbolDraw;
  53. this._lineDraw = lineDraw;
  54. this._firstRender = true;
  55. },
  56. render: function (seriesModel, ecModel, api) {
  57. var coordSys = seriesModel.coordinateSystem;
  58. this._model = seriesModel;
  59. this._nodeScaleRatio = seriesModel.get('nodeScaleRatio');
  60. var symbolDraw = this._symbolDraw;
  61. var lineDraw = this._lineDraw;
  62. var group = this.group;
  63. if (coordSys.type === 'view') {
  64. var groupNewProp = {
  65. position: coordSys.position,
  66. scale: coordSys.scale
  67. };
  68. if (this._firstRender) {
  69. group.attr(groupNewProp);
  70. } else {
  71. graphic.updateProps(group, groupNewProp, seriesModel);
  72. }
  73. } // Fix edge contact point with node
  74. adjustEdge(seriesModel.getGraph(), this._getNodeGlobalScale(seriesModel));
  75. var data = seriesModel.getData();
  76. symbolDraw.updateData(data);
  77. var edgeData = seriesModel.getEdgeData();
  78. lineDraw.updateData(edgeData);
  79. this._updateNodeAndLinkScale();
  80. this._updateController(seriesModel, ecModel, api);
  81. clearTimeout(this._layoutTimeout);
  82. var forceLayout = seriesModel.forceLayout;
  83. var layoutAnimation = seriesModel.get('force.layoutAnimation');
  84. if (forceLayout) {
  85. this._startForceLayoutIteration(forceLayout, layoutAnimation);
  86. }
  87. data.eachItemGraphicEl(function (el, idx) {
  88. var itemModel = data.getItemModel(idx); // Update draggable
  89. el.off('drag').off('dragend');
  90. var draggable = data.getItemModel(idx).get('draggable');
  91. if (draggable) {
  92. el.on('drag', function () {
  93. if (forceLayout) {
  94. forceLayout.warmUp();
  95. !this._layouting && this._startForceLayoutIteration(forceLayout, layoutAnimation);
  96. forceLayout.setFixed(idx); // Write position back to layout
  97. data.setItemLayout(idx, el.position);
  98. }
  99. }, this).on('dragend', function () {
  100. if (forceLayout) {
  101. forceLayout.setUnfixed(idx);
  102. }
  103. }, this);
  104. }
  105. el.setDraggable(draggable && forceLayout);
  106. el.off('mouseover', el.__focusNodeAdjacency);
  107. el.off('mouseout', el.__unfocusNodeAdjacency);
  108. if (itemModel.get('focusNodeAdjacency')) {
  109. el.on('mouseover', el.__focusNodeAdjacency = function () {
  110. api.dispatchAction({
  111. type: 'focusNodeAdjacency',
  112. seriesId: seriesModel.id,
  113. dataIndex: el.dataIndex
  114. });
  115. });
  116. el.on('mouseout', el.__unfocusNodeAdjacency = function () {
  117. api.dispatchAction({
  118. type: 'unfocusNodeAdjacency',
  119. seriesId: seriesModel.id
  120. });
  121. });
  122. }
  123. }, this);
  124. data.graph.eachEdge(function (edge) {
  125. var el = edge.getGraphicEl();
  126. el.off('mouseover', el.__focusNodeAdjacency);
  127. el.off('mouseout', el.__unfocusNodeAdjacency);
  128. if (edge.getModel().get('focusNodeAdjacency')) {
  129. el.on('mouseover', el.__focusNodeAdjacency = function () {
  130. api.dispatchAction({
  131. type: 'focusNodeAdjacency',
  132. seriesId: seriesModel.id,
  133. edgeDataIndex: edge.dataIndex
  134. });
  135. });
  136. el.on('mouseout', el.__unfocusNodeAdjacency = function () {
  137. api.dispatchAction({
  138. type: 'unfocusNodeAdjacency',
  139. seriesId: seriesModel.id
  140. });
  141. });
  142. }
  143. });
  144. var circularRotateLabel = seriesModel.get('layout') === 'circular' && seriesModel.get('circular.rotateLabel');
  145. var cx = data.getLayout('cx');
  146. var cy = data.getLayout('cy');
  147. data.eachItemGraphicEl(function (el, idx) {
  148. var symbolPath = el.getSymbolPath();
  149. if (circularRotateLabel) {
  150. var pos = data.getItemLayout(idx);
  151. var rad = Math.atan2(pos[1] - cy, pos[0] - cx);
  152. if (rad < 0) {
  153. rad = Math.PI * 2 + rad;
  154. }
  155. var isLeft = pos[0] < cx;
  156. if (isLeft) {
  157. rad = rad - Math.PI;
  158. }
  159. var textPosition = isLeft ? 'left' : 'right';
  160. symbolPath.setStyle({
  161. textRotation: -rad,
  162. textPosition: textPosition,
  163. textOrigin: 'center'
  164. });
  165. symbolPath.hoverStyle && (symbolPath.hoverStyle.textPosition = textPosition);
  166. } else {
  167. symbolPath.setStyle({
  168. textRotation: 0
  169. });
  170. }
  171. });
  172. this._firstRender = false;
  173. },
  174. dispose: function () {
  175. this._controller && this._controller.dispose();
  176. this._controllerHost = {};
  177. },
  178. focusNodeAdjacency: function (seriesModel, ecModel, api, payload) {
  179. var data = this._model.getData();
  180. var graph = data.graph;
  181. var dataIndex = payload.dataIndex;
  182. var edgeDataIndex = payload.edgeDataIndex;
  183. var node = graph.getNodeByIndex(dataIndex);
  184. var edge = graph.getEdgeByIndex(edgeDataIndex);
  185. if (!node && !edge) {
  186. return;
  187. }
  188. graph.eachNode(function (node) {
  189. fadeOutItem(node, nodeOpacityPath, 0.1);
  190. });
  191. graph.eachEdge(function (edge) {
  192. fadeOutItem(edge, lineOpacityPath, 0.1);
  193. });
  194. if (node) {
  195. fadeInItem(node, nodeOpacityPath);
  196. zrUtil.each(node.edges, function (adjacentEdge) {
  197. if (adjacentEdge.dataIndex < 0) {
  198. return;
  199. }
  200. fadeInItem(adjacentEdge, lineOpacityPath);
  201. fadeInItem(adjacentEdge.node1, nodeOpacityPath);
  202. fadeInItem(adjacentEdge.node2, nodeOpacityPath);
  203. });
  204. }
  205. if (edge) {
  206. fadeInItem(edge, lineOpacityPath);
  207. fadeInItem(edge.node1, nodeOpacityPath);
  208. fadeInItem(edge.node2, nodeOpacityPath);
  209. }
  210. },
  211. unfocusNodeAdjacency: function (seriesModel, ecModel, api, payload) {
  212. var graph = this._model.getData().graph;
  213. graph.eachNode(function (node) {
  214. fadeOutItem(node, nodeOpacityPath);
  215. });
  216. graph.eachEdge(function (edge) {
  217. fadeOutItem(edge, lineOpacityPath);
  218. });
  219. },
  220. _startForceLayoutIteration: function (forceLayout, layoutAnimation) {
  221. var self = this;
  222. (function step() {
  223. forceLayout.step(function (stopped) {
  224. self.updateLayout(self._model);
  225. (self._layouting = !stopped) && (layoutAnimation ? self._layoutTimeout = setTimeout(step, 16) : step());
  226. });
  227. })();
  228. },
  229. _updateController: function (seriesModel, ecModel, api) {
  230. var controller = this._controller;
  231. var controllerHost = this._controllerHost;
  232. var group = this.group;
  233. controller.setPointerChecker(function (e, x, y) {
  234. var rect = group.getBoundingRect();
  235. rect.applyTransform(group.transform);
  236. return rect.contain(x, y) && !onIrrelevantElement(e, api, seriesModel);
  237. });
  238. if (seriesModel.coordinateSystem.type !== 'view') {
  239. controller.disable();
  240. return;
  241. }
  242. controller.enable(seriesModel.get('roam'));
  243. controllerHost.zoomLimit = seriesModel.get('scaleLimit');
  244. controllerHost.zoom = seriesModel.coordinateSystem.getZoom();
  245. controller.off('pan').off('zoom').on('pan', function (dx, dy) {
  246. roamHelper.updateViewOnPan(controllerHost, dx, dy);
  247. api.dispatchAction({
  248. seriesId: seriesModel.id,
  249. type: 'graphRoam',
  250. dx: dx,
  251. dy: dy
  252. });
  253. }).on('zoom', function (zoom, mouseX, mouseY) {
  254. roamHelper.updateViewOnZoom(controllerHost, zoom, mouseX, mouseY);
  255. api.dispatchAction({
  256. seriesId: seriesModel.id,
  257. type: 'graphRoam',
  258. zoom: zoom,
  259. originX: mouseX,
  260. originY: mouseY
  261. });
  262. this._updateNodeAndLinkScale();
  263. adjustEdge(seriesModel.getGraph(), this._getNodeGlobalScale(seriesModel));
  264. this._lineDraw.updateLayout();
  265. }, this);
  266. },
  267. _updateNodeAndLinkScale: function () {
  268. var seriesModel = this._model;
  269. var data = seriesModel.getData();
  270. var nodeScale = this._getNodeGlobalScale(seriesModel);
  271. var invScale = [nodeScale, nodeScale];
  272. data.eachItemGraphicEl(function (el, idx) {
  273. el.attr('scale', invScale);
  274. });
  275. },
  276. _getNodeGlobalScale: function (seriesModel) {
  277. var coordSys = seriesModel.coordinateSystem;
  278. if (coordSys.type !== 'view') {
  279. return 1;
  280. }
  281. var nodeScaleRatio = this._nodeScaleRatio;
  282. var groupScale = coordSys.scale;
  283. var groupZoom = groupScale && groupScale[0] || 1; // Scale node when zoom changes
  284. var roamZoom = coordSys.getZoom();
  285. var nodeScale = (roamZoom - 1) * nodeScaleRatio + 1;
  286. return nodeScale / groupZoom;
  287. },
  288. updateLayout: function (seriesModel) {
  289. adjustEdge(seriesModel.getGraph(), this._getNodeGlobalScale(seriesModel));
  290. this._symbolDraw.updateLayout();
  291. this._lineDraw.updateLayout();
  292. },
  293. remove: function (ecModel, api) {
  294. this._symbolDraw && this._symbolDraw.remove();
  295. this._lineDraw && this._lineDraw.remove();
  296. }
  297. });
  298. module.exports = _default;