From a4c806e7e0b0ba13bd8d30d9d44281d3df7aee0d Mon Sep 17 00:00:00 2001 From: wisonic-s Date: Thu, 9 May 2024 12:03:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E5=9B=BE=E8=A1=A8):=20=E5=8D=95?= =?UTF-8?q?=E4=B8=AA=E5=9B=BE=E8=A1=A8=E5=8F=AF=E4=BB=A5=E6=9C=89=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E5=AF=B9=E8=B1=A1=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/js/panel/types/impl/g2plot.ts | 37 ++++++++++++++++++- .../chart/components/js/panel/types/index.ts | 6 ++- 2 files changed, 40 insertions(+), 3 deletions(-) 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