GraphView.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 echarts = require("../../echarts");
  20. var zrUtil = require("zrender/lib/core/util");
  21. var SymbolDraw = require("../helper/SymbolDraw");
  22. var LineDraw = require("../helper/LineDraw");
  23. var RoamController = require("../../component/helper/RoamController");
  24. var roamHelper = require("../../component/helper/roamHelper");
  25. var _cursorHelper = require("../../component/helper/cursorHelper");
  26. var onIrrelevantElement = _cursorHelper.onIrrelevantElement;
  27. var graphic = require("../../util/graphic");
  28. var adjustEdge = require("./adjustEdge");
  29. var _graphHelper = require("./graphHelper");
  30. var getNodeGlobalScale = _graphHelper.getNodeGlobalScale;
  31. /*
  32. * Licensed to the Apache Software Foundation (ASF) under one
  33. * or more contributor license agreements. See the NOTICE file
  34. * distributed with this work for additional information
  35. * regarding copyright ownership. The ASF licenses this file
  36. * to you under the Apache License, Version 2.0 (the
  37. * "License"); you may not use this file except in compliance
  38. * with the License. You may obtain a copy of the License at
  39. *
  40. * http://www.apache.org/licenses/LICENSE-2.0
  41. *
  42. * Unless required by applicable law or agreed to in writing,
  43. * software distributed under the License is distributed on an
  44. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  45. * KIND, either express or implied. See the License for the
  46. * specific language governing permissions and limitations
  47. * under the License.
  48. */
  49. var FOCUS_ADJACENCY = '__focusNodeAdjacency';
  50. var UNFOCUS_ADJACENCY = '__unfocusNodeAdjacency';
  51. var nodeOpacityPath = ['itemStyle', 'opacity'];
  52. var lineOpacityPath = ['lineStyle', 'opacity'];
  53. function getItemOpacity(item, opacityPath) {
  54. var opacity = item.getVisual('opacity');
  55. return opacity != null ? opacity : item.getModel().get(opacityPath);
  56. }
  57. function fadeOutItem(item, opacityPath, opacityRatio) {
  58. var el = item.getGraphicEl();
  59. var opacity = getItemOpacity(item, opacityPath);
  60. if (opacityRatio != null) {
  61. opacity == null && (opacity = 1);
  62. opacity *= opacityRatio;
  63. }
  64. el.downplay && el.downplay();
  65. el.traverse(function (child) {
  66. if (!child.isGroup) {
  67. var opct = child.lineLabelOriginalOpacity;
  68. if (opct == null || opacityRatio != null) {
  69. opct = opacity;
  70. }
  71. child.setStyle('opacity', opct);
  72. }
  73. });
  74. }
  75. function fadeInItem(item, opacityPath) {
  76. var opacity = getItemOpacity(item, opacityPath);
  77. var el = item.getGraphicEl(); // Should go back to normal opacity first, consider hoverLayer,
  78. // where current state is copied to elMirror, and support
  79. // emphasis opacity here.
  80. el.traverse(function (child) {
  81. !child.isGroup && child.setStyle('opacity', opacity);
  82. });
  83. el.highlight && el.highlight();
  84. }
  85. var _default = echarts.extendChartView({
  86. type: 'graph',
  87. init: function (ecModel, api) {
  88. var symbolDraw = new SymbolDraw();
  89. var lineDraw = new LineDraw();
  90. var group = this.group;
  91. this._controller = new RoamController(api.getZr());
  92. this._controllerHost = {
  93. target: group
  94. };
  95. group.add(symbolDraw.group);
  96. group.add(lineDraw.group);
  97. this._symbolDraw = symbolDraw;
  98. this._lineDraw = lineDraw;
  99. this._firstRender = true;
  100. },
  101. render: function (seriesModel, ecModel, api) {
  102. var graphView = this;
  103. var coordSys = seriesModel.coordinateSystem;
  104. this._model = seriesModel;
  105. var symbolDraw = this._symbolDraw;
  106. var lineDraw = this._lineDraw;
  107. var group = this.group;
  108. if (coordSys.type === 'view') {
  109. var groupNewProp = {
  110. position: coordSys.position,
  111. scale: coordSys.scale
  112. };
  113. if (this._firstRender) {
  114. group.attr(groupNewProp);
  115. } else {
  116. graphic.updateProps(group, groupNewProp, seriesModel);
  117. }
  118. } // Fix edge contact point with node
  119. adjustEdge(seriesModel.getGraph(), getNodeGlobalScale(seriesModel));
  120. var data = seriesModel.getData();
  121. symbolDraw.updateData(data);
  122. var edgeData = seriesModel.getEdgeData();
  123. lineDraw.updateData(edgeData);
  124. this._updateNodeAndLinkScale();
  125. this._updateController(seriesModel, ecModel, api);
  126. clearTimeout(this._layoutTimeout);
  127. var forceLayout = seriesModel.forceLayout;
  128. var layoutAnimation = seriesModel.get('force.layoutAnimation');
  129. if (forceLayout) {
  130. this._startForceLayoutIteration(forceLayout, layoutAnimation);
  131. }
  132. data.eachItemGraphicEl(function (el, idx) {
  133. var itemModel = data.getItemModel(idx); // Update draggable
  134. el.off('drag').off('dragend');
  135. var draggable = itemModel.get('draggable');
  136. if (draggable) {
  137. el.on('drag', function () {
  138. if (forceLayout) {
  139. forceLayout.warmUp();
  140. !this._layouting && this._startForceLayoutIteration(forceLayout, layoutAnimation);
  141. forceLayout.setFixed(idx); // Write position back to layout
  142. data.setItemLayout(idx, el.position);
  143. }
  144. }, this).on('dragend', function () {
  145. if (forceLayout) {
  146. forceLayout.setUnfixed(idx);
  147. }
  148. }, this);
  149. }
  150. el.setDraggable(draggable && forceLayout);
  151. el[FOCUS_ADJACENCY] && el.off('mouseover', el[FOCUS_ADJACENCY]);
  152. el[UNFOCUS_ADJACENCY] && el.off('mouseout', el[UNFOCUS_ADJACENCY]);
  153. if (itemModel.get('focusNodeAdjacency')) {
  154. el.on('mouseover', el[FOCUS_ADJACENCY] = function () {
  155. graphView._clearTimer();
  156. api.dispatchAction({
  157. type: 'focusNodeAdjacency',
  158. seriesId: seriesModel.id,
  159. dataIndex: el.dataIndex
  160. });
  161. });
  162. el.on('mouseout', el[UNFOCUS_ADJACENCY] = function () {
  163. graphView._dispatchUnfocus(api);
  164. });
  165. }
  166. }, this);
  167. data.graph.eachEdge(function (edge) {
  168. var el = edge.getGraphicEl();
  169. el[FOCUS_ADJACENCY] && el.off('mouseover', el[FOCUS_ADJACENCY]);
  170. el[UNFOCUS_ADJACENCY] && el.off('mouseout', el[UNFOCUS_ADJACENCY]);
  171. if (edge.getModel().get('focusNodeAdjacency')) {
  172. el.on('mouseover', el[FOCUS_ADJACENCY] = function () {
  173. graphView._clearTimer();
  174. api.dispatchAction({
  175. type: 'focusNodeAdjacency',
  176. seriesId: seriesModel.id,
  177. edgeDataIndex: edge.dataIndex
  178. });
  179. });
  180. el.on('mouseout', el[UNFOCUS_ADJACENCY] = function () {
  181. graphView._dispatchUnfocus(api);
  182. });
  183. }
  184. });
  185. var circularRotateLabel = seriesModel.get('layout') === 'circular' && seriesModel.get('circular.rotateLabel');
  186. var cx = data.getLayout('cx');
  187. var cy = data.getLayout('cy');
  188. data.eachItemGraphicEl(function (el, idx) {
  189. var itemModel = data.getItemModel(idx);
  190. var labelRotate = itemModel.get('label.rotate') || 0;
  191. var symbolPath = el.getSymbolPath();
  192. if (circularRotateLabel) {
  193. var pos = data.getItemLayout(idx);
  194. var rad = Math.atan2(pos[1] - cy, pos[0] - cx);
  195. if (rad < 0) {
  196. rad = Math.PI * 2 + rad;
  197. }
  198. var isLeft = pos[0] < cx;
  199. if (isLeft) {
  200. rad = rad - Math.PI;
  201. }
  202. var textPosition = isLeft ? 'left' : 'right';
  203. graphic.modifyLabelStyle(symbolPath, {
  204. textRotation: -rad,
  205. textPosition: textPosition,
  206. textOrigin: 'center'
  207. }, {
  208. textPosition: textPosition
  209. });
  210. } else {
  211. graphic.modifyLabelStyle(symbolPath, {
  212. textRotation: labelRotate *= Math.PI / 180
  213. });
  214. }
  215. });
  216. this._firstRender = false;
  217. },
  218. dispose: function () {
  219. this._controller && this._controller.dispose();
  220. this._controllerHost = {};
  221. this._clearTimer();
  222. },
  223. _dispatchUnfocus: function (api, opt) {
  224. var self = this;
  225. this._clearTimer();
  226. this._unfocusDelayTimer = setTimeout(function () {
  227. self._unfocusDelayTimer = null;
  228. api.dispatchAction({
  229. type: 'unfocusNodeAdjacency',
  230. seriesId: self._model.id
  231. });
  232. }, 500);
  233. },
  234. _clearTimer: function () {
  235. if (this._unfocusDelayTimer) {
  236. clearTimeout(this._unfocusDelayTimer);
  237. this._unfocusDelayTimer = null;
  238. }
  239. },
  240. focusNodeAdjacency: function (seriesModel, ecModel, api, payload) {
  241. var data = seriesModel.getData();
  242. var graph = data.graph;
  243. var dataIndex = payload.dataIndex;
  244. var edgeDataIndex = payload.edgeDataIndex;
  245. var node = graph.getNodeByIndex(dataIndex);
  246. var edge = graph.getEdgeByIndex(edgeDataIndex);
  247. if (!node && !edge) {
  248. return;
  249. }
  250. graph.eachNode(function (node) {
  251. fadeOutItem(node, nodeOpacityPath, 0.1);
  252. });
  253. graph.eachEdge(function (edge) {
  254. fadeOutItem(edge, lineOpacityPath, 0.1);
  255. });
  256. if (node) {
  257. fadeInItem(node, nodeOpacityPath);
  258. zrUtil.each(node.edges, function (adjacentEdge) {
  259. if (adjacentEdge.dataIndex < 0) {
  260. return;
  261. }
  262. fadeInItem(adjacentEdge, lineOpacityPath);
  263. fadeInItem(adjacentEdge.node1, nodeOpacityPath);
  264. fadeInItem(adjacentEdge.node2, nodeOpacityPath);
  265. });
  266. }
  267. if (edge) {
  268. fadeInItem(edge, lineOpacityPath);
  269. fadeInItem(edge.node1, nodeOpacityPath);
  270. fadeInItem(edge.node2, nodeOpacityPath);
  271. }
  272. },
  273. unfocusNodeAdjacency: function (seriesModel, ecModel, api, payload) {
  274. var graph = seriesModel.getData().graph;
  275. graph.eachNode(function (node) {
  276. fadeOutItem(node, nodeOpacityPath);
  277. });
  278. graph.eachEdge(function (edge) {
  279. fadeOutItem(edge, lineOpacityPath);
  280. });
  281. },
  282. _startForceLayoutIteration: function (forceLayout, layoutAnimation) {
  283. var self = this;
  284. (function step() {
  285. forceLayout.step(function (stopped) {
  286. self.updateLayout(self._model);
  287. (self._layouting = !stopped) && (layoutAnimation ? self._layoutTimeout = setTimeout(step, 16) : step());
  288. });
  289. })();
  290. },
  291. _updateController: function (seriesModel, ecModel, api) {
  292. var controller = this._controller;
  293. var controllerHost = this._controllerHost;
  294. var group = this.group;
  295. controller.setPointerChecker(function (e, x, y) {
  296. var rect = group.getBoundingRect();
  297. rect.applyTransform(group.transform);
  298. return rect.contain(x, y) && !onIrrelevantElement(e, api, seriesModel);
  299. });
  300. if (seriesModel.coordinateSystem.type !== 'view') {
  301. controller.disable();
  302. return;
  303. }
  304. controller.enable(seriesModel.get('roam'));
  305. controllerHost.zoomLimit = seriesModel.get('scaleLimit');
  306. controllerHost.zoom = seriesModel.coordinateSystem.getZoom();
  307. controller.off('pan').off('zoom').on('pan', function (e) {
  308. roamHelper.updateViewOnPan(controllerHost, e.dx, e.dy);
  309. api.dispatchAction({
  310. seriesId: seriesModel.id,
  311. type: 'graphRoam',
  312. dx: e.dx,
  313. dy: e.dy
  314. });
  315. }).on('zoom', function (e) {
  316. roamHelper.updateViewOnZoom(controllerHost, e.scale, e.originX, e.originY);
  317. api.dispatchAction({
  318. seriesId: seriesModel.id,
  319. type: 'graphRoam',
  320. zoom: e.scale,
  321. originX: e.originX,
  322. originY: e.originY
  323. });
  324. this._updateNodeAndLinkScale();
  325. adjustEdge(seriesModel.getGraph(), getNodeGlobalScale(seriesModel));
  326. this._lineDraw.updateLayout();
  327. }, this);
  328. },
  329. _updateNodeAndLinkScale: function () {
  330. var seriesModel = this._model;
  331. var data = seriesModel.getData();
  332. var nodeScale = getNodeGlobalScale(seriesModel);
  333. var invScale = [nodeScale, nodeScale];
  334. data.eachItemGraphicEl(function (el, idx) {
  335. el.attr('scale', invScale);
  336. });
  337. },
  338. updateLayout: function (seriesModel) {
  339. adjustEdge(seriesModel.getGraph(), getNodeGlobalScale(seriesModel));
  340. this._symbolDraw.updateLayout();
  341. this._lineDraw.updateLayout();
  342. },
  343. remove: function (ecModel, api) {
  344. this._symbolDraw && this._symbolDraw.remove();
  345. this._lineDraw && this._lineDraw.remove();
  346. }
  347. });
  348. module.exports = _default;