Skip to content

Commit

Permalink
fix(chart): chart.clear preserve some global options (#5318)
Browse files Browse the repository at this point in the history
  • Loading branch information
pearmini authored Jul 17, 2023
1 parent 3312a06 commit 0c4fc99
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
42 changes: 42 additions & 0 deletions __tests__/unit/api/chart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,48 @@ describe('Chart', () => {
expect(chart.getGroup()).toEqual(null);
});

it('chart.clear() should preserve some global options.', () => {
const globals = {
theme: 'classic',
autoFit: true,
width: 300,
height: 200,
inset: 10,
insetTop: 20,
insetRight: 30,
insetBottom: 40,
insetLeft: 50,
margin: 10,
marginTop: 20,
marginRight: 30,
marginBottom: 40,
padding: 10,
paddingTop: 20,
paddingRight: 30,
paddingBottom: 40,
};

const chart = new Chart(globals);

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

chart.clear();

expect(chart.options()).toStrictEqual({ ...globals, type: 'view' });
});

it('chart.changeData() should update all children data although mark children have their own data', async () => {
const data = [
{ genre: 'Sports', sold: 275 },
Expand Down
11 changes: 10 additions & 1 deletion src/api/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,17 @@ export class Chart extends View<ChartOptions> {
}

private _reset() {
const KEYS = ['theme', 'type', 'width', 'height', 'autoFit'];
this.type = 'view';
this.value = {};
this.value = Object.fromEntries(
Object.entries(this.value).filter(
([key]) =>
key.startsWith('margin') ||
key.startsWith('padding') ||
key.startsWith('inset') ||
KEYS.includes(key),
),
);
this.children = [];
}

Expand Down

0 comments on commit 0c4fc99

Please sign in to comment.