From 14b7bc912da55ff0610a6cec72fe366ac9e349cd Mon Sep 17 00:00:00 2001 From: Howard Zhang Date: Thu, 19 Oct 2023 05:20:52 +0800 Subject: [PATCH] fix: unit test problems --- packages/vchart/__tests__/unit/chart/bar.test.ts | 5 +---- .../vchart/__tests__/unit/chart/histogram.test.ts | 5 +---- packages/vchart/__tests__/unit/chart/line.test.ts | 9 +++------ .../__tests__/unit/chart/linearProgress.test.ts | 5 +---- .../__tests__/unit/chart/rangeColumn.test.ts | 5 ++--- .../vchart/__tests__/unit/chart/treemap.test.ts | 5 ++--- .../__tests__/unit/chart/word-cloud.test.ts | 7 +++---- .../component/cartesian/axis/linear-axis.test.ts | 4 +--- .../vchart/__tests__/unit/data/fields.test.ts | 9 +++------ .../vchart/__tests__/unit/morph/morph.test.ts | 7 ++----- .../__tests__/unit/scale/global-scale.test.ts | 11 ++++------- packages/vchart/__tests__/unit/theme/line.test.ts | 6 ++---- packages/vchart/src/chart/base-chart.ts | 15 ++++++++------- .../vchart/src/component/tooltip/handler/base.ts | 10 ++++++++-- packages/vchart/src/model/base-model.ts | 5 +++-- packages/vchart/src/theme/builtin/index.ts | 8 ++++++++ packages/vchart/src/theme/color-scheme/util.ts | 2 +- packages/vchart/src/theme/util.ts | 8 +++++++- packages/vchart/src/util/spec/merge-theme.ts | 2 +- packages/vchart/src/util/spec/preprocess.ts | 5 ++++- 20 files changed, 65 insertions(+), 68 deletions(-) diff --git a/packages/vchart/__tests__/unit/chart/bar.test.ts b/packages/vchart/__tests__/unit/chart/bar.test.ts index 71bfd468a1..725377ad20 100644 --- a/packages/vchart/__tests__/unit/chart/bar.test.ts +++ b/packages/vchart/__tests__/unit/chart/bar.test.ts @@ -4,8 +4,6 @@ import { GlobalScale } from '../../../src/scale/global-scale'; import { EventDispatcher } from '../../../src/event/event-dispatcher'; import type { BarSeries, IChartSpec } from '../../../src'; // eslint-disable-next-line no-duplicate-imports -import { ThemeManager } from '../../../src'; -// eslint-disable-next-line no-duplicate-imports import { BarChart } from '../../../src'; import { DataSet } from '@visactor/vdataset'; import { createCanvas, removeDom } from '../../util/dom'; @@ -117,8 +115,7 @@ describe('Bar chart test', () => { } } as any; }, - globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), - getTheme: () => ThemeManager.getCurrentTheme() + globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any) } as any ); chart.created(); diff --git a/packages/vchart/__tests__/unit/chart/histogram.test.ts b/packages/vchart/__tests__/unit/chart/histogram.test.ts index 2ba6e61d28..69bbba2a87 100644 --- a/packages/vchart/__tests__/unit/chart/histogram.test.ts +++ b/packages/vchart/__tests__/unit/chart/histogram.test.ts @@ -1,8 +1,6 @@ import { EventDispatcher } from '../../../src/event/event-dispatcher'; import type { BarSeries } from '../../../src'; // eslint-disable-next-line no-duplicate-imports -import { ThemeManager } from '../../../src'; -// eslint-disable-next-line no-duplicate-imports import { HistogramChart } from '../../../src'; import { DataSet, DataView, csvParser, dataViewParser } from '@visactor/vdataset'; import { createCanvas, removeDom } from '../../util/dom'; @@ -63,8 +61,7 @@ describe('histogram chart test', () => { container: null, mode: 'desktop-browser', getCompiler: getTestCompiler, - globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), - getTheme: () => ThemeManager.getCurrentTheme() + globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any) } as any); chart.created(); chart.init(); diff --git a/packages/vchart/__tests__/unit/chart/line.test.ts b/packages/vchart/__tests__/unit/chart/line.test.ts index 19f1bedcd0..a1ff69212e 100644 --- a/packages/vchart/__tests__/unit/chart/line.test.ts +++ b/packages/vchart/__tests__/unit/chart/line.test.ts @@ -1,15 +1,14 @@ import type { ILineChartSpec } from '../../../src/chart/line/interface'; import { GlobalScale } from '../../../src/scale/global-scale'; -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-nocheck import type { LineSeries } from '../../../src/series/line/line'; import { DataSet, DataView, csvParser } from '@visactor/vdataset'; import { LineChart } from '../../../src/chart/line/line'; import { EventDispatcher } from '../../../src/event/event-dispatcher'; import { getTestCompiler } from '../../util/factory/compiler'; -import { ThemeManager } from '../../../src'; import { initChartDataSet } from '../../util/context'; +import VChart from '../../../src'; +const VChartClass = VChart; // 确保引用 vchart 以确保注册所需的图表 const dataSet = new DataSet(); initChartDataSet(dataSet); dataSet.registerParser('csv', csvParser); @@ -48,7 +47,6 @@ describe('line chart test', () => { mode: 'desktop-browser', getCompiler: getTestCompiler, globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), - getTheme: () => ThemeManager.getCurrentTheme(), onError: () => {} } as any); chart.created(); @@ -120,8 +118,7 @@ describe('line chart test', () => { container: null, mode: 'mobile-browser', getCompiler: getTestCompiler, - globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), - getTheme: () => ThemeManager.getCurrentTheme() + globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any) } as any ); chart.created(); diff --git a/packages/vchart/__tests__/unit/chart/linearProgress.test.ts b/packages/vchart/__tests__/unit/chart/linearProgress.test.ts index c2a2a0a6c5..563d9fd1ad 100644 --- a/packages/vchart/__tests__/unit/chart/linearProgress.test.ts +++ b/packages/vchart/__tests__/unit/chart/linearProgress.test.ts @@ -1,9 +1,7 @@ -import { DataSet, csvParser, dataViewParser } from '@visactor/vdataset'; +import { DataSet, csvParser } from '@visactor/vdataset'; import { EventDispatcher } from '../../../src/event/event-dispatcher'; import type { LinearProgressSeries } from '../../../src'; // eslint-disable-next-line no-duplicate-imports -import { ThemeManager } from '../../../src'; -// eslint-disable-next-line no-duplicate-imports import { LinearProgressChart } from '../../../src'; import { getTestCompiler } from '../../util/factory/compiler'; import { GlobalScale } from '../../../src/scale/global-scale'; @@ -52,7 +50,6 @@ describe('linearProgress chart test', () => { mode: 'desktop-browser', getCompiler: getTestCompiler, globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), - getTheme: () => ThemeManager.getCurrentTheme(), animation: false } as any); chart.created(); diff --git a/packages/vchart/__tests__/unit/chart/rangeColumn.test.ts b/packages/vchart/__tests__/unit/chart/rangeColumn.test.ts index 896ee77d56..7f7e5289c5 100644 --- a/packages/vchart/__tests__/unit/chart/rangeColumn.test.ts +++ b/packages/vchart/__tests__/unit/chart/rangeColumn.test.ts @@ -1,5 +1,5 @@ -import { DataSet, DataView, csvParser, dataViewParser } from '@visactor/vdataset'; -import { RangeColumnChart, ThemeManager } from '../../../src'; +import { DataSet, DataView, csvParser } from '@visactor/vdataset'; +import { RangeColumnChart } from '../../../src'; import type { RangeColumnSeries } from '../../../src/series/range-column/range-column'; import { EventDispatcher } from '../../../src/event/event-dispatcher'; import { getTestCompiler } from '../../util/factory/compiler'; @@ -63,7 +63,6 @@ describe('rangeColumn chart test', () => { mode: 'desktop-browser', getCompiler: getTestCompiler, globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), - getTheme: () => ThemeManager.getCurrentTheme(), animation: false } as any); chart.created(); diff --git a/packages/vchart/__tests__/unit/chart/treemap.test.ts b/packages/vchart/__tests__/unit/chart/treemap.test.ts index 9704ae4f38..2606154e0b 100644 --- a/packages/vchart/__tests__/unit/chart/treemap.test.ts +++ b/packages/vchart/__tests__/unit/chart/treemap.test.ts @@ -2,8 +2,8 @@ import { GlobalScale } from '../../../src/scale/global-scale'; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-nocheck import { EventDispatcher } from '../../../src/event/event-dispatcher'; -import { ThemeManager, default as VChart } from '../../../src'; -import { DataSet, csvParser, dataViewParser } from '@visactor/vdataset'; +import { default as VChart } from '../../../src'; +import { DataSet, csvParser } from '@visactor/vdataset'; import { createCanvas, removeDom } from '../../util/dom'; import { getTestCompiler } from '../../util/factory/compiler'; import { disk } from '../../data/disk'; @@ -66,7 +66,6 @@ describe('treemap chart test', () => { mode: 'desktop-browser', getCompiler: getTestCompiler, globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), - getTheme: () => ThemeManager.getCurrentTheme(), onError: () => {} } as any); chart.created(); diff --git a/packages/vchart/__tests__/unit/chart/word-cloud.test.ts b/packages/vchart/__tests__/unit/chart/word-cloud.test.ts index 2af11d5790..a259308a73 100644 --- a/packages/vchart/__tests__/unit/chart/word-cloud.test.ts +++ b/packages/vchart/__tests__/unit/chart/word-cloud.test.ts @@ -4,8 +4,8 @@ import { GlobalScale } from '../../../src/scale/global-scale'; import { EventDispatcher } from '../../../src/event/event-dispatcher'; import type { IWordCloudChartSpec } from '../../../src'; // eslint-disable-next-line no-duplicate-imports -import { ThemeManager, default as VChart } from '../../../src'; -import { DataSet, csvParser, dataViewParser } from '@visactor/vdataset'; +import { default as VChart } from '../../../src'; +import { DataSet, csvParser } from '@visactor/vdataset'; import { WordCloudChart } from '../../../src/chart/word-cloud/word-cloud'; import type { WordCloudSeries } from '../../../src/series/word-cloud/word-cloud'; import { dataWordCloud } from '../../data/data-wordcloud'; @@ -73,8 +73,7 @@ describe('wordCloud chart test', () => { container: null, mode: 'desktop-browser', getCompiler: getTestCompiler, - globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), - getTheme: () => ThemeManager.getCurrentTheme() + globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any) } as any); chart.created(); chart.init(); diff --git a/packages/vchart/__tests__/unit/component/cartesian/axis/linear-axis.test.ts b/packages/vchart/__tests__/unit/component/cartesian/axis/linear-axis.test.ts index e4ab243da1..6971b70800 100644 --- a/packages/vchart/__tests__/unit/component/cartesian/axis/linear-axis.test.ts +++ b/packages/vchart/__tests__/unit/component/cartesian/axis/linear-axis.test.ts @@ -2,14 +2,13 @@ import { GlobalScale } from '../../../../../src/scale/global-scale'; import type { IEventDispatcher } from '../../../../../src/event/interface'; /* eslint-disable no-lone-blocks */ /* eslint-disable @typescript-eslint/ban-ts-comment */ -import { DataSet, csvParser, dataViewParser } from '@visactor/vdataset'; +import { DataSet, csvParser } from '@visactor/vdataset'; import { dimensionStatistics } from '../../../../../src/data/transforms/dimension-statistics'; import type { CartesianLinearAxis } from '../../../../../src/index'; // eslint-disable-next-line no-duplicate-imports import { CartesianAxis } from '../../../../../src/index'; import type { IComponent, IComponentOption } from '../../../../../src/component/interface'; import { EventDispatcher } from '../../../../../src/event/event-dispatcher'; -import { lightTheme } from '../../../../../src/theme/builtin/light'; import { getTestCompiler } from '../../../../util/factory/compiler'; import { initChartDataSet } from '../../../../util/context'; @@ -90,7 +89,6 @@ const ctx: IComponentOption = { getChartViewRect: () => { return { width: 500, height: 500 } as any; }, - getTheme: () => lightTheme, globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), getComponentByUserId: function (user_id: string | number): IComponent | undefined { throw new Error('Function not implemented.'); diff --git a/packages/vchart/__tests__/unit/data/fields.test.ts b/packages/vchart/__tests__/unit/data/fields.test.ts index bc5e68dd9f..4504fcc908 100644 --- a/packages/vchart/__tests__/unit/data/fields.test.ts +++ b/packages/vchart/__tests__/unit/data/fields.test.ts @@ -1,10 +1,9 @@ import { LineChart } from '../../../src/chart/line/line'; -import { DataSet, dataViewParser } from '@visactor/vdataset'; +import { DataSet } from '@visactor/vdataset'; import { EventDispatcher } from '../../../src/event/event-dispatcher'; import * as bt from '../../../src/vchart-all'; import { getTestCompiler } from '../../util/factory/compiler'; import { GlobalScale } from '../../../src/scale/global-scale'; -import { ThemeManager } from '../../../src'; import { initChartDataSet } from '../../util/context'; bt; @@ -68,8 +67,7 @@ describe('data fields test', () => { container: null, mode: 'desktop-browser', getCompiler: getTestCompiler, - globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), - getTheme: () => ThemeManager.getCurrentTheme() + globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any) } as any ); chart.created(); @@ -143,8 +141,7 @@ describe('data fields test', () => { container: null, mode: 'desktop-browser', getCompiler: getTestCompiler, - globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), - getTheme: () => ThemeManager.getCurrentTheme() + globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any) } as any ); chart.created(); diff --git a/packages/vchart/__tests__/unit/morph/morph.test.ts b/packages/vchart/__tests__/unit/morph/morph.test.ts index fcd379269a..9bec7a4802 100644 --- a/packages/vchart/__tests__/unit/morph/morph.test.ts +++ b/packages/vchart/__tests__/unit/morph/morph.test.ts @@ -1,5 +1,5 @@ -import { BarChart, CommonChart, ThemeManager } from '../../../src'; -import { DataSet, dataViewParser, DataView, csvParser } from '@visactor/vdataset'; +import { BarChart, CommonChart } from '../../../src'; +import { DataSet, DataView, csvParser } from '@visactor/vdataset'; import { EventDispatcher } from '../../../src/event/event-dispatcher'; import { createCanvas, removeDom } from '../../util/dom'; import { getTestCompiler } from '../../util/factory/compiler'; @@ -126,7 +126,6 @@ describe('Bar chart test', () => { getCompiler: getTestCompiler, globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), animation: true, - getTheme: () => ThemeManager.getCurrentTheme(), onError: () => {} } as any); scatterChart.created(); @@ -147,7 +146,6 @@ describe('Bar chart test', () => { getCompiler: getTestCompiler, globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), animation: true, - getTheme: () => ThemeManager.getCurrentTheme(), onError: () => {} } as any); barChart.created(); @@ -184,7 +182,6 @@ describe('Bar chart test', () => { getCompiler: getTestCompiler, globalScale: new GlobalScale([], { getAllSeries: () => [] as any[] } as any), animation: true, - getTheme: () => ThemeManager.getCurrentTheme(), onError: () => {} } as any ); diff --git a/packages/vchart/__tests__/unit/scale/global-scale.test.ts b/packages/vchart/__tests__/unit/scale/global-scale.test.ts index b911370fd6..387d70f9a5 100644 --- a/packages/vchart/__tests__/unit/scale/global-scale.test.ts +++ b/packages/vchart/__tests__/unit/scale/global-scale.test.ts @@ -4,15 +4,14 @@ import { CommonChart } from '../../../src/chart/common/common'; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-nocheck import { EventDispatcher } from '../../../src/event/event-dispatcher'; -import type { IChartSpec, ScatterSeries } from '../../../src'; -// eslint-disable-next-line no-duplicate-imports -import { ThemeManager } from '../../../src'; +import { VChart, type IChartSpec, type ScatterSeries } from '../../../src'; // eslint-disable-next-line no-duplicate-imports import { DataSet, dataViewParser, DataView } from '@visactor/vdataset'; import { createCanvas, removeDom } from '../../util/dom'; import type { IAttrs, VisualScaleType } from '../../../src/mark/interface'; import { dimensionStatistics } from '../../../src/data/transforms/dimension-statistics'; +const VChartClass = VChart; // 确保引用 vchart 以确保注册所需的图表 // 保证引入执行 Build-in let dataSet: DataSet; const data = [ @@ -315,8 +314,7 @@ describe('global-scale test', () => { }; } } as any; - }, - getTheme: () => ThemeManager.getCurrentTheme() + } } as any ); chart.created(); @@ -470,8 +468,7 @@ describe('global-scale test', () => { }; } } as any; - }, - getTheme: () => ThemeManager.getCurrentTheme() + } } as any ); chart.created(); diff --git a/packages/vchart/__tests__/unit/theme/line.test.ts b/packages/vchart/__tests__/unit/theme/line.test.ts index e3298799df..745675f806 100644 --- a/packages/vchart/__tests__/unit/theme/line.test.ts +++ b/packages/vchart/__tests__/unit/theme/line.test.ts @@ -1,4 +1,4 @@ -import { DataSet, DataView, csvParser, dataViewParser } from '@visactor/vdataset'; +import { DataSet, DataView, csvParser } from '@visactor/vdataset'; import { VChart } from '../../../src/vchart-all'; import type { ITheme } from '../../../src/theme'; // eslint-disable-next-line no-duplicate-imports @@ -137,8 +137,7 @@ describe('theme switch test', () => { await vchart.renderAsync(); // await vchart.setCurrentTheme('light'); // sepc - expect(vchart.getCurrentTheme().background).toBe('red'); - expect(vchart.getCurrentThemeName()).toBe('newTheme'); + expect(vchart.getCompiler().getVGrammarView().background()).toBe('red'); }); it('set theme in spec and theme is an object', async () => { @@ -179,7 +178,6 @@ describe('theme switch test', () => { await vchart.renderAsync(); // spec - expect(vchart.getCurrentTheme().background).toBe('red'); expect(vchart.getCompiler().getVGrammarView().background()).toBe('red'); expect(vchart.getCurrentThemeName()).toBe('light'); }); diff --git a/packages/vchart/src/chart/base-chart.ts b/packages/vchart/src/chart/base-chart.ts index 5b2ef8d0fc..3fb1e1426d 100644 --- a/packages/vchart/src/chart/base-chart.ts +++ b/packages/vchart/src/chart/base-chart.ts @@ -83,6 +83,7 @@ import { calculateChartSize, mergeUpdateResult } from './util'; import { isDiscrete } from '@visactor/vscale'; import { updateDataViewInData } from '../data/initialize'; import { getThemeFromOption } from '../theme/util'; +import { defaultChartLevelTheme } from '../theme'; export class BaseChart extends CompilableBase implements IChart { readonly type: string = 'chart'; @@ -484,13 +485,13 @@ export class BaseChart extends CompilableBase implements IChart { if (this._spec.zField || (this._spec.series && this._spec.series.some((s: any) => s.zField))) { use3dLayout = true; } - const layout = new (Factory.getLayoutInKey(this._spec.layout?.type ?? (use3dLayout ? 'layout3d' : 'base')))( - this._spec.layout, - { + const constructor = Factory.getLayoutInKey(this._spec.layout?.type ?? (use3dLayout ? 'layout3d' : 'base')); + if (constructor) { + const layout = new constructor(this._spec.layout, { onError: this._option?.onError - } - ); - this._layoutFunc = layout.layoutItems.bind(layout); + }); + this._layoutFunc = layout.layoutItems.bind(layout); + } } } @@ -1401,6 +1402,6 @@ export class BaseChart extends CompilableBase implements IChart { } getColorScheme() { - return this._option.getThemeConfig?.().chartLevelTheme?.colorScheme; + return (this._option.getThemeConfig?.().chartLevelTheme ?? defaultChartLevelTheme).colorScheme; } } diff --git a/packages/vchart/src/component/tooltip/handler/base.ts b/packages/vchart/src/component/tooltip/handler/base.ts index bca4ab9e5f..48c94b57b1 100644 --- a/packages/vchart/src/component/tooltip/handler/base.ts +++ b/packages/vchart/src/component/tooltip/handler/base.ts @@ -45,6 +45,8 @@ import type { IContainerSize, TooltipAttributes } from '@visactor/vrender-compon import { getTooltipAttributes } from './utils/attribute'; import type { DimensionEventParams } from '../../../event/events/dimension/interface'; import type { IChartOption } from '../../../chart/interface'; +import type { IChartLevelTheme } from '../../../core/interface'; +import { defaultChartLevelTheme } from '../../../theme'; type ChangeTooltipFunc = ( visible: boolean, @@ -669,8 +671,12 @@ export abstract class BaseTooltipHandler implements ITooltipHandler { // 计算 tooltip 内容区域的宽高,并缓存结果 protected _getTooltipBoxSize(actualTooltip: IToolTipActual, changePositionOnly: boolean): IContainerSize | undefined { if (!changePositionOnly || isNil(this._attributes)) { - const { chartLevelTheme } = this._chartOption.getThemeConfig?.() ?? {}; - this._attributes = getTooltipAttributes(actualTooltip, this._component.getSpec(), chartLevelTheme); + const { chartLevelTheme = defaultChartLevelTheme } = this._chartOption.getThemeConfig?.() ?? {}; + this._attributes = getTooltipAttributes( + actualTooltip, + this._component.getSpec(), + chartLevelTheme as IChartLevelTheme + ); } return { width: this._attributes?.panel?.width, diff --git a/packages/vchart/src/model/base-model.ts b/packages/vchart/src/model/base-model.ts index 866304f10a..0283d87543 100644 --- a/packages/vchart/src/model/base-model.ts +++ b/packages/vchart/src/model/base-model.ts @@ -26,6 +26,7 @@ import { Factory } from '../core/factory'; import type { SeriesTypeEnum } from '../series/interface'; import { MarkSet } from '../mark/mark-set'; import { getThemeFromOption } from '../theme/util'; +import { defaultChartLevelTheme } from '../theme'; export abstract class BaseModel extends LayoutItem implements IModel { readonly type: string = 'null'; @@ -430,10 +431,10 @@ export abstract class BaseModel extends LayoutItem impl } getColorScheme() { - return this._option.getThemeConfig?.().chartLevelTheme?.colorScheme; + return (this._option.getThemeConfig?.().chartLevelTheme ?? defaultChartLevelTheme).colorScheme; } protected _getChartLevelTheme() { - return this._option.getThemeConfig?.().chartLevelTheme; + return this._option.getThemeConfig?.().chartLevelTheme ?? defaultChartLevelTheme; } } diff --git a/packages/vchart/src/theme/builtin/index.ts b/packages/vchart/src/theme/builtin/index.ts index 64aa1a51e0..c4fc0b1479 100644 --- a/packages/vchart/src/theme/builtin/index.ts +++ b/packages/vchart/src/theme/builtin/index.ts @@ -2,7 +2,9 @@ export * from './light'; /** 历史弃用主题 */ export * from './common/legacy'; +import type { IChartLevelTheme } from '../../core/interface'; import { mergeTheme } from '../../util/spec/merge-theme'; +import { getActualColor } from '../color-scheme/util'; import type { ITheme } from '../interface'; import { darkTheme } from './dark'; import { lightTheme } from './light'; @@ -24,3 +26,9 @@ export const hasThemeMerged: Map = new Map( /** 使新主题基于默认主题扩展,保证基础值 */ export const getMergedTheme = (theme: Partial): ITheme => mergeTheme({}, themes.get(defaultThemeName), theme); + +export const defaultChartLevelTheme: IChartLevelTheme = { + background: getActualColor(builtinThemes[defaultThemeName].background, builtinThemes[defaultThemeName].colorScheme), + fontFamily: builtinThemes[defaultThemeName].fontFamily, + colorScheme: builtinThemes[defaultThemeName].colorScheme +}; diff --git a/packages/vchart/src/theme/color-scheme/util.ts b/packages/vchart/src/theme/color-scheme/util.ts index f419f35129..e76e0e126c 100644 --- a/packages/vchart/src/theme/color-scheme/util.ts +++ b/packages/vchart/src/theme/color-scheme/util.ts @@ -148,7 +148,7 @@ export function isColorKey(obj: any): obj is IColorKey { } export function isProgressiveDataColorScheme(obj: any): obj is ProgressiveDataScheme { - if (!isArray(obj)) { + if (!isArray(obj) || obj.length === 0) { return false; } return obj.every(item => { diff --git a/packages/vchart/src/theme/util.ts b/packages/vchart/src/theme/util.ts index 3cda47bd99..99a095db2d 100644 --- a/packages/vchart/src/theme/util.ts +++ b/packages/vchart/src/theme/util.ts @@ -5,6 +5,7 @@ import { mergeSpec, transformColorSchemeToMerge, transformSeriesThemeToMerge } f import { getActualColor } from './color-scheme/util'; import type { IThemeColorScheme } from './color-scheme/interface'; import type { IModelOption } from '../model/interface'; +import { defaultChartLevelTheme, defaultThemeName } from './builtin'; /** * 性能优化过的主题合并,只合并需要取用的主题部分 @@ -79,6 +80,11 @@ const getMergedValue = (...sources: any[]) => { }; export const getThemeFromOption = (path: string, option: Partial) => { - const { globalTheme, optionTheme, specTheme, chartLevelTheme } = option.getThemeConfig?.() ?? {}; + const { + globalTheme = defaultThemeName, + optionTheme, + specTheme, + chartLevelTheme = defaultChartLevelTheme + } = option.getThemeConfig?.() ?? {}; return mergeThemeAndGet(path, globalTheme, optionTheme, specTheme, chartLevelTheme?.colorScheme); }; diff --git a/packages/vchart/src/util/spec/merge-theme.ts b/packages/vchart/src/util/spec/merge-theme.ts index b28ee97b41..63781a78e3 100644 --- a/packages/vchart/src/util/spec/merge-theme.ts +++ b/packages/vchart/src/util/spec/merge-theme.ts @@ -58,7 +58,7 @@ export function transformSeriesThemeToMerge( ): any { const newTheme: any = {}; Object.values(seriesMarkInfoMap[seriesType]).forEach(({ type, name }) => { - newTheme[name] = mergeSpec({}, markByType?.[array(type)[0]] ?? {}, markByName?.[name] ?? {}, seriesTheme[name]); + newTheme[name] = mergeSpec({}, markByType?.[array(type)[0]], markByName?.[name], seriesTheme?.[name]); }); return { ...seriesTheme, diff --git a/packages/vchart/src/util/spec/preprocess.ts b/packages/vchart/src/util/spec/preprocess.ts index 7c025c1af2..b56c5e2571 100644 --- a/packages/vchart/src/util/spec/preprocess.ts +++ b/packages/vchart/src/util/spec/preprocess.ts @@ -1,4 +1,4 @@ -import { isArray, isFunction, isObject, isString, isValid, isValidNumber } from '@visactor/vutils'; +import { isArray, isFunction, isNil, isObject, isString, isValid, isValidNumber } from '@visactor/vutils'; import { seriesMarkNameSet, type SeriesTypeEnum } from '../../series/interface'; import type { IThemeColorScheme } from '../../theme/color-scheme/interface'; import { isDataView, isHTMLElement } from './common'; @@ -19,6 +19,9 @@ export function preprocessSpecOrTheme( colorScheme?: IThemeColorScheme, seriesType?: SeriesTypeEnum ): any { + if (isNil(obj)) { + return obj; + } if (isArray(obj)) { return obj.map(element => { if (isObject(element) && !isFunction(element)) {