detail.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.afterCanvasInit = afterCanvasInit;
  4. exports.beforeCanvasDraw = beforeCanvasDraw;
  5. exports.afterCanvasDestroyed = afterCanvasDestroyed;
  6. exports["default"] = void 0;
  7. var _common = require("../util/common");
  8. var _element = _interopRequireDefault(require("../graphic/engine/element"));
  9. var _timeline = _interopRequireDefault(require("../graphic/animate/timeline"));
  10. var _animator = _interopRequireDefault(require("../graphic/animate/animator"));
  11. var _animate2 = _interopRequireDefault(require("./animate"));
  12. var ShapeAction = _interopRequireWildcard(require("./shape-action"));
  13. var GroupAction = _interopRequireWildcard(require("./group-action"));
  14. var _chart = _interopRequireDefault(require("../chart/chart"));
  15. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  16. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  18. /**
  19. * Handle the detail animations
  20. * @author sima.zhang1990@gmail.com
  21. */
  22. var timeline;
  23. _element["default"].prototype.animate = function () {
  24. var attrs = (0, _common.mix)({}, this.get('attrs'));
  25. return new _animator["default"](this, attrs, timeline);
  26. };
  27. _chart["default"].prototype.animate = function (cfg) {
  28. this.set('animate', cfg);
  29. return this;
  30. };
  31. _animate2["default"].Action = ShapeAction;
  32. _animate2["default"].defaultCfg = {
  33. interval: {
  34. enter: function enter(coord) {
  35. if (coord.isPolar && coord.transposed) {
  36. // for pie chart
  37. return function (shape) {
  38. shape.set('zIndex', -1);
  39. var container = shape.get('parent');
  40. container.sort();
  41. };
  42. }
  43. return ShapeAction.fadeIn;
  44. }
  45. },
  46. area: {
  47. enter: function enter(coord) {
  48. if (coord.isPolar) return null;
  49. return ShapeAction.fadeIn;
  50. }
  51. },
  52. line: {
  53. enter: function enter(coord) {
  54. if (coord.isPolar) return null;
  55. return ShapeAction.fadeIn;
  56. }
  57. },
  58. path: {
  59. enter: function enter(coord) {
  60. if (coord.isPolar) return null;
  61. return ShapeAction.fadeIn;
  62. }
  63. }
  64. };
  65. var GROUP_ANIMATION = {
  66. line: function line(coord) {
  67. if (coord.isPolar) {
  68. return GroupAction.groupScaleInXY;
  69. }
  70. return GroupAction.groupWaveIn;
  71. },
  72. area: function area(coord) {
  73. if (coord.isPolar) {
  74. return GroupAction.groupScaleInXY;
  75. }
  76. return GroupAction.groupWaveIn;
  77. },
  78. path: function path(coord) {
  79. if (coord.isPolar) {
  80. return GroupAction.groupScaleInXY;
  81. }
  82. return GroupAction.groupWaveIn;
  83. },
  84. point: function point() {
  85. return GroupAction.shapesScaleInXY;
  86. },
  87. interval: function interval(coord) {
  88. var result;
  89. if (coord.isPolar) {
  90. // polar coodinate
  91. result = GroupAction.groupScaleInXY;
  92. if (coord.transposed) {
  93. // pie chart
  94. result = GroupAction.groupWaveIn;
  95. }
  96. } else {
  97. result = coord.transposed ? GroupAction.groupScaleInX : GroupAction.groupScaleInY;
  98. }
  99. return result;
  100. },
  101. schema: function schema() {
  102. return GroupAction.groupWaveIn;
  103. }
  104. };
  105. function diff(fromAttrs, toAttrs) {
  106. var endState = {};
  107. for (var k in toAttrs) {
  108. if ((0, _common.isNumber)(fromAttrs[k]) && fromAttrs[k] !== toAttrs[k]) {
  109. endState[k] = toAttrs[k];
  110. } else if ((0, _common.isArray)(fromAttrs[k]) && JSON.stringify(fromAttrs[k]) !== JSON.stringify(toAttrs[k])) {
  111. endState[k] = toAttrs[k];
  112. }
  113. }
  114. return endState;
  115. } // Add a unique id identifier to each shape
  116. function _getShapeId(geom, dataObj, geomIdx) {
  117. var type = geom.get('type');
  118. var id = 'geom' + geomIdx + '-' + type;
  119. var xScale = geom.getXScale();
  120. var yScale = geom.getYScale();
  121. var xField = xScale.field || 'x';
  122. var yField = yScale.field || 'y';
  123. var yVal = dataObj[yField];
  124. var xVal;
  125. if (xScale.isIdentity) {
  126. xVal = xScale.value;
  127. } else {
  128. xVal = dataObj[xField];
  129. }
  130. if (type === 'interval' || type === 'schema') {
  131. id += '-' + xVal;
  132. } else if (type === 'line' || type === 'area' || type === 'path') {
  133. id += '-' + type;
  134. } else {
  135. id += xScale.isCategory ? '-' + xVal : '-' + xVal + '-' + yVal;
  136. }
  137. var groupScales = geom._getGroupScales();
  138. (0, _common.each)(groupScales, function (groupScale) {
  139. var field = groupScale.field;
  140. if (groupScale.type !== 'identity') {
  141. id += '-' + dataObj[field];
  142. }
  143. });
  144. return id;
  145. } // get geometry's shapes
  146. function getShapes(geoms, chart, coord) {
  147. var shapes = [];
  148. (0, _common.each)(geoms, function (geom, geomIdx) {
  149. var geomContainer = geom.get('container');
  150. var geomShapes = geomContainer.get('children');
  151. var type = geom.get('type');
  152. var animateCfg = (0, _common.isNil)(geom.get('animateCfg')) ? _getAnimateCfgByShapeType(type, chart) : geom.get('animateCfg');
  153. if (animateCfg !== false) {
  154. (0, _common.each)(geomShapes, function (shape, index) {
  155. if (shape.get('className') === type) {
  156. shape._id = _getShapeId(geom, shape.get('origin')._origin, geomIdx);
  157. shape.set('coord', coord);
  158. shape.set('animateCfg', animateCfg);
  159. shape.set('index', index);
  160. shapes.push(shape);
  161. }
  162. });
  163. }
  164. geom.set('shapes', geomShapes);
  165. });
  166. return shapes;
  167. }
  168. function cache(shapes) {
  169. var rst = {};
  170. for (var i = 0, len = shapes.length; i < len; i++) {
  171. var shape = shapes[i];
  172. if (!shape._id || shape.isClip) continue;
  173. var id = shape._id;
  174. rst[id] = {
  175. _id: id,
  176. type: shape.get('type'),
  177. // the type of shape
  178. attrs: (0, _common.mix)({}, shape._attrs.attrs),
  179. // the graphics attributes of shape
  180. className: shape.get('className'),
  181. geomType: shape.get('className'),
  182. index: shape.get('index'),
  183. coord: shape.get('coord'),
  184. animateCfg: shape.get('animateCfg')
  185. };
  186. }
  187. return rst;
  188. }
  189. function getAnimate(geomType, coord, animationType, animationName) {
  190. var result;
  191. if ((0, _common.isFunction)(animationName)) {
  192. result = animationName;
  193. } else if ((0, _common.isString)(animationName)) {
  194. result = _animate2["default"].Action[animationName];
  195. } else {
  196. result = _animate2["default"].getAnimation(geomType, coord, animationType);
  197. }
  198. return result;
  199. }
  200. function getAnimateCfg(geomType, animationType, animateCfg) {
  201. if (animateCfg === false || (0, _common.isObject)(animateCfg) && animateCfg[animationType] === false) {
  202. return false;
  203. }
  204. var defaultCfg = _animate2["default"].getAnimateCfg(geomType, animationType);
  205. if (animateCfg && animateCfg[animationType]) {
  206. return (0, _common.deepMix)({}, defaultCfg, animateCfg[animationType]);
  207. }
  208. return defaultCfg;
  209. }
  210. function addAnimate(cache, shapes, canvas) {
  211. var animate;
  212. var animateCfg; // the order of animation: leave -> update -> enter
  213. var updateShapes = [];
  214. var newShapes = [];
  215. (0, _common.each)(shapes, function (shape) {
  216. var result = cache[shape._id];
  217. if (!result) {
  218. newShapes.push(shape);
  219. } else {
  220. shape.set('cacheShape', result);
  221. updateShapes.push(shape);
  222. delete cache[shape._id];
  223. }
  224. }); // first do the leave animation
  225. (0, _common.each)(cache, function (deletedShape) {
  226. var className = deletedShape.className,
  227. coord = deletedShape.coord,
  228. _id = deletedShape._id,
  229. attrs = deletedShape.attrs,
  230. index = deletedShape.index,
  231. type = deletedShape.type;
  232. animateCfg = getAnimateCfg(className, 'leave', deletedShape.animateCfg);
  233. if (animateCfg === false) return true;
  234. animate = getAnimate(className, coord, 'leave', animateCfg.animation);
  235. if ((0, _common.isFunction)(animate)) {
  236. var tempShape = canvas.addShape(type, {
  237. attrs: attrs,
  238. index: index,
  239. canvas: canvas,
  240. className: className
  241. });
  242. tempShape._id = _id;
  243. animate(tempShape, animateCfg, coord);
  244. }
  245. }); // then do the update animation
  246. (0, _common.each)(updateShapes, function (updateShape) {
  247. var className = updateShape.get('className');
  248. animateCfg = getAnimateCfg(className, 'update', updateShape.get('animateCfg'));
  249. if (animateCfg === false) return true;
  250. var coord = updateShape.get('coord');
  251. var cacheAttrs = updateShape.get('cacheShape').attrs;
  252. var endState = diff(cacheAttrs, updateShape._attrs.attrs); // 判断如果属性相同的话就不进行变换
  253. if (Object.keys(endState).length) {
  254. animate = getAnimate(className, coord, 'update', animateCfg.animation);
  255. if ((0, _common.isFunction)(animate)) {
  256. animate(updateShape, animateCfg, coord);
  257. } else {
  258. var startState = {};
  259. (0, _common.each)(endState, function (value, key) {
  260. startState[key] = cacheAttrs[key];
  261. });
  262. updateShape.attr(startState);
  263. updateShape.animate().to({
  264. attrs: endState,
  265. duration: animateCfg.duration,
  266. easing: animateCfg.easing,
  267. delay: animateCfg.delay
  268. }).onEnd(function () {
  269. updateShape.set('cacheShape', null);
  270. });
  271. }
  272. }
  273. }); // last, enter animation
  274. (0, _common.each)(newShapes, function (newShape) {
  275. // 新图形元素的进场元素
  276. var className = newShape.get('className');
  277. var coord = newShape.get('coord');
  278. animateCfg = getAnimateCfg(className, 'enter', newShape.get('animateCfg'));
  279. if (animateCfg === false) return true;
  280. animate = getAnimate(className, coord, 'enter', animateCfg.animation);
  281. if ((0, _common.isFunction)(animate)) {
  282. if (className === 'interval' && coord.isPolar && coord.transposed) {
  283. var index = newShape.get('index');
  284. var lastShape = updateShapes[index - 1];
  285. animate(newShape, animateCfg, lastShape);
  286. } else {
  287. animate(newShape, animateCfg, coord);
  288. }
  289. }
  290. });
  291. }
  292. function _getAnimateCfgByShapeType(type, chart) {
  293. if (!type) {
  294. return null;
  295. }
  296. var animateCfg = chart.get('animate');
  297. if (type.indexOf('guide-tag') > -1) {
  298. type = 'guide-tag';
  299. }
  300. if ((0, _common.isObject)(animateCfg)) {
  301. return animateCfg[type];
  302. }
  303. if (animateCfg === false) {
  304. return false;
  305. }
  306. return null;
  307. }
  308. function afterCanvasInit()
  309. /* chart */
  310. {
  311. timeline = new _timeline["default"]();
  312. timeline.play();
  313. }
  314. function beforeCanvasDraw(chart) {
  315. if (chart.get('animate') === false) {
  316. return;
  317. }
  318. var isUpdate = chart.get('isUpdate');
  319. var canvas = chart.get('canvas');
  320. var coord = chart.get('coord');
  321. var geoms = chart.get('geoms');
  322. var caches = canvas.get('caches') || [];
  323. if (caches.length === 0) {
  324. isUpdate = false;
  325. }
  326. var cacheShapes = getShapes(geoms, chart, coord);
  327. var _chart$get = chart.get('axisController'),
  328. frontPlot = _chart$get.frontPlot,
  329. backPlot = _chart$get.backPlot;
  330. var axisShapes = frontPlot.get('children').concat(backPlot.get('children'));
  331. var guideShapes = [];
  332. if (chart.get('guideController')) {
  333. guideShapes = chart.get('guideController').guideShapes;
  334. }
  335. var componentShapes = [];
  336. axisShapes.concat(guideShapes).forEach(function (s) {
  337. var className = s.get('className');
  338. var animateCfg = _getAnimateCfgByShapeType(className, chart);
  339. s.set('coord', coord);
  340. s.set('animateCfg', animateCfg);
  341. componentShapes.push(s);
  342. cacheShapes.push(s);
  343. });
  344. canvas.set('caches', cache(cacheShapes));
  345. if (isUpdate) {
  346. addAnimate(caches, cacheShapes, canvas);
  347. } else {
  348. // do the appear animation
  349. var animateCfg;
  350. var animate;
  351. (0, _common.each)(geoms, function (geom) {
  352. var type = geom.get('type');
  353. var geomCfg = (0, _common.isNil)(geom.get('animateCfg')) ? _getAnimateCfgByShapeType(type, chart) : geom.get('animateCfg');
  354. if (geomCfg !== false) {
  355. animateCfg = getAnimateCfg(type, 'appear', geomCfg);
  356. animate = getAnimate(type, coord, 'appear', animateCfg.animation);
  357. if ((0, _common.isFunction)(animate)) {
  358. var shapes = geom.get('shapes');
  359. (0, _common.each)(shapes, function (shape) {
  360. animate(shape, animateCfg, coord);
  361. });
  362. } else if (GROUP_ANIMATION[type]) {
  363. // do the default animation
  364. animate = GroupAction[animateCfg.animation] || GROUP_ANIMATION[type](coord);
  365. var yScale = geom.getYScale();
  366. var zeroY = coord.convertPoint({
  367. x: 0,
  368. y: yScale.scale(geom.getYMinValue())
  369. });
  370. var container = geom.get('container');
  371. animate && animate(container, animateCfg, coord, zeroY);
  372. }
  373. }
  374. }); // do the animation of components
  375. (0, _common.each)(componentShapes, function (shape) {
  376. var animateCfg = shape.get('animateCfg');
  377. var className = shape.get('className');
  378. if (animateCfg && animateCfg.appear) {
  379. // if user configure
  380. var defaultCfg = _animate2["default"].getAnimateCfg(className, 'appear');
  381. var appearCfg = (0, _common.deepMix)({}, defaultCfg, animateCfg.appear);
  382. var _animate = getAnimate(className, coord, 'appear', appearCfg.animation);
  383. if ((0, _common.isFunction)(_animate)) {
  384. _animate(shape, appearCfg, coord);
  385. }
  386. }
  387. });
  388. }
  389. }
  390. function afterCanvasDestroyed()
  391. /* chart */
  392. {
  393. timeline.stop();
  394. }
  395. var _default = {
  396. afterCanvasInit: afterCanvasInit,
  397. beforeCanvasDraw: beforeCanvasDraw,
  398. afterCanvasDestroyed: afterCanvasDestroyed
  399. };
  400. exports["default"] = _default;