diff --git a/packages/f2/src/components/guide/views/Image.tsx b/packages/f2/src/components/guide/views/Image.tsx index 028a64d65..bc5f6c5c8 100644 --- a/packages/f2/src/components/guide/views/Image.tsx +++ b/packages/f2/src/components/guide/views/Image.tsx @@ -1,5 +1,5 @@ import { jsx, ImageStyleProps } from '@antv/f-engine'; -import { deepMix } from '@antv/util'; +import { deepMix, isNumber } from '@antv/util'; import { GuideProps } from '../withGuide'; export interface ImageGuideProps extends GuideProps { @@ -11,7 +11,7 @@ export interface ImageGuideProps extends GuideProps { offsetY?: number | string; } -const defaultProps: Omit = { +const defaultProps: Omit = { offsetX: 0, offsetY: 0, points: [], @@ -22,11 +22,12 @@ export default (props: ImageGuideProps, context) => { const cfg = deepMix({}, defaultProps, props); const { points, style, attrs, offsetX, offsetY, src, animation } = cfg; const { x, y } = points[0] || {}; - if(isNaN(x) || isNaN(y)) return null; - + if (isNaN(x) || isNaN(y)) return null; + const { height = 0, width = 0 } = { ...attrs, ...style }; - const heightNum = context.px2hd(height + 'px'); - const widthNum = context.px2hd(width + 'px'); + + const heightNum = isNumber(height) ? context.px2hd(height + 'px') : context.px2hd(height); + const widthNum = isNumber(width) ? context.px2hd(width + 'px') : context.px2hd(width); const offsetXNum = context.px2hd(offsetX); const offsetYNum = context.px2hd(offsetY); diff --git a/packages/f2/src/components/guide/views/Rect.tsx b/packages/f2/src/components/guide/views/Rect.tsx index ed18174f9..f96bd6b11 100644 --- a/packages/f2/src/components/guide/views/Rect.tsx +++ b/packages/f2/src/components/guide/views/Rect.tsx @@ -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 ( = { +const defaultProps: Omit = { offsetX: 0, offsetY: 0, points: [], @@ -54,7 +54,7 @@ const defaultStyle = { }, }; -const Label = ({ content, background, textStyle }) => { +const Label = ({ content, background, textStyle, animation = {} }) => { return ( { radius: defaultStyle.container.radius, ...background, }} + animation={animation} > { fill: defaultStyle.text.fill, ...textStyle, }} + animation={animation} /> ); @@ -93,10 +95,11 @@ export default class Tag extends Component { canvasHeight, background, textStyle, + animation, } = px2hd(cfg); const { x, y } = points[0] || {}; - if(isNaN(x) || isNaN(y)) return null; - + if (isNaN(x) || isNaN(y)) return null; + const offsetXNum = context.px2hd(offsetX); const offsetYNum = context.px2hd(offsetY); let posX = x + (offsetXNum || 0); @@ -220,12 +223,18 @@ export default class Tag extends Component { y: posY, }} > - ); diff --git "a/packages/f2/test/components/guide/__image_snapshots__/guide-test-tsx-guide-image-guide\346\224\257\346\214\201px\344\274\240\345\205\245-1-snap.png" "b/packages/f2/test/components/guide/__image_snapshots__/guide-test-tsx-guide-image-guide\346\224\257\346\214\201px\344\274\240\345\205\245-1-snap.png" new file mode 100644 index 000000000..4bb6c0881 Binary files /dev/null and "b/packages/f2/test/components/guide/__image_snapshots__/guide-test-tsx-guide-image-guide\346\224\257\346\214\201px\344\274\240\345\205\245-1-snap.png" differ diff --git a/packages/f2/test/components/guide/__image_snapshots__/guide-test-tsx-guide-rect-guide-1-snap.png b/packages/f2/test/components/guide/__image_snapshots__/guide-test-tsx-guide-rect-guide-1-snap.png new file mode 100644 index 000000000..b0194fcfd Binary files /dev/null and b/packages/f2/test/components/guide/__image_snapshots__/guide-test-tsx-guide-rect-guide-1-snap.png differ diff --git a/packages/f2/test/components/guide/guide.test.tsx b/packages/f2/test/components/guide/guide.test.tsx index e19760aff..afad0e818 100644 --- a/packages/f2/test/components/guide/guide.test.tsx +++ b/packages/f2/test/components/guide/guide.test.tsx @@ -5,6 +5,7 @@ import { LineGuide, PointGuide, TextGuide, + RectGuide, withGuide, LottieGuide, withLegend, @@ -131,6 +132,56 @@ describe('Guide ', () => { expect(context).toMatchImageSnapshot(); }); + it('image guide支持px传入', async () => { + const context = createContext(); + const { props } = ( + + + + + {data.map((item) => { + const { sold } = item; + return ( + { + return { + height: 24, + width: 24, + }; + }} + /> + ); + })} + + {/* 图片Guide */} + {data.map((item, key) => { + return ( + { + return { + height: '24px', + width: '24px', + }; + }} + offsetX="0px" + offsetY="-24px" + /> + ); + })} + + + ); + const chart = new Canvas(props); + await chart.render(); + + await delay(500); + expect(context).toMatchImageSnapshot(); + }); + it('point', async () => { const context = createContext(); const { props } = ( @@ -189,13 +240,37 @@ describe('Guide ', () => { }); it('tag', () => {}); - it('guide 超出范围', async() => { + it('rect guide', async () => { const context = createContext(); const { props } = ( - + + + + ); + const chart = new Canvas(props); + chart.render(); + await delay(50); + expect(context).toMatchImageSnapshot(); + }); + it('guide 超出范围', async () => { + const context = createContext(); + const { props } = ( + + + + );