Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Guide 支持动画callback配置 #1370

Merged
merged 5 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/f2/src/components/guide/views/Arc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ArcGuideProps = {

export default (props: ArcGuideProps) => {
const { theme = {} } = props;
const { coord, points, style } = deepMix({ ...theme.line }, props);
const { coord, points, style, animation } = deepMix({ ...theme.line }, props);

const start = points[0] || {};
const end = points[1] || {};
Expand All @@ -35,6 +35,7 @@ export default (props: ArcGuideProps) => {
endAngle,
...style,
}}
animation={animation}
/>
</group>
);
Expand Down
20 changes: 12 additions & 8 deletions packages/f2/src/components/guide/views/Image.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { jsx } from '../../../jsx';
import { Style } from '../../../types';
import { deepMix } from '@antv/util';

type ImageGuideProps = {
src: string;
Expand All @@ -22,8 +23,8 @@ const baseAttrs = {
};

export default (props: ImageGuideProps) => {
const cfg = { ...defaultProps, ...props };
const { points, style, attrs, offsetX, offsetY, src } = cfg;
const cfg = deepMix({}, defaultProps, props);
const { points, style, attrs, offsetX, offsetY, src, animation } = cfg;
const { x, y } = points[0] || {};
const { height = 0, width = 0 } = attrs;
const posX = x + (offsetX || 0) - height / 2;
Expand All @@ -41,13 +42,16 @@ export default (props: ImageGuideProps) => {
y: posY,
src,
}}
animation={{
update: {
easing: 'linear',
duration: 450,
property: ['x', 'y'],
animation={deepMix(
{
update: {
easing: 'linear',
duration: 450,
property: ['x', 'y'],
},
},
}}
animation
)}
/>
</group>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/f2/src/components/guide/views/Line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type LineGuideProps = {

export default (props: LineGuideProps) => {
const { theme = {} } = props;
const { points, style, offsetX, offsetY } = deepMix({ ...theme.line }, props);
const { points, style, offsetX, offsetY, animation } = deepMix({ ...theme.line }, props);
const { x: x1, y: y1 } = points[0] || {};
const { x: x2, y: y2 } = points[1] || {};

Expand All @@ -32,6 +32,7 @@ export default (props: LineGuideProps) => {
y2: posY2,
...style,
}}
animation={animation}
/>
</group>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/f2/src/components/guide/views/Point.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type PointGuideProps = {

export default (props: PointGuideProps) => {
const { theme } = props;
const { points, style, offsetX, offsetY } = deepMix({ ...theme.point }, props);
const { points, style, offsetX, offsetY, animation } = deepMix({ ...theme.point }, props);
const { x, y } = points[0] || {};

const posX = x + (offsetX || 0);
Expand All @@ -26,6 +26,7 @@ export default (props: PointGuideProps) => {
y: posY,
...style,
}}
animation={animation}
/>
</group>
);
Expand Down
3 changes: 2 additions & 1 deletion packages/f2/src/components/guide/views/Rect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type RectGuideProps = {

export default (props: RectGuideProps) => {
const { theme = {} } = props;
const { points, style } = deepMix({ ...theme.rect }, props);
const { points, style, animation } = deepMix({ ...theme.rect }, props);

const start = points[0] || {};
const end = points[1] || {};
Expand All @@ -25,6 +25,7 @@ export default (props: RectGuideProps) => {
height: Math.abs(start.y - end.y),
...style,
}}
animation={animation}
/>
</group>
);
Expand Down
11 changes: 9 additions & 2 deletions packages/f2/src/components/guide/withGuide.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jsx } from '../../jsx';
import Component from '../../base/component';
import { isString, isNil } from '@antv/util';
import { isString, isNil, isFunction } from '@antv/util';
import { Ref } from '../../types';
import Chart from '../../chart';
import { renderShape } from '../../base/diff';
Expand Down Expand Up @@ -112,12 +112,18 @@ export default (View) => {

render() {
const { props, context } = this;
const { coord, records = [] } = props;
const { coord, records = [], animation, chart } = props;
const { width, height } = context;
const points = this.convertPoints(records);
const theme = this.getGuideTheme();
const { guideWidth, guideHeight, guideBBox } = this.state;

let animationCfg = animation;
if (isFunction(animation)) {
// 透传绘制关键点和chart实例
animationCfg = animation(points, chart);
}

return (
<View
triggerRef={this.triggerRef}
Expand All @@ -130,6 +136,7 @@ export default (View) => {
guideWidth={guideWidth}
guideHeight={guideHeight}
guideBBox={guideBBox}
animation={animationCfg}
/>
);
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 64 additions & 2 deletions packages/f2/test/components/guide/guide.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
TextGuide,
LineGuide,
} from '../../../src/components';
import { Canvas, Chart } from '../../../src';
import { Canvas, Chart, Interval } from '../../../src';
import { createContext, delay } from '../../util';
import imageBianzu from './images/bianzu';

Expand Down Expand Up @@ -75,7 +75,7 @@ describe('Guide ', () => {
// 10个图例 和1条线
expect(container._attrs.children[0]._attrs.children.length).toBe(11);

await delay(50);
await delay(250);
expect(context).toMatchImageSnapshot();
});

Expand Down Expand Up @@ -218,4 +218,66 @@ describe('Guide ', () => {
await delay(50);
expect(context).toMatchImageSnapshot();
});

it('TextGuide 动画 - 支持callback配置', async () => {
const context = createContext();
const { props } = (
<Canvas context={context} pixelRatio={1}>
<Chart
data={data}
coord={{
transposed: true,
}}
>
<Interval
x="genre"
y="sold"
color="type"
animation={{
appear: {
duration: 1000,
easing: 'quinticIn',
property: ['width'],
},
}}
/>

{data.map((item) => {
const { sold } = item;
return (
<TextGuide
records={[item]}
onClick={(ev) => {
console.log('ev: ', ev.points);
}}
content={`${sold}`}
attrs={{
fill: '#000',
fontSize: '24px',
}}
animation={(points, props) => {

return {
appear: {
easing: 'quinticIn',
duration: 1000,
property: ['x'],
start: {
x: props.coord.left,
},
},
};
}}
/>
);
})}
</Chart>
</Canvas>
);
const chart = new Canvas(props);
chart.render();

await delay(1100);
expect(context).toMatchImageSnapshot();
});
});