SankeyView.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 graphic = require("../../util/graphic");
  20. var echarts = require("../../echarts");
  21. var zrUtil = require("zrender/lib/core/util");
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. var nodeOpacityPath = ['itemStyle', 'opacity'];
  41. var hoverNodeOpacityPath = ['emphasis', 'itemStyle', 'opacity'];
  42. var lineOpacityPath = ['lineStyle', 'opacity'];
  43. var hoverLineOpacityPath = ['emphasis', 'lineStyle', 'opacity'];
  44. function getItemOpacity(item, opacityPath) {
  45. return item.getVisual('opacity') || item.getModel().get(opacityPath);
  46. }
  47. function fadeOutItem(item, opacityPath, opacityRatio) {
  48. var el = item.getGraphicEl();
  49. var opacity = getItemOpacity(item, opacityPath);
  50. if (opacityRatio != null) {
  51. opacity == null && (opacity = 1);
  52. opacity *= opacityRatio;
  53. }
  54. el.downplay && el.downplay();
  55. el.traverse(function (child) {
  56. if (child.type !== 'group') {
  57. child.setStyle('opacity', opacity);
  58. }
  59. });
  60. }
  61. function fadeInItem(item, opacityPath) {
  62. var opacity = getItemOpacity(item, opacityPath);
  63. var el = item.getGraphicEl();
  64. el.traverse(function (child) {
  65. if (child.type !== 'group') {
  66. child.setStyle('opacity', opacity);
  67. }
  68. }); // Support emphasis here.
  69. el.highlight && el.highlight();
  70. }
  71. var SankeyShape = graphic.extendShape({
  72. shape: {
  73. x1: 0,
  74. y1: 0,
  75. x2: 0,
  76. y2: 0,
  77. cpx1: 0,
  78. cpy1: 0,
  79. cpx2: 0,
  80. cpy2: 0,
  81. extent: 0,
  82. orient: ''
  83. },
  84. buildPath: function (ctx, shape) {
  85. var extent = shape.extent;
  86. ctx.moveTo(shape.x1, shape.y1);
  87. ctx.bezierCurveTo(shape.cpx1, shape.cpy1, shape.cpx2, shape.cpy2, shape.x2, shape.y2);
  88. if (shape.orient === 'vertical') {
  89. ctx.lineTo(shape.x2 + extent, shape.y2);
  90. ctx.bezierCurveTo(shape.cpx2 + extent, shape.cpy2, shape.cpx1 + extent, shape.cpy1, shape.x1 + extent, shape.y1);
  91. } else {
  92. ctx.lineTo(shape.x2, shape.y2 + extent);
  93. ctx.bezierCurveTo(shape.cpx2, shape.cpy2 + extent, shape.cpx1, shape.cpy1 + extent, shape.x1, shape.y1 + extent);
  94. }
  95. ctx.closePath();
  96. },
  97. highlight: function () {
  98. this.trigger('emphasis');
  99. },
  100. downplay: function () {
  101. this.trigger('normal');
  102. }
  103. });
  104. var _default = echarts.extendChartView({
  105. type: 'sankey',
  106. /**
  107. * @private
  108. * @type {module:echarts/chart/sankey/SankeySeries}
  109. */
  110. _model: null,
  111. /**
  112. * @private
  113. * @type {boolean}
  114. */
  115. _focusAdjacencyDisabled: false,
  116. render: function (seriesModel, ecModel, api) {
  117. var sankeyView = this;
  118. var graph = seriesModel.getGraph();
  119. var group = this.group;
  120. var layoutInfo = seriesModel.layoutInfo; // view width
  121. var width = layoutInfo.width; // view height
  122. var height = layoutInfo.height;
  123. var nodeData = seriesModel.getData();
  124. var edgeData = seriesModel.getData('edge');
  125. var orient = seriesModel.get('orient');
  126. this._model = seriesModel;
  127. group.removeAll();
  128. group.attr('position', [layoutInfo.x, layoutInfo.y]); // generate a bezire Curve for each edge
  129. graph.eachEdge(function (edge) {
  130. var curve = new SankeyShape();
  131. curve.dataIndex = edge.dataIndex;
  132. curve.seriesIndex = seriesModel.seriesIndex;
  133. curve.dataType = 'edge';
  134. var lineStyleModel = edge.getModel('lineStyle');
  135. var curvature = lineStyleModel.get('curveness');
  136. var n1Layout = edge.node1.getLayout();
  137. var node1Model = edge.node1.getModel();
  138. var dragX1 = node1Model.get('localX');
  139. var dragY1 = node1Model.get('localY');
  140. var n2Layout = edge.node2.getLayout();
  141. var node2Model = edge.node2.getModel();
  142. var dragX2 = node2Model.get('localX');
  143. var dragY2 = node2Model.get('localY');
  144. var edgeLayout = edge.getLayout();
  145. var x1;
  146. var y1;
  147. var x2;
  148. var y2;
  149. var cpx1;
  150. var cpy1;
  151. var cpx2;
  152. var cpy2;
  153. curve.shape.extent = Math.max(1, edgeLayout.dy);
  154. curve.shape.orient = orient;
  155. if (orient === 'vertical') {
  156. x1 = (dragX1 != null ? dragX1 * width : n1Layout.x) + edgeLayout.sy;
  157. y1 = (dragY1 != null ? dragY1 * height : n1Layout.y) + n1Layout.dy;
  158. x2 = (dragX2 != null ? dragX2 * width : n2Layout.x) + edgeLayout.ty;
  159. y2 = dragY2 != null ? dragY2 * height : n2Layout.y;
  160. cpx1 = x1;
  161. cpy1 = y1 * (1 - curvature) + y2 * curvature;
  162. cpx2 = x2;
  163. cpy2 = y1 * curvature + y2 * (1 - curvature);
  164. } else {
  165. x1 = (dragX1 != null ? dragX1 * width : n1Layout.x) + n1Layout.dx;
  166. y1 = (dragY1 != null ? dragY1 * height : n1Layout.y) + edgeLayout.sy;
  167. x2 = dragX2 != null ? dragX2 * width : n2Layout.x;
  168. y2 = (dragY2 != null ? dragY2 * height : n2Layout.y) + edgeLayout.ty;
  169. cpx1 = x1 * (1 - curvature) + x2 * curvature;
  170. cpy1 = y1;
  171. cpx2 = x1 * curvature + x2 * (1 - curvature);
  172. cpy2 = y2;
  173. }
  174. curve.setShape({
  175. x1: x1,
  176. y1: y1,
  177. x2: x2,
  178. y2: y2,
  179. cpx1: cpx1,
  180. cpy1: cpy1,
  181. cpx2: cpx2,
  182. cpy2: cpy2
  183. });
  184. curve.setStyle(lineStyleModel.getItemStyle()); // Special color, use source node color or target node color
  185. switch (curve.style.fill) {
  186. case 'source':
  187. curve.style.fill = edge.node1.getVisual('color');
  188. break;
  189. case 'target':
  190. curve.style.fill = edge.node2.getVisual('color');
  191. break;
  192. }
  193. graphic.setHoverStyle(curve, edge.getModel('emphasis.lineStyle').getItemStyle());
  194. group.add(curve);
  195. edgeData.setItemGraphicEl(edge.dataIndex, curve);
  196. }); // Generate a rect for each node
  197. graph.eachNode(function (node) {
  198. var layout = node.getLayout();
  199. var itemModel = node.getModel();
  200. var dragX = itemModel.get('localX');
  201. var dragY = itemModel.get('localY');
  202. var labelModel = itemModel.getModel('label');
  203. var labelHoverModel = itemModel.getModel('emphasis.label');
  204. var rect = new graphic.Rect({
  205. shape: {
  206. x: dragX != null ? dragX * width : layout.x,
  207. y: dragY != null ? dragY * height : layout.y,
  208. width: layout.dx,
  209. height: layout.dy
  210. },
  211. style: itemModel.getModel('itemStyle').getItemStyle()
  212. });
  213. var hoverStyle = node.getModel('emphasis.itemStyle').getItemStyle();
  214. graphic.setLabelStyle(rect.style, hoverStyle, labelModel, labelHoverModel, {
  215. labelFetcher: seriesModel,
  216. labelDataIndex: node.dataIndex,
  217. defaultText: node.id,
  218. isRectText: true
  219. });
  220. rect.setStyle('fill', node.getVisual('color'));
  221. graphic.setHoverStyle(rect, hoverStyle);
  222. group.add(rect);
  223. nodeData.setItemGraphicEl(node.dataIndex, rect);
  224. rect.dataType = 'node';
  225. });
  226. nodeData.eachItemGraphicEl(function (el, dataIndex) {
  227. var itemModel = nodeData.getItemModel(dataIndex);
  228. if (itemModel.get('draggable')) {
  229. el.drift = function (dx, dy) {
  230. sankeyView._focusAdjacencyDisabled = true;
  231. this.shape.x += dx;
  232. this.shape.y += dy;
  233. this.dirty();
  234. api.dispatchAction({
  235. type: 'dragNode',
  236. seriesId: seriesModel.id,
  237. dataIndex: nodeData.getRawIndex(dataIndex),
  238. localX: this.shape.x / width,
  239. localY: this.shape.y / height
  240. });
  241. };
  242. el.ondragend = function () {
  243. sankeyView._focusAdjacencyDisabled = false;
  244. };
  245. el.draggable = true;
  246. el.cursor = 'move';
  247. }
  248. el.highlight = function () {
  249. this.trigger('emphasis');
  250. };
  251. el.downplay = function () {
  252. this.trigger('normal');
  253. };
  254. el.focusNodeAdjHandler && el.off('mouseover', el.focusNodeAdjHandler);
  255. el.unfocusNodeAdjHandler && el.off('mouseout', el.unfocusNodeAdjHandler);
  256. if (itemModel.get('focusNodeAdjacency')) {
  257. el.on('mouseover', el.focusNodeAdjHandler = function () {
  258. if (!sankeyView._focusAdjacencyDisabled) {
  259. sankeyView._clearTimer();
  260. api.dispatchAction({
  261. type: 'focusNodeAdjacency',
  262. seriesId: seriesModel.id,
  263. dataIndex: el.dataIndex
  264. });
  265. }
  266. });
  267. el.on('mouseout', el.unfocusNodeAdjHandler = function () {
  268. if (!sankeyView._focusAdjacencyDisabled) {
  269. sankeyView._dispatchUnfocus(api);
  270. }
  271. });
  272. }
  273. });
  274. edgeData.eachItemGraphicEl(function (el, dataIndex) {
  275. var edgeModel = edgeData.getItemModel(dataIndex);
  276. el.focusNodeAdjHandler && el.off('mouseover', el.focusNodeAdjHandler);
  277. el.unfocusNodeAdjHandler && el.off('mouseout', el.unfocusNodeAdjHandler);
  278. if (edgeModel.get('focusNodeAdjacency')) {
  279. el.on('mouseover', el.focusNodeAdjHandler = function () {
  280. if (!sankeyView._focusAdjacencyDisabled) {
  281. sankeyView._clearTimer();
  282. api.dispatchAction({
  283. type: 'focusNodeAdjacency',
  284. seriesId: seriesModel.id,
  285. edgeDataIndex: el.dataIndex
  286. });
  287. }
  288. });
  289. el.on('mouseout', el.unfocusNodeAdjHandler = function () {
  290. if (!sankeyView._focusAdjacencyDisabled) {
  291. sankeyView._dispatchUnfocus(api);
  292. }
  293. });
  294. }
  295. });
  296. if (!this._data && seriesModel.get('animation')) {
  297. group.setClipPath(createGridClipShape(group.getBoundingRect(), seriesModel, function () {
  298. group.removeClipPath();
  299. }));
  300. }
  301. this._data = seriesModel.getData();
  302. },
  303. dispose: function () {
  304. this._clearTimer();
  305. },
  306. _dispatchUnfocus: function (api) {
  307. var self = this;
  308. this._clearTimer();
  309. this._unfocusDelayTimer = setTimeout(function () {
  310. self._unfocusDelayTimer = null;
  311. api.dispatchAction({
  312. type: 'unfocusNodeAdjacency',
  313. seriesId: self._model.id
  314. });
  315. }, 500);
  316. },
  317. _clearTimer: function () {
  318. if (this._unfocusDelayTimer) {
  319. clearTimeout(this._unfocusDelayTimer);
  320. this._unfocusDelayTimer = null;
  321. }
  322. },
  323. focusNodeAdjacency: function (seriesModel, ecModel, api, payload) {
  324. var data = seriesModel.getData();
  325. var graph = data.graph;
  326. var dataIndex = payload.dataIndex;
  327. var itemModel = data.getItemModel(dataIndex);
  328. var edgeDataIndex = payload.edgeDataIndex;
  329. if (dataIndex == null && edgeDataIndex == null) {
  330. return;
  331. }
  332. var node = graph.getNodeByIndex(dataIndex);
  333. var edge = graph.getEdgeByIndex(edgeDataIndex);
  334. graph.eachNode(function (node) {
  335. fadeOutItem(node, nodeOpacityPath, 0.1);
  336. });
  337. graph.eachEdge(function (edge) {
  338. fadeOutItem(edge, lineOpacityPath, 0.1);
  339. });
  340. if (node) {
  341. fadeInItem(node, hoverNodeOpacityPath);
  342. var focusNodeAdj = itemModel.get('focusNodeAdjacency');
  343. if (focusNodeAdj === 'outEdges') {
  344. zrUtil.each(node.outEdges, function (edge) {
  345. if (edge.dataIndex < 0) {
  346. return;
  347. }
  348. fadeInItem(edge, hoverLineOpacityPath);
  349. fadeInItem(edge.node2, hoverNodeOpacityPath);
  350. });
  351. } else if (focusNodeAdj === 'inEdges') {
  352. zrUtil.each(node.inEdges, function (edge) {
  353. if (edge.dataIndex < 0) {
  354. return;
  355. }
  356. fadeInItem(edge, hoverLineOpacityPath);
  357. fadeInItem(edge.node1, hoverNodeOpacityPath);
  358. });
  359. } else if (focusNodeAdj === 'allEdges') {
  360. zrUtil.each(node.edges, function (edge) {
  361. if (edge.dataIndex < 0) {
  362. return;
  363. }
  364. fadeInItem(edge, hoverLineOpacityPath);
  365. edge.node1 !== node && fadeInItem(edge.node1, hoverNodeOpacityPath);
  366. edge.node2 !== node && fadeInItem(edge.node2, hoverNodeOpacityPath);
  367. });
  368. }
  369. }
  370. if (edge) {
  371. fadeInItem(edge, hoverLineOpacityPath);
  372. fadeInItem(edge.node1, hoverNodeOpacityPath);
  373. fadeInItem(edge.node2, hoverNodeOpacityPath);
  374. }
  375. },
  376. unfocusNodeAdjacency: function (seriesModel, ecModel, api, payload) {
  377. var graph = seriesModel.getGraph();
  378. graph.eachNode(function (node) {
  379. fadeOutItem(node, nodeOpacityPath);
  380. });
  381. graph.eachEdge(function (edge) {
  382. fadeOutItem(edge, lineOpacityPath);
  383. });
  384. }
  385. }); // Add animation to the view
  386. function createGridClipShape(rect, seriesModel, cb) {
  387. var rectEl = new graphic.Rect({
  388. shape: {
  389. x: rect.x - 10,
  390. y: rect.y - 10,
  391. width: 0,
  392. height: rect.height + 20
  393. }
  394. });
  395. graphic.initProps(rectEl, {
  396. shape: {
  397. width: rect.width + 20
  398. }
  399. }, seriesModel, cb);
  400. return rectEl;
  401. }
  402. module.exports = _default;