SankeyView.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. setLabelStyle(curve, getLabelStatesModels(edgeModel, 'edgeLabel'), {
  199. labelFetcher: seriesModel,
  200. labelDataIndex: edge.dataIndex,
  201. defaultText: "" + edgeModel.get('value')
  202. });
  203. curve.setTextConfig({
  204. position: 'inside'
  205. });
  206. var emphasisModel = edgeModel.getModel('emphasis');
  207. setStatesStylesFromModel(curve, edgeModel, 'lineStyle', function (model) {
  208. return model.getItemStyle();
  209. });
  210. group.add(curve);
  211. edgeData.setItemGraphicEl(edge.dataIndex, curve);
  212. var focus = emphasisModel.get('focus');
  213. toggleHoverEmphasis(curve, focus === 'adjacency' ? edge.getAdjacentDataIndices() : focus, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  214. getECData(curve).dataType = 'edge';
  215. }); // Generate a rect for each node
  216. graph.eachNode(function (node) {
  217. var layout = node.getLayout();
  218. var itemModel = node.getModel();
  219. var dragX = itemModel.get('localX');
  220. var dragY = itemModel.get('localY');
  221. var emphasisModel = itemModel.getModel('emphasis');
  222. var rect = new graphic.Rect({
  223. shape: {
  224. x: dragX != null ? dragX * width : layout.x,
  225. y: dragY != null ? dragY * height : layout.y,
  226. width: layout.dx,
  227. height: layout.dy
  228. },
  229. style: itemModel.getModel('itemStyle').getItemStyle(),
  230. z2: 10
  231. });
  232. setLabelStyle(rect, getLabelStatesModels(itemModel), {
  233. labelFetcher: seriesModel,
  234. labelDataIndex: node.dataIndex,
  235. defaultText: node.id
  236. });
  237. rect.disableLabelAnimation = true;
  238. rect.setStyle('fill', node.getVisual('color'));
  239. rect.setStyle('decal', node.getVisual('style').decal);
  240. setStatesStylesFromModel(rect, itemModel);
  241. group.add(rect);
  242. nodeData.setItemGraphicEl(node.dataIndex, rect);
  243. getECData(rect).dataType = 'node';
  244. var focus = emphasisModel.get('focus');
  245. toggleHoverEmphasis(rect, focus === 'adjacency' ? node.getAdjacentDataIndices() : focus, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  246. });
  247. nodeData.eachItemGraphicEl(function (el, dataIndex) {
  248. var itemModel = nodeData.getItemModel(dataIndex);
  249. if (itemModel.get('draggable')) {
  250. el.drift = function (dx, dy) {
  251. sankeyView._focusAdjacencyDisabled = true;
  252. this.shape.x += dx;
  253. this.shape.y += dy;
  254. this.dirty();
  255. api.dispatchAction({
  256. type: 'dragNode',
  257. seriesId: seriesModel.id,
  258. dataIndex: nodeData.getRawIndex(dataIndex),
  259. localX: this.shape.x / width,
  260. localY: this.shape.y / height
  261. });
  262. };
  263. el.ondragend = function () {
  264. sankeyView._focusAdjacencyDisabled = false;
  265. };
  266. el.draggable = true;
  267. el.cursor = 'move';
  268. }
  269. });
  270. if (!this._data && seriesModel.isAnimationEnabled()) {
  271. group.setClipPath(createGridClipShape(group.getBoundingRect(), seriesModel, function () {
  272. group.removeClipPath();
  273. }));
  274. }
  275. this._data = seriesModel.getData();
  276. };
  277. SankeyView.prototype.dispose = function () {};
  278. SankeyView.type = 'sankey';
  279. return SankeyView;
  280. }(ChartView); // Add animation to the view
  281. function createGridClipShape(rect, seriesModel, cb) {
  282. var rectEl = new graphic.Rect({
  283. shape: {
  284. x: rect.x - 10,
  285. y: rect.y - 10,
  286. width: 0,
  287. height: rect.height + 20
  288. }
  289. });
  290. graphic.initProps(rectEl, {
  291. shape: {
  292. width: rect.width + 20
  293. }
  294. }, seriesModel, cb);
  295. return rectEl;
  296. }
  297. export default SankeyView;