Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 数据改变时图例未更新 #1436

Merged
merged 1 commit into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions packages/f2/src/components/legend/withLegend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,8 @@ export default (View) => {

getItems() {
const { props, state } = this;
const { items, filtered } = state;
const renderItems = items?.length
? items
: props.items?.length
? props.items
: this.getOriginItems();
const { filtered } = state;
const renderItems = props.items?.length ? props.items : this.getOriginItems();
if (!renderItems) return null;
return renderItems.map((item) => {
const { tickValue } = item;
Expand Down Expand Up @@ -280,12 +276,6 @@ export default (View) => {
});
}

setItems(items) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个为啥去掉了?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getItems 现在 props 变化时没有更新

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你可以看下我写那个单测,之前的问题是 data 变化时,对应的图例没有更新

this.setState({
items,
});
}

render() {
const { props, state } = this;
const items = this.getItems();
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.
97 changes: 97 additions & 0 deletions packages/f2/test/components/legend/legend.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,101 @@ describe('图例', () => {
expect(context).toMatchImageSnapshot();
});
});

it('图例数据更新', async () => {
const data = [
{
name: '芳华',
percent: 0.4,
a: '1',
},
{
name: '妖猫传',
percent: 0.2,
a: '1',
},
{
name: '机器之血',
percent: 0.18,
a: '1',
},
{
name: '心理罪',
percent: 0.15,
a: '1',
},
{
name: '寻梦环游记',
percent: 0.05,
a: '1',
},
{
name: '其他',
percent: 0.02,
a: '1',
},
];
const data1 = [
{
name: '妖猫传',
percent: 0.4,
a: '1',
},
{
name: '芳华',
percent: 0.2,
a: '1',
},
{
name: '机器之血',
percent: 0.18,
a: '1',
},
{
name: '心理罪',
percent: 0.15,
a: '1',
},
{
name: '寻梦环游记',
percent: 0.05,
a: '1',
},
{
name: '其他',
percent: 0.02,
a: '1',
},
];
const context = createContext('图例数据更新');

const getProps = (data) => {
const { props } = (
<Canvas context={context} pixelRatio={1}>
<Chart
data={data}
coord={{
type: 'polar',
transposed: true,
}}
scale={{}}
>
<Interval x="a" y="percent" adjust="stack" color="name" animate={false} />
<Legend />
</Chart>
</Canvas>
);
return props;
};

const props = getProps(data);
const canvas = new Canvas(props);
canvas.render();
await delay(200);
expect(context).toMatchImageSnapshot();

const updateProps = getProps(data1);
canvas.update(updateProps);
expect(context).toMatchImageSnapshot();
});
});