Skip to content

Commit

Permalink
feat: rect gudie 增加offsetXoffsetY
Browse files Browse the repository at this point in the history
  • Loading branch information
xuying.xu committed May 16, 2024
1 parent df8b666 commit c3de434
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/f2/src/components/guide/views/Rect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ export interface RectGuideProps extends GuideProps {
theme?: any;
}

export default (props: RectGuideProps) => {
export default (props: RectGuideProps, context) => {
const { theme = {} } = props;
const { points, style, animation } = deepMix({ ...theme.rect }, props);
const checkNaN = points.some((d)=> isNaN(d.x) || isNaN(d.y));
if(checkNaN) return null;
const { points, style, animation, offsetX, offsetY } = deepMix({ ...theme.rect }, props);
const checkNaN = points.some((d) => isNaN(d.x) || isNaN(d.y));
if (checkNaN) return null;

const start = points[0] || {};
const end = points[1] || {};

const offsetXNum = context.px2hd(offsetX);
const offsetYNum = context.px2hd(offsetY);
const posX = Math.min(start.x, end.x) + (offsetXNum || 0);
const posY = Math.min(start.y, end.y) + (offsetYNum || 0);

return (
<group>
<rect
style={{
x: Math.min(start.x, end.x),
y: Math.min(start.y, end.y),
x: posX,
y: posY,
width: Math.abs(end.x - start.x),
height: Math.abs(start.y - end.y),
...style,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions packages/f2/test/components/guide/guide.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
LineGuide,
PointGuide,
TextGuide,
RectGuide,
withGuide,
LottieGuide,
withLegend,
Expand Down Expand Up @@ -239,6 +240,26 @@ describe('Guide ', () => {
});
it('tag', () => {});

it('rect guide', async () => {
const context = createContext();
const { props } = (
<Canvas context={context} pixelRatio={1} animate={false}>
<Chart data={data}>
<Line x="genre" y="sold" color="type" />
<RectGuide
records={[data[0], data[1]]}
style={{ fill: 'yellow', fillOpacity: 0.5 }}
offsetX="-24px"
offsetY="24px"
/>
</Chart>
</Canvas>
);
const chart = new Canvas(props);
chart.render();
await delay(50);
expect(context).toMatchImageSnapshot();
});
it('guide 超出范围', async () => {
const context = createContext();
const { props } = (
Expand Down

0 comments on commit c3de434

Please sign in to comment.