Skip to content

Commit

Permalink
feat(guide): 增加 PolylineGuide (#1960)
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue authored May 16, 2024
1 parent ad38d32 commit 6680534
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/f2/src/components/guide/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import RectGuideView from './views/Rect';
import ImageGuideView from './views/Image';
import TagGuideView from './views/Tag';
import LottieGuideView from './views/Lottie';
import PolylineGuideView from './views/Polyline';

const DefaultGuideView = () => null;
const TextGuide = withGuide(TextGuideView);
Expand All @@ -17,6 +18,7 @@ const RectGuide = withGuide(RectGuideView);
const ImageGuide = withGuide(ImageGuideView);
const TagGuide = withGuide(TagGuideView);
const LottieGuide = withGuide(LottieGuideView);
const PolylineGuide = withGuide(PolylineGuideView);

export { GuideProps };

Expand All @@ -32,4 +34,5 @@ export {
ImageGuide,
TagGuide,
LottieGuide,
PolylineGuide,
};
36 changes: 36 additions & 0 deletions packages/f2/src/components/guide/views/Polyline.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { jsx, PolygonStyleProps } from '@antv/f-engine';
import { GuideProps } from '../withGuide';
import { deepMix } from '@antv/util';

export interface LineGuideProps extends GuideProps {
points?: { x: number; y: number }[] | null;
style?: Partial<PolygonStyleProps> | ((record?) => Partial<PolygonStyleProps>);
offsetX?: number | string | (number | string)[];
offsetY?: number | string | (number | string)[];
theme?: any;
}

export default (props: LineGuideProps, context) => {
const { theme = {} } = props;
const { points, style, offsetX, offsetY, animation } = deepMix({ ...theme.polyline }, props);

const checkNaN = points.some((d) => isNaN(d.x) || isNaN(d.y));
if (checkNaN) return;

const offsetXNum = context.px2hd(offsetX);
const offsetYNum = context.px2hd(offsetY);

return (
<group>
<polyline
style={{
points: points.map((point) => {
return [point.x + offsetXNum, point.y + offsetYNum];
}),
...style,
}}
animation={animation}
/>
</group>
);
};
1 change: 1 addition & 0 deletions packages/f2/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
ImageGuide,
TagGuide,
LottieGuide,
PolylineGuide,
} from './guide';
export { default as Tooltip, TooltipProps, withTooltip, TooltipView } from './tooltip';
export { default as Treemap, TreemapProps, withTreemap, TreemapView } from './treemap';
Expand Down
9 changes: 9 additions & 0 deletions packages/f2/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ const guide = {
stroke: '#1890ff',
},
},
polyline: {
style: {
lineWidth: '4px',
lineJoin: 'round',
lineCap: 'round',
},
offsetX: 0,
offsetY: 0,
},
};

const chart = {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions packages/f2/test/components/guide/polyline.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { jsx, Canvas, Chart } from '../../../src';
import { Line, PolylineGuide } from '../../../src/components';
import { createContext, delay } from '../../util';

const data = [
{ genre: 'Sports', sold: 275, type: 'a' },
{ genre: 'Strategy', sold: 115, type: 'a' },
{ genre: 'Action', sold: 120, type: 'a' },
{ genre: 'Shooter', sold: 350, type: 'a' },
{ genre: 'Other', sold: 150, type: 'a' },
];

describe('PolylineGuide ', () => {
it('default', async () => {
const context = createContext();
const { props } = (
<Canvas context={context} pixelRatio={1} animate={false}>
<Chart data={data}>
<Line x="genre" y="sold" />
<PolylineGuide
records={data.slice(1, 4)}
style={{
stroke: 'red',
}}
/>
</Chart>
</Canvas>
);

const chart = new Canvas(props);
await chart.render();

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

it('offset', async () => {
const context = createContext();
const { props } = (
<Canvas context={context} pixelRatio={1} animate={false}>
<Chart data={data}>
<Line x="genre" y="sold" />
<PolylineGuide
records={data.slice(1, 4)}
offsetX="10px"
offsetY="10px"
style={{
stroke: '#000',
}}
/>
</Chart>
</Canvas>
);

const chart = new Canvas(props);
await chart.render();

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

0 comments on commit 6680534

Please sign in to comment.