SankeyView.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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';
  42. import { enterEmphasis, leaveEmphasis, enableHoverEmphasis, setStatesStylesFromModel } from '../../util/states';
  43. import ChartView from '../../view/Chart';
  44. import { setLabelStyle, getLabelStatesModels } from '../../label/labelStyle';
  45. import { getECData } from '../../util/innerStore';
  46. var SankeyPathShape =
  47. /** @class */
  48. function () {
  49. function SankeyPathShape() {
  50. this.x1 = 0;
  51. this.y1 = 0;
  52. this.x2 = 0;
  53. this.y2 = 0;
  54. this.cpx1 = 0;
  55. this.cpy1 = 0;
  56. this.cpx2 = 0;
  57. this.cpy2 = 0;
  58. this.extent = 0;
  59. }
  60. return SankeyPathShape;
  61. }();
  62. var SankeyPath =
  63. /** @class */
  64. function (_super) {
  65. __extends(SankeyPath, _super);
  66. function SankeyPath(opts) {
  67. return _super.call(this, opts) || this;
  68. }
  69. SankeyPath.prototype.getDefaultShape = function () {
  70. return new SankeyPathShape();
  71. };
  72. SankeyPath.prototype.buildPath = function (ctx, shape) {
  73. var extent = shape.extent;
  74. ctx.moveTo(shape.x1, shape.y1);
  75. ctx.bezierCurveTo(shape.cpx1, shape.cpy1, shape.cpx2, shape.cpy2, shape.x2, shape.y2);
  76. if (shape.orient === 'vertical') {
  77. ctx.lineTo(shape.x2 + extent, shape.y2);
  78. ctx.bezierCurveTo(shape.cpx2 + extent, shape.cpy2, shape.cpx1 + extent, shape.cpy1, shape.x1 + extent, shape.y1);
  79. } else {
  80. ctx.lineTo(shape.x2, shape.y2 + extent);
  81. ctx.bezierCurveTo(shape.cpx2, shape.cpy2 + extent, shape.cpx1, shape.cpy1 + extent, shape.x1, shape.y1 + extent);
  82. }
  83. ctx.closePath();
  84. };
  85. SankeyPath.prototype.highlight = function () {
  86. enterEmphasis(this);
  87. };
  88. SankeyPath.prototype.downplay = function () {
  89. leaveEmphasis(this);
  90. };
  91. return SankeyPath;
  92. }(graphic.Path);
  93. var SankeyView =
  94. /** @class */
  95. function (_super) {
  96. __extends(SankeyView, _super);
  97. function SankeyView() {
  98. var _this = _super !== null && _super.apply(this, arguments) || this;
  99. _this.type = SankeyView.type;
  100. _this._focusAdjacencyDisabled = false;
  101. return _this;
  102. }
  103. SankeyView.prototype.render = function (seriesModel, ecModel, api) {
  104. var sankeyView = this;
  105. var graph = seriesModel.getGraph();
  106. var group = this.group;
  107. var layoutInfo = seriesModel.layoutInfo; // view width
  108. var width = layoutInfo.width; // view height
  109. var height = layoutInfo.height;
  110. var nodeData = seriesModel.getData();
  111. var edgeData = seriesModel.getData('edge');
  112. var orient = seriesModel.get('orient');
  113. this._model = seriesModel;
  114. group.removeAll();
  115. group.x = layoutInfo.x;
  116. group.y = layoutInfo.y; // generate a bezire Curve for each edge
  117. graph.eachEdge(function (edge) {
  118. var curve = new SankeyPath();
  119. var ecData = getECData(curve);
  120. ecData.dataIndex = edge.dataIndex;
  121. ecData.seriesIndex = seriesModel.seriesIndex;
  122. ecData.dataType = 'edge';
  123. var edgeModel = edge.getModel();
  124. var lineStyleModel = edgeModel.getModel('lineStyle');
  125. var curvature = lineStyleModel.get('curveness');
  126. var n1Layout = edge.node1.getLayout();
  127. var node1Model = edge.node1.getModel();
  128. var dragX1 = node1Model.get('localX');
  129. var dragY1 = node1Model.get('localY');
  130. var n2Layout = edge.node2.getLayout();
  131. var node2Model = edge.node2.getModel();
  132. var dragX2 = node2Model.get('localX');
  133. var dragY2 = node2Model.get('localY');
  134. var edgeLayout = edge.getLayout();
  135. var x1;
  136. var y1;
  137. var x2;
  138. var y2;
  139. var cpx1;
  140. var cpy1;
  141. var cpx2;
  142. var cpy2;
  143. curve.shape.extent = Math.max(1, edgeLayout.dy);
  144. curve.shape.orient = orient;
  145. if (orient === 'vertical') {
  146. x1 = (dragX1 != null ? dragX1 * width : n1Layout.x) + edgeLayout.sy;
  147. y1 = (dragY1 != null ? dragY1 * height : n1Layout.y) + n1Layout.dy;
  148. x2 = (dragX2 != null ? dragX2 * width : n2Layout.x) + edgeLayout.ty;
  149. y2 = dragY2 != null ? dragY2 * height : n2Layout.y;
  150. cpx1 = x1;
  151. cpy1 = y1 * (1 - curvature) + y2 * curvature;
  152. cpx2 = x2;
  153. cpy2 = y1 * curvature + y2 * (1 - curvature);
  154. } else {
  155. x1 = (dragX1 != null ? dragX1 * width : n1Layout.x) + n1Layout.dx;
  156. y1 = (dragY1 != null ? dragY1 * height : n1Layout.y) + edgeLayout.sy;
  157. x2 = dragX2 != null ? dragX2 * width : n2Layout.x;
  158. y2 = (dragY2 != null ? dragY2 * height : n2Layout.y) + edgeLayout.ty;
  159. cpx1 = x1 * (1 - curvature) + x2 * curvature;
  160. cpy1 = y1;
  161. cpx2 = x1 * curvature + x2 * (1 - curvature);
  162. cpy2 = y2;
  163. }
  164. curve.setShape({
  165. x1: x1,
  166. y1: y1,
  167. x2: x2,
  168. y2: y2,
  169. cpx1: cpx1,
  170. cpy1: cpy1,
  171. cpx2: cpx2,
  172. cpy2: cpy2
  173. });
  174. curve.useStyle(lineStyleModel.getItemStyle()); // Special color, use source node color or target node color
  175. switch (curve.style.fill) {
  176. case 'source':
  177. curve.style.fill = edge.node1.getVisual('color');
  178. curve.style.decal = edge.node1.getVisual('style').decal;
  179. break;
  180. case 'target':
  181. curve.style.fill = edge.node2.getVisual('color');
  182. curve.style.decal = edge.node2.getVisual('style').decal;
  183. break;
  184. case 'gradient':
  185. var sourceColor = edge.node1.getVisual('color');
  186. var targetColor = edge.node2.getVisual('color');
  187. if (typeof sourceColor === 'string' && typeof targetColor === 'string') {
  188. curve.style.fill = new graphic.LinearGradient(0, 0, +(orient === 'horizontal'), +(orient === 'vertical'), [{
  189. color: sourceColor,
  190. offset: 0
  191. }, {
  192. color: targetColor,
  193. offset: 1
  194. }]);
  195. }
  196. }
  197. var emphasisModel = edgeModel.getModel('emphasis');
  198. setStatesStylesFromModel(curve, edgeModel, 'lineStyle', function (model) {
  199. return model.getItemStyle();
  200. });
  201. group.add(curve);
  202. edgeData.setItemGraphicEl(edge.dataIndex, curve);
  203. var focus = emphasisModel.get('focus');
  204. enableHoverEmphasis(curve, focus === 'adjacency' ? edge.getAdjacentDataIndices() : focus, emphasisModel.get('blurScope'));
  205. getECData(curve).dataType = 'edge';
  206. }); // Generate a rect for each node
  207. graph.eachNode(function (node) {
  208. var layout = node.getLayout();
  209. var itemModel = node.getModel();
  210. var dragX = itemModel.get('localX');
  211. var dragY = itemModel.get('localY');
  212. var emphasisModel = itemModel.getModel('emphasis');
  213. var rect = new graphic.Rect({
  214. shape: {
  215. x: dragX != null ? dragX * width : layout.x,
  216. y: dragY != null ? dragY * height : layout.y,
  217. width: layout.dx,
  218. height: layout.dy
  219. },
  220. style: itemModel.getModel('itemStyle').getItemStyle(),
  221. z2: 10
  222. });
  223. setLabelStyle(rect, getLabelStatesModels(itemModel), {
  224. labelFetcher: seriesModel,
  225. labelDataIndex: node.dataIndex,
  226. defaultText: node.id
  227. });
  228. rect.disableLabelAnimation = true;
  229. rect.setStyle('fill', node.getVisual('color'));
  230. rect.setStyle('decal', node.getVisual('style').decal);
  231. setStatesStylesFromModel(rect, itemModel);
  232. group.add(rect);
  233. nodeData.setItemGraphicEl(node.dataIndex, rect);
  234. getECData(rect).dataType = 'node';
  235. var focus = emphasisModel.get('focus');
  236. enableHoverEmphasis(rect, focus === 'adjacency' ? node.getAdjacentDataIndices() : focus, emphasisModel.get('blurScope'));
  237. });
  238. nodeData.eachItemGraphicEl(function (el, dataIndex) {
  239. var itemModel = nodeData.getItemModel(dataIndex);
  240. if (itemModel.get('draggable')) {
  241. el.drift = function (dx, dy) {
  242. sankeyView._focusAdjacencyDisabled = true;
  243. this.shape.x += dx;
  244. this.shape.y += dy;
  245. this.dirty();
  246. api.dispatchAction({
  247. type: 'dragNode',
  248. seriesId: seriesModel.id,
  249. dataIndex: nodeData.getRawIndex(dataIndex),
  250. localX: this.shape.x / width,
  251. localY: this.shape.y / height
  252. });
  253. };
  254. el.ondragend = function () {
  255. sankeyView._focusAdjacencyDisabled = false;
  256. };
  257. el.draggable = true;
  258. el.cursor = 'move';
  259. }
  260. });
  261. if (!this._data && seriesModel.isAnimationEnabled()) {
  262. group.setClipPath(createGridClipShape(group.getBoundingRect(), seriesModel, function () {
  263. group.removeClipPath();
  264. }));
  265. }
  266. this._data = seriesModel.getData();
  267. };
  268. SankeyView.prototype.dispose = function () {};
  269. SankeyView.type = 'sankey';
  270. return SankeyView;
  271. }(ChartView); // Add animation to the view
  272. function createGridClipShape(rect, seriesModel, cb) {
  273. var rectEl = new graphic.Rect({
  274. shape: {
  275. x: rect.x - 10,
  276. y: rect.y - 10,
  277. width: 0,
  278. height: rect.height + 20
  279. }
  280. });
  281. graphic.initProps(rectEl, {
  282. shape: {
  283. width: rect.width + 20
  284. }
  285. }, seriesModel, cb);
  286. return rectEl;
  287. }
  288. export default SankeyView;