fix(echart): Thrown errors shown after resized (#33143)

This commit is contained in:
JUST.in DO IT
2025-04-17 09:49:49 -07:00
committed by GitHub
parent a53907a646
commit 172e5dd095

View File

@@ -25,6 +25,7 @@ import {
useLayoutEffect,
useCallback,
Ref,
useState,
} from 'react';
import { useSelector } from 'react-redux';
@@ -106,6 +107,16 @@ use([
LabelLayout,
]);
const loadLocale = async (locale: string) => {
let lang;
try {
lang = await import(`echarts/lib/i18n/lang${locale}`);
} catch (e) {
console.error(`Locale ${locale} not supported in ECharts`, e);
}
return lang?.default;
};
function Echart(
{
width,
@@ -123,6 +134,7 @@ function Echart(
// eslint-disable-next-line no-param-reassign
refs.divRef = divRef;
}
const [didMount, setDidMount] = useState(false);
const chartRef = useRef<EChartsType>();
const currentSelection = useMemo(
() => Object.keys(selectedValues) || [],
@@ -148,20 +160,20 @@ function Echart(
);
useEffect(() => {
const loadLocaleAndInitChart = async () => {
if (!divRef.current) return;
const lang = await import(`echarts/lib/i18n/lang${locale}`).catch(e => {
console.error(`Locale ${locale} not supported in ECharts`, e);
});
if (lang?.default) {
registerLocale(locale, lang.default);
loadLocale(locale).then(localeObj => {
if (localeObj) {
registerLocale(locale, localeObj);
}
if (!divRef.current) return;
if (!chartRef.current) {
chartRef.current = init(divRef.current, null, { locale });
}
setDidMount(true);
});
}, [locale]);
useEffect(() => {
if (didMount) {
Object.entries(eventHandlers || {}).forEach(([name, handler]) => {
chartRef.current?.off(name);
chartRef.current?.on(name, handler);
@@ -172,14 +184,14 @@ function Echart(
chartRef.current?.getZr().on(name, handler);
});
chartRef.current.setOption(echartOptions, true);
chartRef.current?.setOption(echartOptions, true);
// did mount
handleSizeChange({ width, height });
};
}
}, [didMount, echartOptions, eventHandlers, zrEventHandlers]);
loadLocaleAndInitChart();
}, [echartOptions, eventHandlers, zrEventHandlers, locale]);
useEffect(() => () => chartRef.current?.dispose(), []);
// highlighting
useEffect(() => {