SankeyView.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 =
  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. applyCurveStyle(curve.style, orient, edge);
  177. var defaultEdgeLabelText = "" + edgeModel.get('value');
  178. var edgeLabelStateModels = getLabelStatesModels(edgeModel, 'edgeLabel');
  179. setLabelStyle(curve, edgeLabelStateModels, {
  180. labelFetcher: {
  181. getFormattedLabel: function (dataIndex, stateName, dataType, labelDimIndex, formatter, extendParams) {
  182. return seriesModel.getFormattedLabel(dataIndex, stateName, 'edge', labelDimIndex, // ensure edgeLabel formatter is provided
  183. // to prevent the inheritance from `label.formatter` of the series
  184. retrieve3(formatter, edgeLabelStateModels.normal && edgeLabelStateModels.normal.get('formatter'), defaultEdgeLabelText), extendParams);
  185. }
  186. },
  187. labelDataIndex: edge.dataIndex,
  188. defaultText: defaultEdgeLabelText
  189. });
  190. curve.setTextConfig({
  191. position: 'inside'
  192. });
  193. var emphasisModel = edgeModel.getModel('emphasis');
  194. setStatesStylesFromModel(curve, edgeModel, 'lineStyle', function (model) {
  195. var style = model.getItemStyle();
  196. applyCurveStyle(style, orient, edge);
  197. return style;
  198. });
  199. group.add(curve);
  200. edgeData.setItemGraphicEl(edge.dataIndex, curve);
  201. var focus = emphasisModel.get('focus');
  202. toggleHoverEmphasis(curve, focus === 'adjacency' ? edge.getAdjacentDataIndices() : focus === 'trajectory' ? edge.getTrajectoryDataIndices() : focus, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  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. } // Add animation to the view
  303. function createGridClipShape(rect, seriesModel, cb) {
  304. var rectEl = new graphic.Rect({
  305. shape: {
  306. x: rect.x - 10,
  307. y: rect.y - 10,
  308. width: 0,
  309. height: rect.height + 20
  310. }
  311. });
  312. graphic.initProps(rectEl, {
  313. shape: {
  314. width: rect.width + 20
  315. }
  316. }, seriesModel, cb);
  317. return rectEl;
  318. }
  319. export default SankeyView;