Skip to content
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
42 changes: 42 additions & 0 deletions packages/vchart/__tests__/unit/core/vchart.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { IAreaSeriesSpec } from '../../../src/series/area/interface';
import type { IPoint } from '../../../src/typings';
import { polarToCartesian } from '@visactor/vutils';
import type { IMarkGraphic } from '../../../src/mark/interface';
import type { BaseEventParams } from '../../../src/event/interface';
import type { IChartSpec } from '../../../src/typings/spec/common';

describe('VChart', () => {
describe('render and update', () => {
Expand Down Expand Up @@ -230,6 +232,46 @@ describe('VChart', () => {
expect(vchart.getChart()?.getAllSeries()[0].getRawData()?.latestData.length).toBe(data.length);
});

it('does not duplicate user event registered before updateSpec initializes chart', () => {
const spec: IBarChartSpec = {
type: 'bar',
direction: 'horizontal',
data: [
{
id: 'barData',
values: [
{ cat: '类目一', value: 80 },
{ cat: '类目二', value: 52 }
]
}
],
yField: 'cat',
xField: 'value'
};
const eventSpy = jest.fn();
const emptySpec: IChartSpec = { type: '' };
const eventParams: BaseEventParams = {
source: 'chart',
model: { type: 'bar' } as unknown as BaseEventParams['model'],
event: {
stopPropagation: jest.fn(),
preventDefault: jest.fn()
} as unknown as BaseEventParams['event'],
item: null as unknown as BaseEventParams['item'],
datum: null as unknown as BaseEventParams['datum']
};

vchart = new VChart(emptySpec, {
renderCanvas: canvasDom,
animation: false
});
vchart.on('click', { source: 'chart', level: 'model' }, eventSpy);
vchart.updateSpecSync(spec);
vchart.event.emit('click', eventParams, 'model');

expect(eventSpy).toBeCalledTimes(1);
});

it('updateViewBox', async () => {
const spec: ICommonChartSpec = {
type: 'common',
Expand Down
14 changes: 9 additions & 5 deletions packages/vchart/src/core/vchart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ export class VChart implements IVChart {
// 设置全局字体
this._setFontFamilyTheme(this.getTheme('fontFamily') as string);
this._initDataSet(this._option.dataSet);
this._autoSize = isTrueBrowseEnv ? (spec.autoFit ?? this._option.autoFit ?? true) : false;
this._autoSize = isTrueBrowseEnv ? spec.autoFit ?? this._option.autoFit ?? true : false;
this._bindResizeEvent();
this._bindViewEvent();
this._initChartPlugin();
Expand Down Expand Up @@ -676,6 +676,8 @@ export class VChart implements IVChart {
}

protected _reCompile(updateResult: IUpdateSpecResult, morphConfig?: IMorphConfig) {
const shouldRestoreUserEvents = updateResult.reMake && !!this._chart;

if (updateResult.reMake) {
this._releaseData();
this._initDataSet();
Expand All @@ -702,7 +704,9 @@ export class VChart implements IVChart {
// chart 内部事件 模块自己必须删除
// 内部模块删除事件时,调用了event Dispatcher.release() 导致用户事件被一起删除
// 外部事件现在需要重新添加
this._userEvents.forEach(e => this._event?.on(e.eType as any, e.query as any, e.handler as any));
if (shouldRestoreUserEvents) {
this._userEvents.forEach(e => this._event?.on(e.eType as any, e.query as any, e.handler as any));
}
} else if (updateResult.reCompile) {
// recompile
// 清除之前的所有 compile 内容
Expand Down Expand Up @@ -1477,8 +1481,8 @@ export class VChart implements IVChart {
isObject(specTheme) && specTheme.type
? specTheme.type
: isObject(optionTheme) && optionTheme.type
? optionTheme.type
: this._currentThemeName
? optionTheme.type
: this._currentThemeName
),
getThemeObject(optionTheme),
getThemeObject(specTheme)
Expand Down Expand Up @@ -1512,7 +1516,7 @@ export class VChart implements IVChart {
}

const lasAutoSize = this._autoSize;
this._autoSize = isTrueBrowser(this._option.mode) ? (this._spec.autoFit ?? this._option.autoFit ?? true) : false;
this._autoSize = isTrueBrowser(this._option.mode) ? this._spec.autoFit ?? this._option.autoFit ?? true : false;
if (this._autoSize !== lasAutoSize) {
resize = true;
}
Expand Down
Loading