poprawki i zmiany ux
This commit is contained in:
@@ -9,23 +9,41 @@ interface EChartProps {
|
||||
|
||||
export function EChart({ option, className = "h-80 w-full" }: EChartProps) {
|
||||
const ref = useRef<HTMLDivElement | null>(null);
|
||||
const chartRef = useRef<echarts.EChartsType | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!ref.current) {
|
||||
if (!ref.current || chartRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
const chart = echarts.init(ref.current);
|
||||
chart.setOption(option);
|
||||
chartRef.current = echarts.init(ref.current);
|
||||
|
||||
const observer = new ResizeObserver(() => chart.resize());
|
||||
return () => {
|
||||
chartRef.current?.dispose();
|
||||
chartRef.current = null;
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!chartRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
chartRef.current.setOption(option, { notMerge: false, lazyUpdate: true });
|
||||
}, [option]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!ref.current || !chartRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
const observer = new ResizeObserver(() => chartRef.current?.resize());
|
||||
observer.observe(ref.current);
|
||||
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
chart.dispose();
|
||||
};
|
||||
}, [option]);
|
||||
}, []);
|
||||
|
||||
return <div ref={ref} className={className} />;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user