group.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. /**
  3. * Group animation
  4. * @author sima.zhang1990@gmail.com
  5. */
  6. var Util = require('../util/common');
  7. var Shape = require('../graphic/shape');
  8. var Timeline = require('../graphic/animate/timeline');
  9. var Animator = require('../graphic/animate/animator');
  10. var timeline;
  11. Shape.prototype.animate = function () {
  12. var attrs = Util.mix({}, this.get('attrs'));
  13. return new Animator(this, attrs, timeline);
  14. };
  15. var Animate = require('./animate');
  16. var Action = require('./group-action');
  17. Animate.Action = Action;
  18. Animate.defaultCfg = {
  19. line: function line(coord) {
  20. if (coord.isPolar) {
  21. return Action.groupScaleInXY;
  22. }
  23. return Action.groupWaveIn;
  24. },
  25. area: function area(coord) {
  26. if (coord.isPolar) {
  27. return Action.groupScaleInXY;
  28. }
  29. return Action.groupWaveIn;
  30. },
  31. path: function path(coord) {
  32. if (coord.isPolar) {
  33. return Action.groupScaleInXY;
  34. }
  35. return Action.groupWaveIn;
  36. },
  37. point: function point() {
  38. return Action.shapesScaleInXY;
  39. },
  40. interval: function interval(coord) {
  41. var result;
  42. if (coord.isPolar) {
  43. result = Action.groupScaleInXY;
  44. if (coord.transposed) {
  45. result = Action.groupWaveIn;
  46. }
  47. } else {
  48. result = coord.transposed ? Action.groupScaleInX : Action.groupScaleInY;
  49. }
  50. return result;
  51. },
  52. schema: function schema() {
  53. return Action.groupWaveIn;
  54. }
  55. };
  56. function getAnimate(geomType, coord, animationName) {
  57. var result;
  58. if (animationName) {
  59. result = Animate.Action[animationName];
  60. } else {
  61. result = Animate.getAnimation(geomType, coord, 'appear');
  62. }
  63. return result;
  64. }
  65. function getAnimateCfg(geomType, animateCfg) {
  66. var defaultCfg = Animate.getAnimateCfg(geomType, 'appear');
  67. if (animateCfg && animateCfg.appear) {
  68. return Util.deepMix({}, defaultCfg, animateCfg.appear);
  69. }
  70. return defaultCfg;
  71. }
  72. module.exports = {
  73. afterCanvasInit: function afterCanvasInit()
  74. /* chart */
  75. {
  76. timeline = new Timeline();
  77. timeline.play();
  78. },
  79. beforeCanvasDraw: function beforeCanvasDraw(chart) {
  80. if (chart.get('animate') === false) {
  81. return;
  82. }
  83. var geoms = chart.get('geoms');
  84. var coord = chart.get('coord');
  85. var animateCfg;
  86. var animate;
  87. Util.each(geoms, function (geom) {
  88. var type = geom.get('type');
  89. var container = geom.get('container');
  90. if (geom.get('animateCfg') !== false) {
  91. animateCfg = getAnimateCfg(type, geom.get('animateCfg'));
  92. animate = getAnimate(type, coord, animateCfg.animation);
  93. if (Util.isFunction(animate)) {
  94. animate(container, animateCfg);
  95. } else if (Animate.defaultCfg[type]) {
  96. animate = Animate.defaultCfg[type](coord);
  97. var yScale = geom.getYScale();
  98. var zeroY = coord.convertPoint({
  99. x: 0,
  100. y: yScale.scale(geom.getYMinValue())
  101. });
  102. animate && animate(container, animateCfg, coord, zeroY);
  103. }
  104. }
  105. });
  106. },
  107. afterCanvasDestroyed: function afterCanvasDestroyed()
  108. /* chart */
  109. {
  110. timeline.stop();
  111. }
  112. };