SankeyView.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 borderRadius = itemModel.get(['itemStyle', 'borderRadius']) || 0;
  211. var rect = new graphic.Rect({
  212. shape: {
  213. x: dragX != null ? dragX * width : layout.x,
  214. y: dragY != null ? dragY * height : layout.y,
  215. width: layout.dx,
  216. height: layout.dy,
  217. r: borderRadius
  218. },
  219. style: itemModel.getModel('itemStyle').getItemStyle(),
  220. z2: 10
  221. });
  222. setLabelStyle(rect, getLabelStatesModels(itemModel), {
  223. labelFetcher: {
  224. getFormattedLabel: function (dataIndex, stateName) {
  225. return seriesModel.getFormattedLabel(dataIndex, stateName, 'node');
  226. }
  227. },
  228. labelDataIndex: node.dataIndex,
  229. defaultText: node.id
  230. });
  231. rect.disableLabelAnimation = true;
  232. rect.setStyle('fill', node.getVisual('color'));
  233. rect.setStyle('decal', node.getVisual('style').decal);
  234. setStatesStylesFromModel(rect, itemModel);
  235. group.add(rect);
  236. nodeData.setItemGraphicEl(node.dataIndex, rect);
  237. getECData(rect).dataType = 'node';
  238. var focus = emphasisModel.get('focus');
  239. toggleHoverEmphasis(rect, focus === 'adjacency' ? node.getAdjacentDataIndices() : focus === 'trajectory' ? node.getTrajectoryDataIndices() : focus, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  240. });
  241. nodeData.eachItemGraphicEl(function (el, dataIndex) {
  242. var itemModel = nodeData.getItemModel(dataIndex);
  243. if (itemModel.get('draggable')) {
  244. el.drift = function (dx, dy) {
  245. sankeyView._focusAdjacencyDisabled = true;
  246. this.shape.x += dx;
  247. this.shape.y += dy;
  248. this.dirty();
  249. api.dispatchAction({
  250. type: 'dragNode',
  251. seriesId: seriesModel.id,
  252. dataIndex: nodeData.getRawIndex(dataIndex),
  253. localX: this.shape.x / width,
  254. localY: this.shape.y / height
  255. });
  256. };
  257. el.ondragend = function () {
  258. sankeyView._focusAdjacencyDisabled = false;
  259. };
  260. el.draggable = true;
  261. el.cursor = 'move';
  262. }
  263. });
  264. if (!this._data && seriesModel.isAnimationEnabled()) {
  265. group.setClipPath(createGridClipShape(group.getBoundingRect(), seriesModel, function () {
  266. group.removeClipPath();
  267. }));
  268. }
  269. this._data = seriesModel.getData();
  270. };
  271. SankeyView.prototype.dispose = function () {};
  272. SankeyView.type = 'sankey';
  273. return SankeyView;
  274. }(ChartView);
  275. /**
  276. * Special color, use source node color or target node color
  277. * @param curveProps curve's style to parse
  278. * @param orient direction
  279. * @param edge current curve data
  280. */
  281. function applyCurveStyle(curveProps, orient, edge) {
  282. switch (curveProps.fill) {
  283. case 'source':
  284. curveProps.fill = edge.node1.getVisual('color');
  285. curveProps.decal = edge.node1.getVisual('style').decal;
  286. break;
  287. case 'target':
  288. curveProps.fill = edge.node2.getVisual('color');
  289. curveProps.decal = edge.node2.getVisual('style').decal;
  290. break;
  291. case 'gradient':
  292. var sourceColor = edge.node1.getVisual('color');
  293. var targetColor = edge.node2.getVisual('color');
  294. if (isString(sourceColor) && isString(targetColor)) {
  295. curveProps.fill = new graphic.LinearGradient(0, 0, +(orient === 'horizontal'), +(orient === 'vertical'), [{
  296. color: sourceColor,
  297. offset: 0
  298. }, {
  299. color: targetColor,
  300. offset: 1
  301. }]);
  302. }
  303. }
  304. }
  305. // Add animation to the view
  306. function createGridClipShape(rect, seriesModel, cb) {
  307. var rectEl = new graphic.Rect({
  308. shape: {
  309. x: rect.x - 10,
  310. y: rect.y - 10,
  311. width: 0,
  312. height: rect.height + 20
  313. }
  314. });
  315. graphic.initProps(rectEl, {
  316. shape: {
  317. width: rect.width + 20
  318. }
  319. }, seriesModel, cb);
  320. return rectEl;
  321. }
  322. export default SankeyView;