Skip to content

Commit

Permalink
fix: pie click event callback
Browse files Browse the repository at this point in the history
  • Loading branch information
xuying.xu committed Oct 7, 2023
1 parent f76b421 commit e73b00f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
18 changes: 7 additions & 11 deletions packages/f2/src/components/pieLabel/pieLabeView.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import { jsx } from '@antv/f-engine';

export default (props) => {
const {
lineStyle,
anchorStyle,
labels,
label1OffsetY,
label2OffsetY,
triggerRef,
onClick,
} = props;
const { lineStyle, anchorStyle, labels, label1OffsetY, label2OffsetY, onClick } = props;

return (
<group ref={triggerRef}>
<group>
{labels.map((label) => {
const { origin, anchor, side, color, label1, label2, points } = label;
const end = points[points.length - 1];

return (
<group onClick={onClick}>
<group
onClick={(e) => {
onClick(e.target?.config?.data);
}}
>
{/* 锚点 */}
<circle
attrs={{
Expand Down
3 changes: 2 additions & 1 deletion packages/f2/src/components/pieLabel/withPieLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { jsx, Component } from '@antv/f-engine';
import { jsx, Component, Ref } from '@antv/f-engine';
import { deepMix, isFunction } from '@antv/util';
import { ChartChildProps, Point } from '../../chart';

Expand Down Expand Up @@ -78,6 +78,7 @@ export default (View) => {
IProps & ChartChildProps
> {
labels: [];
triggerRef: Ref;
constructor(props) {
super(props);
}
Expand Down
72 changes: 71 additions & 1 deletion packages/f2/test/components/pieLabel/pieLabel.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jsx } from '../../../src';
import { Canvas, Chart } from '../../../src';
import { Interval, PieLabel } from '../../../src/components';
import { createContext, delay } from '../../util';
import { createContext, delay, gestureSimulator } from '../../util';

describe('PieLabel', () => {
it('默认显示', async () => {
Expand Down Expand Up @@ -64,6 +64,76 @@ describe('PieLabel', () => {
expect(context).toMatchImageSnapshot();
});

it('事件回调', async () => {
const onClickCallback = jest.fn();
const context = createContext('默认显示', { width: '300px', height: '150px' });
const data = [
{
amount: 20,
memo: 'Study',
const: 'const',
},
{
amount: 10,
memo: 'Eat',
const: 'const',
},
{
amount: 20,
memo: 'Sports',
const: 'const',
},
{
amount: 10,
memo: 'Other',
const: 'const',
},
];
const { props } = (
<Canvas context={context} animate={false} pixelRatio={1}>
<Chart
data={data}
coord={{
type: 'polar',
transposed: true,
innerRadius: 0.3,
radius: 0.5,
}}
>
<Interval x="const" y="amount" adjust="stack" color="memo" />
<PieLabel
label1={(data) => {
return {
text: data.memo,
};
}}
label2={(data) => {
return {
fill: '#000000',
text: '$' + data.amount.toFixed(2),
};
}}
onClick={(data) => {
onClickCallback(data);
}}
/>
</Chart>
</Canvas>
);

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

await delay(300);
await gestureSimulator(context.canvas, 'click', { x: 266, y: 97 });

expect(onClickCallback.mock.calls[0][0]).toStrictEqual({
amount: 10,
memo: 'Eat',
const: 'const',
});
});

it('左边超过最大显示个数,第四象限显示在第一象限', async () => {
const context = createContext('左边超过最大显示个数,第四象限显示在第一象限', {
width: '300px',
Expand Down

0 comments on commit e73b00f

Please sign in to comment.