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(chart): chart.forceFit rerender only when size changing #5052

Merged
merged 1 commit into from
May 18, 2023
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
37 changes: 37 additions & 0 deletions __tests__/unit/api/chart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,4 +681,41 @@ describe('Chart', () => {
await chart.render();
expect(chart['_hasBindAutoFit']).toBe(false);
});

it('chart.forceFit() should be not rerender if size of container do not change.', async () => {
const div = document.createElement('div');
div.style.width = '500px';
div.style.height = '400px';

const chart = new Chart({
theme: 'classic',
container: div,
autoFit: true,
});

chart
.interval()
.data([
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
])
.encode('x', 'genre')
.encode('y', 'sold')
.encode('color', 'genre');

// Track chart render;
const fn = jest.fn();
const render = chart.render.bind(chart);
chart.render = () => {
fn();
return render();
};
await chart.render();

await chart.forceFit();
expect(fn).toBeCalledTimes(1);
});
});
9 changes: 7 additions & 2 deletions src/api/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,14 @@ export class Chart extends View<ChartOptions> {
}

forceFit() {
// Don't fit if size do not change.
this.options['autoFit'] = true;
const { width, height } = sizeOf(this.options(), this._container);
if (width === this._width && height === this._height) {
return Promise.resolve(this);
}

// Don't call changeSize to prevent update width and height of options.
// @ts-ignore
this.options.autoFit = true;
this.emit(ChartEvent.BEFORE_CHANGE_SIZE);
const finished = this.render();
finished.then(() => {
Expand Down