Skip to content

Commit

Permalink
feat: 增加Pictorial组件 (#1974)
Browse files Browse the repository at this point in the history
* feat: 增加Pictorial组件

* fix: 修改api

---------

Co-authored-by: xuying.xu <xuying.xu@alibaba-inc.com>
  • Loading branch information
tangying1027 and xuying.xu committed May 30, 2024
1 parent d715549 commit 24f5944
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/f2/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export {
withCandlestick,
CandlestickView,
} from './candlestick';
export { default as Pictorial, PictorialProps } from './pictorial';
4 changes: 4 additions & 0 deletions packages/f2/src/components/pictorial/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Pictorial, { PictorialProps } from './pictorial';

export { PictorialProps, Pictorial };
export default Pictorial;
35 changes: 35 additions & 0 deletions packages/f2/src/components/pictorial/pictorial.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { jsx } from '@antv/f-engine';
import { GeometryProps } from '../geometry';
import { withInterval } from '../interval';
import { DataRecord } from '../../chart/Data';
export interface PictorialProps<TRecord extends DataRecord = DataRecord>
extends GeometryProps<TRecord> {
symbol?: any;
}

export default class Pictorial extends withInterval({}) {
props: PictorialProps;

render() {
const { props, context } = this;
const { px2hd } = context;
const { symbol: Symbol } = px2hd(props);
const records = this.mapping();

return (
<group>
{records.map((record) => {
const { key, children } = record;
return (
<group key={key}>
{children.map((item) => {
const { xMax, xMin, yMax, yMin } = item;
return <Symbol {...item} width={xMax - xMin} height={yMax - yMin} px2hd={px2hd} />;
})}
</group>
);
})}
</group>
);
}
}
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.
124 changes: 124 additions & 0 deletions packages/f2/test/components/pictorial/basic.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { jsx } from '../../../src';
import { Canvas, Chart, Pictorial, Axis, Tooltip } from '../../../src';
import { createContext, delay, gestureSimulator } from '../../util';
const context = createContext();

const data = [
{
x: '产品1',
value: 4927,
},
{
x: '产品2',
value: 11607,
},
];

describe('pictorial', () => {
it('image symbol', async () => {
const { props } = (
<Canvas context={context} pixelRatio={1}>
<Chart data={data}>
<Axis field="value" min={0}></Axis>

<Pictorial
x="x"
y="value"
symbol={({ xMin, xMax, yMin, yMax, px2hd }) => (
<group>
<image
style={{
x: xMin,
y: yMax - px2hd('20px') / 2,
width: xMax - xMin,
height: '20px',
src:
'https://gw.alipayobjects.com/zos/finxbff/compress-tinypng/76LdyFixxEmUqrGG6rmCG/tuoyuanxingbeifen_32.png',
}}
/>
<image
style={{
x: xMin,
y: yMin,
width: xMax - xMin,
height: yMax - yMin,
src:
'https://gw.alipayobjects.com/zos/finxbff/compress-tinypng/mNyB6MXFwnxLMwzfEWHYt/juxingbeifen_6.png',
}}
/>
<image
style={{
x: xMin,
y: yMin - px2hd('20px') / 2,
width: xMax - xMin,
height: '20px',
src:
'https://gw.alipayobjects.com/zos/finxbff/compress-tinypng/VV9WVNGexcXLVYpQxjBFH/tuoyuanxingbeifen_13.png',
}}
/>
</group>
)}
/>
</Chart>
</Canvas>
);

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

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

it('ellipse symbol', async () => {
const { props } = (
<Canvas context={context} pixelRatio={1}>
<Chart data={data}>
<Axis field="value" min={0}></Axis>
<Pictorial
x="x"
y="value"
symbol={({ xMin, xMax, yMin, yMax, width, height, origin }) => (
<group>
<ellipse
style={{
cx: xMin + width / 2,
cy: yMax,
rx: width / 2,
ry: '20px',
fill: 'l(90) 0:#1f7eff 1:#64adff',
}}
/>
<rect
style={{
x: xMin,
y: yMin,
width,
height,
fill: 'l(90) 0:#9cc1ff 1:#ecf5ff',
fillOpacity: 0.9,
}}
/>
<ellipse
style={{
cx: xMin + width / 2,
cy: yMin,
rx: width / 2,
ry: '20px',
fill: 'l(90) 0:#1f7eff 1:#64adff',
}}
/>
</group>
)}
/>
</Chart>
</Canvas>
);

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

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

0 comments on commit 24f5944

Please sign in to comment.