From 0c4fc99cfa54bf56328f4a6eebc3619534414b35 Mon Sep 17 00:00:00 2001 From: MiniPear Date: Mon, 17 Jul 2023 15:44:48 +0800 Subject: [PATCH] fix(chart): chart.clear preserve some global options (#5318) --- __tests__/unit/api/chart.spec.ts | 42 ++++++++++++++++++++++++++++++++ src/api/chart.ts | 11 ++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/__tests__/unit/api/chart.spec.ts b/__tests__/unit/api/chart.spec.ts index 9ebb3bbf9c..463459487e 100644 --- a/__tests__/unit/api/chart.spec.ts +++ b/__tests__/unit/api/chart.spec.ts @@ -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 }, diff --git a/src/api/chart.ts b/src/api/chart.ts index 156d810f46..463ae47423 100644 --- a/src/api/chart.ts +++ b/src/api/chart.ts @@ -231,8 +231,17 @@ export class Chart extends View { } 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 = []; }