If you want a more beautiful animation, you can define your own animation.
Animation Register
Animation register is used to achieve the goal of reusing animation functions. After register, you can use it everywhere.
// require registeration class of animationconstAnimate=require('@antv/f2/lib/animation/animate');/** * @param{String} animationName Animation name, specified by users * @param{Function} animationFun define specific animations **/Animate.registerAnimation('animationName', animationFun); // register a animation called animationName// use the registered animationchart.line().animate({ appear: { animation:'animationName'// the registered name of the animation }})
Details on Custom Animation
F2 provides a complete animation customization mechanism. Users can customize animation behavior for any states of graphic element that supports animation. The states here are the four types of animations: appear, enter, leave, update.
The animations in F2 all act on Shape. The animation is performed by changing shape's attributes frame by frame, take the movement of the circle as example:
The animation is very simple, it moves the circle from point A to point B (the coordinate is {x: 230, y: 50}). We need only to call shape.animate() to specify the final state, i.e. graph attributes, of the shape.
// the initial position of circle on canvas is x: 100, y: 100circle.animate().to({ attrs: { x:230,// final coordinate of x-axis y:50// final coordinate of y-axis },// specify the final state of the circle easing:'linear',// easing function duration:1500// duration of the animation})
shape: The shape instance which animation is acted on.
You can get graph attributes attrs from shape instance and then customize the animation.
The attributes are provided to help users ease the customization:
Attributes
Method To Get
Type
Explanation
attrs
shape.get('attrs')
Object
Get all graph attributes of the shape
className
shape.get('className')
String
Get the type name of the shape
origin
shape.get('origin')
Object
Get the drawing data and corresponding original data record of the shape
index
shape.get('index')
Number
Get the index of the shape, i.e. the order of the data record in data set
In addition, shape.attr(name) can also be used to get graph attribute from shape. More methods of shape are listed in Shape API.
F2 also provides a cacheShape attribute for the shape instance in the update state.This attribute stores the content of the previous state for the shape, so that users can customize the animation for changing. The cacheShape contains the following content:
{ animateCfg: {},// configuration for animation attrs: {},// graph attrs of previous state className:"",// class name of the shape}
animateCfg: Object, configuration for animation
The following attributes are contained in animateCfg:
{ easing:,// easing function duration:,// duration of the animation delay:// delay of the animation}
coord: Coordinate object, represents the current coordinate of the chart. The attributes contained in coordinate object are listed in Coordinate API.
Example
The example below shows how to customize the apear animation for bar chart, online demo here:
const { Chart,Animate,Util,G } =F2;// register an animation called delayScaleInYAnimate.registerAnimation('delayScaleInY',function(shape, animateCfg) {constbox=shape.getBBox(); // get the bounding box of the shapeconstorigin=shape.get('origin'); // get the drawing data of the shapeconstpoints=origin.points; // the points that compose of each columnconstcenterX= (box.minX +box.maxX) /2;let centerY;if (points[0].y - points[1].y <=0) { // when the point is below 0 centerY =box.maxY; } else { centerY =box.minY; }shape.transform([ [ 't', centerX, centerY ], [ 's',1,0.1 ], [ 't',-centerX,-centerY ] ]); // use matrix transformation to change the origin state of the shape. scaleY.constindex=shape.get('index');let delay =animateCfg.delay; // get the animation configurationif (Util.isFunction(delay)) { delay =animateCfg.delay(index); // set delay time according to the index }consteasing=animateCfg.easing; // get the animation configurationconstmatrix=shape.getMatrix(); // get the current matrixconstendMatrix=G.Matrix.transform(matrix, [ [ 't', centerX, centerY ], [ 's',1,10 ], [ 't',-centerX,-centerY ] ]); // the matrix for the final state of the shapeshape.animate().to({ attrs: { matrix: endMatrix }, delay, easing, duration:animateCfg.duration }); // do the animation });constdata= [];for (let i =0; i <50; i++) {data.push({ x: i, y: (Math.sin(i /5) * (i /5-10) + i /6) *5 }); }constchart=newChart({ id:'mountNode', width:375, height:200, pixelRatio:window.devicePixelRatio });chart.axis('x',false);chart.legend(false);chart.source(data);chart.interval().position('x*y').color('y','#4a657a-#308e92-#b1cfa5-#f5d69f-#f5898b-#ef5055').animate({ // customize animation configuration appear: { animation:'delayScaleInY',// use the registered animation name easing:'elasticOut',// easing functiondelay(index) {return index *10; } // delay time for each shape according to index } });chart.render();