SankeyView.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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, retrieve3 } from 'zrender/lib/core/util.js';
  47. var SankeyPathShape = /** @class */function () {
  48. function SankeyPathShape() {
  49. this.x1 = 0;
  50. this.y1 = 0;
  51. this.x2 = 0;
  52. this.y2 = 0;
  53. this.cpx1 = 0;
  54. this.cpy1 = 0;
  55. this.cpx2 = 0;
  56. this.cpy2 = 0;
  57. this.extent = 0;
  58. }
  59. return SankeyPathShape;
  60. }();
  61. var SankeyPath = /** @class */function (_super) {
  62. __extends(SankeyPath, _super);
  63. function SankeyPath(opts) {
  64. return _super.call(this, opts) || this;
  65. }
  66. SankeyPath.prototype.getDefaultShape = function () {
  67. return new SankeyPathShape();
  68. };
  69. SankeyPath.prototype.buildPath = function (ctx, shape) {
  70. var extent = shape.extent;
  71. ctx.moveTo(shape.x1, shape.y1);
  72. ctx.bezierCurveTo(shape.cpx1, shape.cpy1, shape.cpx2, shape.cpy2, shape.x2, shape.y2);
  73. if (shape.orient === 'vertical') {
  74. ctx.lineTo(shape.x2 + extent, shape.y2);
  75. ctx.bezierCurveTo(shape.cpx2 + extent, shape.cpy2, shape.cpx1 + extent, shape.cpy1, shape.x1 + extent, shape.y1);
  76. } else {
  77. ctx.lineTo(shape.x2, shape.y2 + extent);
  78. ctx.bezierCurveTo(shape.cpx2, shape.cpy2 + extent, shape.cpx1, shape.cpy1 + extent, shape.x1, shape.y1 + extent);
  79. }
  80. ctx.closePath();
  81. };
  82. SankeyPath.prototype.highlight = function () {
  83. enterEmphasis(this);
  84. };
  85. SankeyPath.prototype.downplay = function () {
  86. leaveEmphasis(this);
  87. };
  88. return SankeyPath;
  89. }(graphic.Path);
  90. var SankeyView = /** @class */function (_super) {
  91. __extends(SankeyView, _super);
  92. function SankeyView() {
  93. var _this = _super !== null && _super.apply(this, arguments) || this;
  94. _this.type = SankeyView.type;
  95. _this._focusAdjacencyDisabled = false;
  96. return _this;
  97. }
  98. SankeyView.prototype.render = function (seriesModel, ecModel, api) {
  99. var sankeyView = this;
  100. var graph = seriesModel.getGraph();
  101. var group = this.group;
  102. var layoutInfo = seriesModel.layoutInfo;
  103. // view width
  104. var width = layoutInfo.width;
  105. // view height
  106. var height = layoutInfo.height;
  107. var nodeData = seriesModel.getData();
  108. var edgeData = seriesModel.getData('edge');
  109. var orient = seriesModel.get('orient');
  110. this._model = seriesModel;
  111. group.removeAll();
  112. group.x = layoutInfo.x;
  113. group.y = layoutInfo.y;
  114. // generate a bezire Curve for each edge
  115. graph.eachEdge(function (edge) {
  116. var curve = new SankeyPath();
  117. var ecData = getECData(curve);
  118. ecData.dataIndex = edge.dataIndex;
  119. ecData.seriesIndex = seriesModel.seriesIndex;
  120. ecData.dataType = 'edge';
  121. var edgeModel = edge.getModel();
  122. var lineStyleModel = edgeModel.getModel('lineStyle');
  123. var curvature = lineStyleModel.get('curveness');
  124. var n1Layout = edge.node1.getLayout();
  125. var node1Model = edge.node1.getModel();
  126. var dragX1 = node1Model.get('localX');
  127. var dragY1 = node1Model.get('localY');
  128. var n2Layout = edge.node2.getLayout();
  129. var node2Model = edge.node2.getModel();
  130. var dragX2 = node2Model.get('localX');
  131. var dragY2 = node2Model.get('localY');
  132. var edgeLayout = edge.getLayout();
  133. var x1;
  134. var y1;
  135. var x2;
  136. var y2;
  137. var cpx1;
  138. var cpy1;
  139. var cpx2;
  140. var cpy2;
  141. curve.shape.extent = Math.max(1, edgeLayout.dy);
  142. curve.shape.orient = orient;
  143. if (orient === 'vertical') {
  144. x1 = (dragX1 != null ? dragX1 * width : n1Layout.x) + edgeLayout.sy;
  145. y1 = (dragY1 != null ? dragY1 * height : n1Layout.y) + n1Layout.dy;
  146. x2 = (dragX2 != null ? dragX2 * width : n2Layout.x) + edgeLayout.ty;
  147. y2 = dragY2 != null ? dragY2 * height : n2Layout.y;
  148. cpx1 = x1;
  149. cpy1 = y1 * (1 - curvature) + y2 * curvature;
  150. cpx2 = x2;
  151. cpy2 = y1 * curvature + y2 * (1 - curvature);
  152. } else {
  153. x1 = (dragX1 != null ? dragX1 * width : n1Layout.x) + n1Layout.dx;
  154. y1 = (dragY1 != null ? dragY1 * height : n1Layout.y) + edgeLayout.sy;
  155. x2 = dragX2 != null ? dragX2 * width : n2Layout.x;
  156. y2 = (dragY2 != null ? dragY2 * height : n2Layout.y) + edgeLayout.ty;
  157. cpx1 = x1 * (1 - curvature) + x2 * curvature;
  158. cpy1 = y1;
  159. cpx2 = x1 * curvature + x2 * (1 - curvature);
  160. cpy2 = y2;
  161. }
  162. curve.setShape({
  163. x1: x1,
  164. y1: y1,
  165. x2: x2,
  166. y2: y2,
  167. cpx1: cpx1,
  168. cpy1: cpy1,
  169. cpx2: cpx2,
  170. cpy2: cpy2
  171. });
  172. curve.useStyle(lineStyleModel.getItemStyle());
  173. // Special color, use source node color or target node color
  174. applyCurveStyle(curve.style, orient, edge);
  175. var defaultEdgeLabelText = "" + edgeModel.get('value');
  176. var edgeLabelStateModels = getLabelStatesModels(edgeModel, 'edgeLabel');
  177. setLabelStyle(curve, edgeLabelStateModels, {
  178. labelFetcher: {
  179. getFormattedLabel: function (dataIndex, stateName, dataType, labelDimIndex, formatter, extendParams) {
  180. return seriesModel.getFormattedLabel(dataIndex, stateName, 'edge', labelDimIndex,
  181. // ensure edgeLabel formatter is provided
  182. // to prevent the inheritance from `label.formatter` of the series
  183. retrieve3(formatter, edgeLabelStateModels.normal && edgeLabelStateModels.normal.get('formatter'), defaultEdgeLabelText), extendParams);
  184. }
  185. },
  186. labelDataIndex: edge.dataIndex,
  187. defaultText: defaultEdgeLabelText
  188. });
  189. curve.setTextConfig({
  190. position: 'inside'
  191. });
  192. var emphasisModel = edgeModel.getModel('emphasis');
  193. setStatesStylesFromModel(curve, edgeModel, 'lineStyle', function (model) {
  194. var style = model.getItemStyle();
  195. applyCurveStyle(style, orient, edge);
  196. return style;
  197. });
  198. group.add(curve);
  199. edgeData.setItemGraphicEl(edge.dataIndex, curve);
  200. var focus = emphasisModel.get('focus');
  201. toggleHoverEmphasis(curve, focus === 'adjacency' ? edge.getAdjacentDataIndices() : focus === 'trajectory' ? edge.getTrajectoryDataIndices() : focus, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  202. });
  203. // Generate a rect for each node
  204. graph.eachNode(function (node) {
  205. var layout = node.getLayout();
  206. var itemModel = node.getModel();
  207. var dragX = itemModel.get('localX');
  208. var dragY = itemModel.get('localY');
  209. var emphasisModel = itemModel.getModel('emphasis');
  210. var rect = new graphic.Rect({
  211. shape: {
  212. x: dragX != null ? dragX * width : layout.x,
  213. y: dragY != null ? dragY * height : layout.y,
  214. width: layout.dx,
  215. height: layout.dy
  216. },
  217. style: itemModel.getModel('itemStyle').getItemStyle(),
  218. z2: 10
  219. });
  220. setLabelStyle(rect, getLabelStatesModels(itemModel), {
  221. labelFetcher: {
  222. getFormattedLabel: function (dataIndex, stateName) {
  223. return seriesModel.getFormattedLabel(dataIndex, stateName, 'node');
  224. }
  225. },
  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 === 'trajectory' ? node.getTrajectoryDataIndices() : 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);
  273. /**
  274. * Special color, use source node color or target node color
  275. * @param curveProps curve's style to parse
  276. * @param orient direction
  277. * @param edge current curve data
  278. */
  279. function applyCurveStyle(curveProps, orient, edge) {
  280. switch (curveProps.fill) {
  281. case 'source':
  282. curveProps.fill = edge.node1.getVisual('color');
  283. curveProps.decal = edge.node1.getVisual('style').decal;
  284. break;
  285. case 'target':
  286. curveProps.fill = edge.node2.getVisual('color');
  287. curveProps.decal = edge.node2.getVisual('style').decal;
  288. break;
  289. case 'gradient':
  290. var sourceColor = edge.node1.getVisual('color');
  291. var targetColor = edge.node2.getVisual('color');
  292. if (isString(sourceColor) && isString(targetColor)) {
  293. curveProps.fill = new graphic.LinearGradient(0, 0, +(orient === 'horizontal'), +(orient === 'vertical'), [{
  294. color: sourceColor,
  295. offset: 0
  296. }, {
  297. color: targetColor,
  298. offset: 1
  299. }]);
  300. }
  301. }
  302. }
  303. // Add animation to the view
  304. function createGridClipShape(rect, seriesModel, cb) {
  305. var rectEl = new graphic.Rect({
  306. shape: {
  307. x: rect.x - 10,
  308. y: rect.y - 10,
  309. width: 0,
  310. height: rect.height + 20
  311. }
  312. });
  313. graphic.initProps(rectEl, {
  314. shape: {
  315. width: rect.width + 20
  316. }
  317. }, seriesModel, cb);
  318. return rectEl;
  319. }
  320. export default SankeyView;