Skip to content

Commit

Permalink
perf: reduce duplicated code in core/vchart.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
xile611 committed Dec 24, 2024
1 parent addc471 commit a8aca46
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 43 deletions.
9 changes: 5 additions & 4 deletions packages/vchart/src/chart/base/base-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ export class BaseChart<T extends IChartSpec> extends CompilableBase implements I
}
setLayoutTag(tag: boolean, morphConfig?: IMorphConfig, renderNextTick: boolean = true): boolean {
this._layoutTag = tag;
if (this.getCompiler()?.getVGrammarView()) {
this.getCompiler().getVGrammarView().updateLayoutTag();
tag && renderNextTick && this.getCompiler().renderNextTick(morphConfig);
const compiler = this.getCompiler();

if (compiler?.getVGrammarView()) {
compiler.getVGrammarView().updateLayoutTag();
tag && renderNextTick && compiler.renderNextTick(morphConfig);
}
return this._layoutTag;
}
Expand Down Expand Up @@ -212,7 +214,6 @@ export class BaseChart<T extends IChartSpec> extends CompilableBase implements I
this._createLayout();
// 基于spec 创建元素。
// region
debugger;
transformer.forEachRegionInSpec(this._spec, this._createRegion.bind(this));
// series
transformer.forEachSeriesInSpec(this._spec, this._createSeries.bind(this));
Expand Down
48 changes: 9 additions & 39 deletions packages/vchart/src/core/vchart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1110,9 +1110,9 @@ export class VChart implements IVChart {

const reSize = this._shouldChartResize(lastSpec);
result.reSize = reSize;
this._compiler?.getVGrammarView()?.updateLayoutTag();

if (this._spec.type !== lastSpec.type) {
this._compiler?.getVGrammarView()?.updateLayoutTag();
result.reMake = true;
result.reTransformSpec = true;
result.change = true;
Expand Down Expand Up @@ -1146,25 +1146,7 @@ export class VChart implements IVChart {
forceMerge: boolean = false,
morphConfig?: IMorphConfig
) {
if (!spec || !this._spec) {
return this as unknown as IVChart;
}
if (isString(spec)) {
spec = JSON.parse(spec);
}

if (!isFunction(filter)) {
// find spec and update
mergeSpecWithFilter(this._spec, filter, spec, forceMerge);
}

if (this._chart) {
const model = this._chart.getModelInFilter(filter);
if (model) {
return this._updateModelSpec(model, spec, false, forceMerge, morphConfig);
}
}
return this as unknown as IVChart;
return this.updateModelSpecSync(filter, spec, forceMerge, morphConfig);
}

/**
Expand Down Expand Up @@ -1553,16 +1535,7 @@ export class VChart implements IVChart {
* @returns
*/
async setCurrentTheme(name: string) {
if (!ThemeManager.themeExist(name)) {
return this as unknown as IVChart;
}
const result = this._setCurrentTheme(name);
this._setFontFamilyTheme(this._currentTheme?.fontFamily as string);
await this.updateCustomConfigAndRerender(result, false, {
transformSpec: false,
actionSource: 'setCurrentTheme'
});
return this as unknown as IVChart;
return this.setCurrentThemeSync(name);
}

/**
Expand Down Expand Up @@ -2126,14 +2099,7 @@ export class VChart implements IVChart {
* @since 1.11.10
*/
geoZoomByIndex(regionIndex: number = 0, zoom: number, center?: { x: number; y: number }) {
const region = this._chart?.getRegionsInQuerier({ regionIndex })[0];
const geoCoordinates = this._chart?.getComponentsByType(
ComponentTypeEnum.geoCoordinate
) as unknown as IGeoCoordinate[];
const coord = geoCoordinates?.find(coord => coord.getRegions()?.includes(region));
if (coord) {
coord.dispatchZoom(zoom, center);
}
this._geoZoomByQuery({ regionIndex }, zoom, center);
}

/**
Expand All @@ -2144,7 +2110,11 @@ export class VChart implements IVChart {
* @since 1.11.10
*/
geoZoomById(regionId: string | number, zoom: number, center?: { x: number; y: number }) {
const region = this._chart?.getRegionsInQuerier({ regionId })[0];
this._geoZoomByQuery({ regionId }, zoom, center);
}

_geoZoomByQuery(query: MaybeArray<IRegionQuerier>, zoom: number, center?: { x: number; y: number }) {
const region = this._chart?.getRegionsInQuerier(query)[0];
const geoCoordinates = this._chart?.getComponentsByType(
ComponentTypeEnum.geoCoordinate
) as unknown as IGeoCoordinate[];
Expand Down

0 comments on commit a8aca46

Please sign in to comment.