SankeyView.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  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. import { __extends } from "tslib";
  41. import * as graphic from '../../util/graphic.js';
  42. import { enterEmphasis, leaveEmphasis, toggleHoverEmphasis, setStatesStylesFromModel } from '../../util/states.js';
  43. import ChartView from '../../view/Chart.js';
  44. import { setLabelStyle, getLabelStatesModels } from '../../label/labelStyle.js';
  45. import { getECData } from '../../util/innerStore.js';
  46. import { isString } from 'zrender/lib/core/util.js';
  47. var SankeyPathShape =
  48. /** @class */
  49. function () {
  50. function SankeyPathShape() {
  51. this.x1 = 0;
  52. this.y1 = 0;
  53. this.x2 = 0;
  54. this.y2 = 0;
  55. this.cpx1 = 0;
  56. this.cpy1 = 0;
  57. this.cpx2 = 0;
  58. this.cpy2 = 0;
  59. this.extent = 0;
  60. }
  61. return SankeyPathShape;
  62. }();
  63. var SankeyPath =
  64. /** @class */
  65. function (_super) {
  66. __extends(SankeyPath, _super);
  67. function SankeyPath(opts) {
  68. return _super.call(this, opts) || this;
  69. }
  70. SankeyPath.prototype.getDefaultShape = function () {
  71. return new SankeyPathShape();
  72. };
  73. SankeyPath.prototype.buildPath = function (ctx, shape) {
  74. var extent = shape.extent;
  75. ctx.moveTo(shape.x1, shape.y1);
  76. ctx.bezierCurveTo(shape.cpx1, shape.cpy1, shape.cpx2, shape.cpy2, shape.x2, shape.y2);
  77. if (shape.orient === 'vertical') {
  78. ctx.lineTo(shape.x2 + extent, shape.y2);
  79. ctx.bezierCurveTo(shape.cpx2 + extent, shape.cpy2, shape.cpx1 + extent, shape.cpy1, shape.x1 + extent, shape.y1);
  80. } else {
  81. ctx.lineTo(shape.x2, shape.y2 + extent);
  82. ctx.bezierCurveTo(shape.cpx2, shape.cpy2 + extent, shape.cpx1, shape.cpy1 + extent, shape.x1, shape.y1 + extent);
  83. }
  84. ctx.closePath();
  85. };
  86. SankeyPath.prototype.highlight = function () {
  87. enterEmphasis(this);
  88. };
  89. SankeyPath.prototype.downplay = function () {
  90. leaveEmphasis(this);
  91. };
  92. return SankeyPath;
  93. }(graphic.Path);
  94. var SankeyView =
  95. /** @class */
  96. function (_super) {
  97. __extends(SankeyView, _super);
  98. function SankeyView() {
  99. var _this = _super !== null && _super.apply(this, arguments) || this;
  100. _this.type = SankeyView.type;
  101. _this._focusAdjacencyDisabled = false;
  102. return _this;
  103. }
  104. SankeyView.prototype.render = function (seriesModel, ecModel, api) {
  105. var sankeyView = this;
  106. var graph = seriesModel.getGraph();
  107. var group = this.group;
  108. var layoutInfo = seriesModel.layoutInfo; // view width
  109. var width = layoutInfo.width; // view height
  110. var height = layoutInfo.height;
  111. var nodeData = seriesModel.getData();
  112. var edgeData = seriesModel.getData('edge');
  113. var orient = seriesModel.get('orient');
  114. this._model = seriesModel;
  115. group.removeAll();
  116. group.x = layoutInfo.x;
  117. group.y = layoutInfo.y; // generate a bezire Curve for each edge
  118. graph.eachEdge(function (edge) {
  119. var curve = new SankeyPath();
  120. var ecData = getECData(curve);
  121. ecData.dataIndex = edge.dataIndex;
  122. ecData.seriesIndex = seriesModel.seriesIndex;
  123. ecData.dataType = 'edge';
  124. var edgeModel = edge.getModel();
  125. var lineStyleModel = edgeModel.getModel('lineStyle');
  126. var curvature = lineStyleModel.get('curveness');
  127. var n1Layout = edge.node1.getLayout();
  128. var node1Model = edge.node1.getModel();
  129. var dragX1 = node1Model.get('localX');
  130. var dragY1 = node1Model.get('localY');
  131. var n2Layout = edge.node2.getLayout();
  132. var node2Model = edge.node2.getModel();
  133. var dragX2 = node2Model.get('localX');
  134. var dragY2 = node2Model.get('localY');
  135. var edgeLayout = edge.getLayout();
  136. var x1;
  137. var y1;
  138. var x2;
  139. var y2;
  140. var cpx1;
  141. var cpy1;
  142. var cpx2;
  143. var cpy2;
  144. curve.shape.extent = Math.max(1, edgeLayout.dy);
  145. curve.shape.orient = orient;
  146. if (orient === 'vertical') {
  147. x1 = (dragX1 != null ? dragX1 * width : n1Layout.x) + edgeLayout.sy;
  148. y1 = (dragY1 != null ? dragY1 * height : n1Layout.y) + n1Layout.dy;
  149. x2 = (dragX2 != null ? dragX2 * width : n2Layout.x) + edgeLayout.ty;
  150. y2 = dragY2 != null ? dragY2 * height : n2Layout.y;
  151. cpx1 = x1;
  152. cpy1 = y1 * (1 - curvature) + y2 * curvature;
  153. cpx2 = x2;
  154. cpy2 = y1 * curvature + y2 * (1 - curvature);
  155. } else {
  156. x1 = (dragX1 != null ? dragX1 * width : n1Layout.x) + n1Layout.dx;
  157. y1 = (dragY1 != null ? dragY1 * height : n1Layout.y) + edgeLayout.sy;
  158. x2 = dragX2 != null ? dragX2 * width : n2Layout.x;
  159. y2 = (dragY2 != null ? dragY2 * height : n2Layout.y) + edgeLayout.ty;
  160. cpx1 = x1 * (1 - curvature) + x2 * curvature;
  161. cpy1 = y1;
  162. cpx2 = x1 * curvature + x2 * (1 - curvature);
  163. cpy2 = y2;
  164. }
  165. curve.setShape({
  166. x1: x1,
  167. y1: y1,
  168. x2: x2,
  169. y2: y2,
  170. cpx1: cpx1,
  171. cpy1: cpy1,
  172. cpx2: cpx2,
  173. cpy2: cpy2
  174. });
  175. curve.useStyle(lineStyleModel.getItemStyle()); // Special color, use source node color or target node color
  176. switch (curve.style.fill) {
  177. case 'source':
  178. curve.style.fill = edge.node1.getVisual('color');
  179. curve.style.decal = edge.node1.getVisual('style').decal;
  180. break;
  181. case 'target':
  182. curve.style.fill = edge.node2.getVisual('color');
  183. curve.style.decal = edge.node2.getVisual('style').decal;
  184. break;
  185. case 'gradient':
  186. var sourceColor = edge.node1.getVisual('color');
  187. var targetColor = edge.node2.getVisual('color');
  188. if (isString(sourceColor) && isString(targetColor)) {
  189. curve.style.fill = new graphic.LinearGradient(0, 0, +(orient === 'horizontal'), +(orient === 'vertical'), [{
  190. color: sourceColor,
  191. offset: 0
  192. }, {
  193. color: targetColor,
  194. offset: 1
  195. }]);
  196. }
  197. }
  198. var emphasisModel = edgeModel.getModel('emphasis');
  199. setStatesStylesFromModel(curve, edgeModel, 'lineStyle', function (model) {
  200. return model.getItemStyle();
  201. });
  202. group.add(curve);
  203. edgeData.setItemGraphicEl(edge.dataIndex, curve);
  204. var focus = emphasisModel.get('focus');
  205. toggleHoverEmphasis(curve, focus === 'adjacency' ? edge.getAdjacentDataIndices() : focus, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  206. getECData(curve).dataType = 'edge';
  207. }); // Generate a rect for each node
  208. graph.eachNode(function (node) {
  209. var layout = node.getLayout();
  210. var itemModel = node.getModel();
  211. var dragX = itemModel.get('localX');
  212. var dragY = itemModel.get('localY');
  213. var emphasisModel = itemModel.getModel('emphasis');
  214. var rect = new graphic.Rect({
  215. shape: {
  216. x: dragX != null ? dragX * width : layout.x,
  217. y: dragY != null ? dragY * height : layout.y,
  218. width: layout.dx,
  219. height: layout.dy
  220. },
  221. style: itemModel.getModel('itemStyle').getItemStyle(),
  222. z2: 10
  223. });
  224. setLabelStyle(rect, getLabelStatesModels(itemModel), {
  225. labelFetcher: seriesModel,
  226. labelDataIndex: node.dataIndex,
  227. defaultText: node.id
  228. });
  229. rect.disableLabelAnimation = true;
  230. rect.setStyle('fill', node.getVisual('color'));
  231. rect.setStyle('decal', node.getVisual('style').decal);
  232. setStatesStylesFromModel(rect, itemModel);
  233. group.add(rect);
  234. nodeData.setItemGraphicEl(node.dataIndex, rect);
  235. getECData(rect).dataType = 'node';
  236. var focus = emphasisModel.get('focus');
  237. toggleHoverEmphasis(rect, focus === 'adjacency' ? node.getAdjacentDataIndices() : focus, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  238. });
  239. nodeData.eachItemGraphicEl(function (el, dataIndex) {
  240. var itemModel = nodeData.getItemModel(dataIndex);
  241. if (itemModel.get('draggable')) {
  242. el.drift = function (dx, dy) {
  243. sankeyView._focusAdjacencyDisabled = true;
  244. this.shape.x += dx;
  245. this.shape.y += dy;
  246. this.dirty();
  247. api.dispatchAction({
  248. type: 'dragNode',
  249. seriesId: seriesModel.id,
  250. dataIndex: nodeData.getRawIndex(dataIndex),
  251. localX: this.shape.x / width,
  252. localY: this.shape.y / height
  253. });
  254. };
  255. el.ondragend = function () {
  256. sankeyView._focusAdjacencyDisabled = false;
  257. };
  258. el.draggable = true;
  259. el.cursor = 'move';
  260. }
  261. });
  262. if (!this._data && seriesModel.isAnimationEnabled()) {
  263. group.setClipPath(createGridClipShape(group.getBoundingRect(), seriesModel, function () {
  264. group.removeClipPath();
  265. }));
  266. }
  267. this._data = seriesModel.getData();
  268. };
  269. SankeyView.prototype.dispose = function () {};
  270. SankeyView.type = 'sankey';
  271. return SankeyView;
  272. }(ChartView); // Add animation to the view
  273. function createGridClipShape(rect, seriesModel, cb) {
  274. var rectEl = new graphic.Rect({
  275. shape: {
  276. x: rect.x - 10,
  277. y: rect.y - 10,
  278. width: 0,
  279. height: rect.height + 20
  280. }
  281. });
  282. graphic.initProps(rectEl, {
  283. shape: {
  284. width: rect.width + 20
  285. }
  286. }, seriesModel, cb);
  287. return rectEl;
  288. }
  289. export default SankeyView;