1. enter: {
  2. animation: 'myAnimation',
  3. }
  4. });

1分钟上手自定义动画

  • 首先获取每根柱子的包围盒。包围盒的意思是能包围图形的最小矩形,所以包围盒含有 minX、minY、maxX、maxY 四个属性,这四个参数限定的区间即为图形的包围盒。
  • 计算变换中点。当顶点在零点之上时,延 Y 轴正向放大,所以变换的中点在包围盒底部中间;当顶点在零点之下时,延 Y 轴反向放大。所以变换的中点在包围盒顶部中间;
  • 设置动画初始状态。要实现柱子的放大,需要先将柱子缩到最小。调用 shape 的 transform() 方法,先将坐标系的原点移到变换中点,然后将柱子的 y 值缩小到 0.1 倍,最后将坐标系的原点移到原处。
  • 实现延迟放大效果的动画。调用 shape 的 animate() 方法,传入变换的结束状态、动画时间和缓动函数。结束状态中可以配置延迟参数 delay ,给每个 shape 的动画添加一个跟序号成正比的延迟,即可实现依次放大的效果。
  1. // shape 是柱子对象,animateCfg 是动画配置
  2. var animateFun = function(shape, animateCfg) {
  3. var box = shape.getBBox(); // 获取柱子包围盒
  4. var origin = shape.get('origin'); // 获取柱子原始数据
  5. var points = origin.points; // 获取柱子顶点
  6. var i = origin.index; // 获取柱子序列号
  7. // 计算柱子的变换中点
  8. var centerX = (box.minX + box.maxX) / 2;
  9. var centerY;
  10. centerY = box.maxY;
  11. } else {
  12. centerY = box.minY;
  13. }
  14. // 设置初始态
  15. ['t', -centerX, -centerY],
  16. ['s', 1, 0.1],
  17. ['t', centerX, centerY]
  18. ]);
  19. // 设置动画目标态
  20. shape.animate({
  21. transform: [
  22. ['t', -centerX, -centerY],
  23. ['s', 1, 10],
  24. ['t', centerX, centerY]
  25. ],
  26. delay: animateCfg.delay // 延迟参数
  27. }, animateCfg.duartion, animateCfg.easing);
  28. };
  1. var data = [];
  2. for (var i = 0; i < 50; i++) {
  3. x: i,
  4. y: (Math.sin(i / 5) * (i / 5 -10) + i / 6) * 5
  5. });
  6. var chart = new G2.Chart({
  7. id: 'c1',
  8. width: 800,
  9. height: 500
  10. });
  11. chart.axis('x', false);
  12. chart.legend(false);
  13. chart.source(data);
  14. chart.interval()
  15. .position('x*y')
  16. .color('y', '#4a657a-#308e92-#b1cfa5-#f5d69f-#f5898b-#ef5055')
  17. .animate({
  18. appear:{
  19. animation: 'delayScaleInY',
  20. easing: 'easeOutElastic',
  21. delay:function(index){
  22. return index * 20;
  23. }
  24. }
  25. });