diff --git a/core/core-frontend/src/views/chart/components/js/panel/types/impl/g2plot.ts b/core/core-frontend/src/views/chart/components/js/panel/types/impl/g2plot.ts index 9aa63414e8..22191b7482 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/types/impl/g2plot.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/types/impl/g2plot.ts @@ -15,7 +15,8 @@ import { import { AntVAbstractChartView, AntVDrawOptions, - ChartLibraryType + ChartLibraryType, + ChartWrapper } from '@/views/chart/components/js/panel/types' import { getEngine } from '@antv/g2/esm/core' import { handleEmptyDataStrategy } from '../../../util' @@ -32,6 +33,38 @@ export interface G2PlotDrawOptions extends AntVDrawOptions { quadrantDefaultBaseline?: (...args: any) => void } +/** + * 图表对象包装类,一个图表里面可能有多个对象实例 + */ +export class G2PlotWrapper> extends ChartWrapper< + P | Array

+> { + constructor(chartInstance: P | Array

) { + super() + this.chartInstance = chartInstance + } + destroy = () => { + if (!this.chartInstance) { + return + } + if (Array.isArray(this.chartInstance)) { + this.chartInstance?.forEach(p => p.destroy()) + } else { + this.chartInstance?.destroy() + } + } + + render = () => { + if (!this.chartInstance) { + return + } + if (Array.isArray(this.chartInstance)) { + this.chartInstance?.forEach(p => p.render()) + } else { + this.chartInstance?.render() + } + } +} /** * G2Plot 的图表抽象类 */ @@ -46,7 +79,7 @@ export abstract class G2PlotChartView< * @param drawOptions 图表配置参数 * @return 生成的图表对象,类型为 Plot 的子类 */ - public abstract drawChart(drawOptions: G2PlotDrawOptions

): P + public abstract drawChart(drawOptions: G2PlotDrawOptions

): G2PlotWrapper | P protected configTheme(chart: Chart, options: O): O { const theme = getTheme(chart) diff --git a/core/core-frontend/src/views/chart/components/js/panel/types/index.ts b/core/core-frontend/src/views/chart/components/js/panel/types/index.ts index aeb78b933f..8049fa54c6 100644 --- a/core/core-frontend/src/views/chart/components/js/panel/types/index.ts +++ b/core/core-frontend/src/views/chart/components/js/panel/types/index.ts @@ -16,7 +16,11 @@ export enum ChartLibraryType { RICH_TEXT = 'rich-text', INDICATOR = 'indicator' } - +export abstract class ChartWrapper { + chartInstance: O + abstract render: () => any + abstract destroy: () => any +} export abstract class AbstractChartView { render: ChartRenderType library: ChartLibraryType