SankeyView.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 { SERIES_TYPE_SANKEY } from './SankeySeries.js';
  44. import ChartView from '../../view/Chart.js';
  45. import { setLabelStyle, getLabelStatesModels } from '../../label/labelStyle.js';
  46. import { getECData } from '../../util/innerStore.js';
  47. import { isString, retrieve3 } from 'zrender/lib/core/util.js';
  48. import RoamController from '../../component/helper/RoamController.js';
  49. import { applyViewCoordSysTransToElement, VIEW_COORD_SYS_TRANS_OVERALL } from '../../coord/View.js';
  50. import { createIsInSelfByPointerCheckerEl, createViewCoordSysSimply, updateRoamControllerSimply } from '../../component/helper/roamHelper.js';
  51. var SankeyPathShape = /** @class */function () {
  52. function SankeyPathShape() {
  53. this.x1 = 0;
  54. this.y1 = 0;
  55. this.x2 = 0;
  56. this.y2 = 0;
  57. this.cpx1 = 0;
  58. this.cpy1 = 0;
  59. this.cpx2 = 0;
  60. this.cpy2 = 0;
  61. this.extent = 0;
  62. }
  63. return SankeyPathShape;
  64. }();
  65. var SankeyPath = /** @class */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 = /** @class */function (_super) {
  95. __extends(SankeyView, _super);
  96. function SankeyView() {
  97. var _this = _super !== null && _super.apply(this, arguments) || this;
  98. _this.type = SERIES_TYPE_SANKEY;
  99. _this._mainGroup = new graphic.Group();
  100. return _this;
  101. }
  102. SankeyView.prototype.init = function (ecModel, api) {
  103. this._controller = new RoamController(api.getZr());
  104. this.group.add(this._mainGroup);
  105. this._firstRender = true;
  106. };
  107. SankeyView.prototype.render = function (seriesModel, ecModel, api) {
  108. var graph = seriesModel.getGraph();
  109. var mainGroup = this._mainGroup;
  110. var layoutInfo = seriesModel.layoutInfo;
  111. // view width
  112. var width = layoutInfo.width;
  113. // 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. mainGroup.removeAll();
  119. mainGroup.x = layoutInfo.x;
  120. mainGroup.y = layoutInfo.y;
  121. this._updateViewCoordSys(seriesModel, api);
  122. updateRoamControllerSimply(seriesModel, api, this._controller, createIsInSelfByPointerCheckerEl(mainGroup), null);
  123. // generate a bezire Curve for each edge
  124. graph.eachEdge(function (edge) {
  125. var curve = new SankeyPath();
  126. var ecData = getECData(curve);
  127. ecData.dataIndex = edge.dataIndex;
  128. ecData.seriesIndex = seriesModel.seriesIndex;
  129. ecData.dataType = 'edge';
  130. var edgeModel = edge.getModel();
  131. var lineStyleModel = edgeModel.getModel('lineStyle');
  132. var curvature = lineStyleModel.get('curveness');
  133. var n1Layout = edge.node1.getLayout();
  134. var node1Model = edge.node1.getModel();
  135. var dragX1 = node1Model.get('localX');
  136. var dragY1 = node1Model.get('localY');
  137. var n2Layout = edge.node2.getLayout();
  138. var node2Model = edge.node2.getModel();
  139. var dragX2 = node2Model.get('localX');
  140. var dragY2 = node2Model.get('localY');
  141. var edgeLayout = edge.getLayout();
  142. var x1;
  143. var y1;
  144. var x2;
  145. var y2;
  146. var cpx1;
  147. var cpy1;
  148. var cpx2;
  149. var cpy2;
  150. curve.shape.extent = Math.max(1, edgeLayout.dy);
  151. curve.shape.orient = orient;
  152. if (orient === 'vertical') {
  153. x1 = (dragX1 != null ? dragX1 * width : n1Layout.x) + edgeLayout.sy;
  154. y1 = (dragY1 != null ? dragY1 * height : n1Layout.y) + n1Layout.dy;
  155. x2 = (dragX2 != null ? dragX2 * width : n2Layout.x) + edgeLayout.ty;
  156. y2 = dragY2 != null ? dragY2 * height : n2Layout.y;
  157. cpx1 = x1;
  158. cpy1 = y1 * (1 - curvature) + y2 * curvature;
  159. cpx2 = x2;
  160. cpy2 = y1 * curvature + y2 * (1 - curvature);
  161. } else {
  162. x1 = (dragX1 != null ? dragX1 * width : n1Layout.x) + n1Layout.dx;
  163. y1 = (dragY1 != null ? dragY1 * height : n1Layout.y) + edgeLayout.sy;
  164. x2 = dragX2 != null ? dragX2 * width : n2Layout.x;
  165. y2 = (dragY2 != null ? dragY2 * height : n2Layout.y) + edgeLayout.ty;
  166. cpx1 = x1 * (1 - curvature) + x2 * curvature;
  167. cpy1 = y1;
  168. cpx2 = x1 * curvature + x2 * (1 - curvature);
  169. cpy2 = y2;
  170. }
  171. curve.setShape({
  172. x1: x1,
  173. y1: y1,
  174. x2: x2,
  175. y2: y2,
  176. cpx1: cpx1,
  177. cpy1: cpy1,
  178. cpx2: cpx2,
  179. cpy2: cpy2
  180. });
  181. curve.useStyle(lineStyleModel.getItemStyle());
  182. // Special color, use source node color or target node color
  183. applyCurveStyle(curve.style, orient, edge);
  184. var defaultEdgeLabelText = "" + edgeModel.get('value');
  185. var edgeLabelStateModels = getLabelStatesModels(edgeModel, 'edgeLabel');
  186. setLabelStyle(curve, edgeLabelStateModels, {
  187. labelFetcher: {
  188. getFormattedLabel: function (dataIndex, stateName, dataType, labelDimIndex, formatter, extendParams) {
  189. return seriesModel.getFormattedLabel(dataIndex, stateName, 'edge', labelDimIndex,
  190. // ensure edgeLabel formatter is provided
  191. // to prevent the inheritance from `label.formatter` of the series
  192. retrieve3(formatter, edgeLabelStateModels.normal && edgeLabelStateModels.normal.get('formatter'), defaultEdgeLabelText), extendParams);
  193. }
  194. },
  195. labelDataIndex: edge.dataIndex,
  196. defaultText: defaultEdgeLabelText
  197. });
  198. curve.setTextConfig({
  199. position: 'inside'
  200. });
  201. var emphasisModel = edgeModel.getModel('emphasis');
  202. setStatesStylesFromModel(curve, edgeModel, 'lineStyle', function (model) {
  203. var style = model.getItemStyle();
  204. applyCurveStyle(style, orient, edge);
  205. return style;
  206. });
  207. mainGroup.add(curve);
  208. edgeData.setItemGraphicEl(edge.dataIndex, curve);
  209. var focus = emphasisModel.get('focus');
  210. toggleHoverEmphasis(curve, focus === 'adjacency' ? edge.getAdjacentDataIndices() : focus === 'trajectory' ? edge.getTrajectoryDataIndices() : focus, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  211. });
  212. // Generate a rect for each node
  213. graph.eachNode(function (node) {
  214. var layout = node.getLayout();
  215. var itemModel = node.getModel();
  216. var dragX = itemModel.get('localX');
  217. var dragY = itemModel.get('localY');
  218. var emphasisModel = itemModel.getModel('emphasis');
  219. var borderRadius = itemModel.get(['itemStyle', 'borderRadius']) || 0;
  220. var rect = new graphic.Rect({
  221. shape: {
  222. x: dragX != null ? dragX * width : layout.x,
  223. y: dragY != null ? dragY * height : layout.y,
  224. width: layout.dx,
  225. height: layout.dy,
  226. r: borderRadius
  227. },
  228. style: itemModel.getModel('itemStyle').getItemStyle(),
  229. z2: 10
  230. });
  231. setLabelStyle(rect, getLabelStatesModels(itemModel), {
  232. labelFetcher: {
  233. getFormattedLabel: function (dataIndex, stateName) {
  234. return seriesModel.getFormattedLabel(dataIndex, stateName, 'node');
  235. }
  236. },
  237. labelDataIndex: node.dataIndex,
  238. defaultText: node.id
  239. });
  240. rect.disableLabelAnimation = true;
  241. rect.setStyle('fill', node.getVisual('color'));
  242. rect.setStyle('decal', node.getVisual('style').decal);
  243. setStatesStylesFromModel(rect, itemModel);
  244. mainGroup.add(rect);
  245. nodeData.setItemGraphicEl(node.dataIndex, rect);
  246. getECData(rect).dataType = 'node';
  247. var focus = emphasisModel.get('focus');
  248. toggleHoverEmphasis(rect, focus === 'adjacency' ? node.getAdjacentDataIndices() : focus === 'trajectory' ? node.getTrajectoryDataIndices() : focus, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  249. });
  250. nodeData.eachItemGraphicEl(function (el, dataIndex) {
  251. var itemModel = nodeData.getItemModel(dataIndex);
  252. if (itemModel.get('draggable')) {
  253. el.drift = function (dx, dy) {
  254. // sankeyView._focusAdjacencyDisabled = true;
  255. this.shape.x += dx;
  256. this.shape.y += dy;
  257. this.dirty();
  258. api.dispatchAction({
  259. type: 'dragNode',
  260. seriesId: seriesModel.id,
  261. dataIndex: nodeData.getRawIndex(dataIndex),
  262. localX: this.shape.x / width,
  263. localY: this.shape.y / height
  264. });
  265. };
  266. // el.ondragend = function () {
  267. // sankeyView._focusAdjacencyDisabled = false;
  268. // };
  269. el.draggable = true;
  270. el.cursor = 'move';
  271. }
  272. });
  273. if (!this._data && seriesModel.isAnimationEnabled()) {
  274. mainGroup.setClipPath(createGridClipShape(mainGroup.getBoundingRect(), seriesModel, function () {
  275. mainGroup.removeClipPath();
  276. }));
  277. }
  278. this._data = seriesModel.getData();
  279. this._firstRender = false;
  280. };
  281. SankeyView.prototype.__updateOnOwnRoam = function (payload, seriesModel, api) {
  282. applyViewCoordSysTransToElement(this.group, VIEW_COORD_SYS_TRANS_OVERALL, seriesModel.coordinateSystem, null);
  283. };
  284. SankeyView.prototype.dispose = function () {
  285. this._controller && this._controller.dispose();
  286. };
  287. SankeyView.prototype._updateViewCoordSys = function (seriesModel, api) {
  288. var layoutInfo = seriesModel.layoutInfo;
  289. var ownCoordSys = seriesModel.coordinateSystem = createViewCoordSysSimply(seriesModel, api, layoutInfo.x, layoutInfo.y, layoutInfo.width, layoutInfo.height
  290. // 0, 0, layoutInfo.width, layoutInfo.height
  291. );
  292. applyViewCoordSysTransToElement(this.group, VIEW_COORD_SYS_TRANS_OVERALL, ownCoordSys, this._firstRender ? null : seriesModel);
  293. };
  294. SankeyView.type = SERIES_TYPE_SANKEY;
  295. return SankeyView;
  296. }(ChartView);
  297. /**
  298. * Special color, use source node color or target node color
  299. * @param curveProps curve's style to parse
  300. * @param orient direction
  301. * @param edge current curve data
  302. */
  303. function applyCurveStyle(curveProps, orient, edge) {
  304. switch (curveProps.fill) {
  305. case 'source':
  306. curveProps.fill = edge.node1.getVisual('color');
  307. curveProps.decal = edge.node1.getVisual('style').decal;
  308. break;
  309. case 'target':
  310. curveProps.fill = edge.node2.getVisual('color');
  311. curveProps.decal = edge.node2.getVisual('style').decal;
  312. break;
  313. case 'gradient':
  314. var sourceColor = edge.node1.getVisual('color');
  315. var targetColor = edge.node2.getVisual('color');
  316. if (isString(sourceColor) && isString(targetColor)) {
  317. curveProps.fill = new graphic.LinearGradient(0, 0, +(orient === 'horizontal'), +(orient === 'vertical'), [{
  318. color: sourceColor,
  319. offset: 0
  320. }, {
  321. color: targetColor,
  322. offset: 1
  323. }]);
  324. }
  325. }
  326. }
  327. // Add animation to the view
  328. function createGridClipShape(rect, seriesModel, cb) {
  329. var rectEl = new graphic.Rect({
  330. shape: {
  331. x: rect.x - 10,
  332. y: rect.y - 10,
  333. width: 0,
  334. height: rect.height + 20
  335. }
  336. });
  337. graphic.initProps(rectEl, {
  338. shape: {
  339. width: rect.width + 20
  340. }
  341. }, seriesModel, cb);
  342. return rectEl;
  343. }
  344. export default SankeyView;