SankeyView.js 13 KB

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