diff --git a/packages/plugins/@nocobase/plugin-data-visualization/src/client/chart/g2plot/AntChart.tsx b/packages/plugins/@nocobase/plugin-data-visualization/src/client/chart/g2plot/AntChart.tsx new file mode 100644 index 000000000..17beee816 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-data-visualization/src/client/chart/g2plot/AntChart.tsx @@ -0,0 +1,26 @@ +import React, { useContext, useEffect, useRef } from 'react'; +import { ChartRendererContext } from '../../renderer'; + +export const getAntChart = (Component: React.FC) => (props: any) => { + const { service } = useContext(ChartRendererContext); + const chartRef = useRef(null); + const [height, setHeight] = React.useState(0); + useEffect(() => { + const el = chartRef.current; + if (!el || service.loading === true) { + return; + } + const observer = new ResizeObserver((entries) => { + entries.forEach((entry) => { + setHeight(entry.contentRect.height); + }); + }); + observer.observe(el); + return () => observer.disconnect(); + }, [service.loading]); + return ( +
+ +
+ ); +}; diff --git a/packages/plugins/@nocobase/plugin-data-visualization/src/client/chart/g2plot/g2plot.ts b/packages/plugins/@nocobase/plugin-data-visualization/src/client/chart/g2plot/g2plot.ts index d1c512921..a804c3c72 100644 --- a/packages/plugins/@nocobase/plugin-data-visualization/src/client/chart/g2plot/g2plot.ts +++ b/packages/plugins/@nocobase/plugin-data-visualization/src/client/chart/g2plot/g2plot.ts @@ -1,12 +1,13 @@ import { Chart, ChartProps, ChartType, RenderProps } from '../chart'; import configs from './configs'; +import { getAntChart } from './AntChart'; export class G2PlotChart extends Chart { constructor({ name, title, Component, config }: ChartProps) { super({ name, title, - Component, + Component: getAntChart(Component), config: ['xField', 'yField', 'seriesField', ...(config || [])], }); this.addConfigs(configs); diff --git a/packages/plugins/@nocobase/plugin-data-visualization/src/client/renderer/ChartRenderer.tsx b/packages/plugins/@nocobase/plugin-data-visualization/src/client/renderer/ChartRenderer.tsx index d2f0f0046..fb5c53d38 100644 --- a/packages/plugins/@nocobase/plugin-data-visualization/src/client/renderer/ChartRenderer.tsx +++ b/packages/plugins/@nocobase/plugin-data-visualization/src/client/renderer/ChartRenderer.tsx @@ -34,8 +34,6 @@ export const ChartRenderer: React.FC & { const general = config?.general || {}; const advanced = config?.advanced || {}; const api = useAPIClient(); - const chartRef = useRef(null); - const [height, setHeight] = React.useState(0); const chart = useChart(config?.chartType); const locale = api.auth.getLocale(); const transformers = useFieldTransformer(transform, locale); @@ -54,20 +52,6 @@ export const ChartRenderer: React.FC & { }); const C = chart?.Component; - useEffect(() => { - const el = chartRef.current; - if (!el || service.loading === true) { - return; - } - const observer = new ResizeObserver((entries) => { - entries.forEach((entry) => { - setHeight(entry.contentRect.height); - }); - }); - observer.observe(el); - return () => observer.disconnect(); - }, [service.loading]); - if (!chart) { return ; } @@ -83,9 +67,7 @@ export const ChartRenderer: React.FC & { }} FallbackComponent={ErrorFallback} > -
- -
+ );