chore: init bj_power monorepo (dashboard/mes/wms/wms_client/workstation), clear subproject git metadata and align naming to folder names
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import * as echarts from 'echarts';
|
||||
import type { EChartsOption } from 'echarts';
|
||||
|
||||
/**
|
||||
* 共享 ECharts Hook
|
||||
*
|
||||
* 相比在每个组件里 `echarts.init` + `dispose` 的写法,这里做了三点优化:
|
||||
* 1. 实例只初始化一次 —— 数据变化时走 `setOption(notMerge)`,避免每次刷新
|
||||
* 销毁重建造成的闪烁与动画状态丢失,刷新更平滑。
|
||||
* 2. 用 `ResizeObserver` 监听容器尺寸 —— 面板伸缩 / 切页 / 父级 flex 变化时
|
||||
* 图表能自适应,而不仅仅依赖 `window.resize`。
|
||||
* 3. 卸载时统一 dispose,杜绝泄漏。
|
||||
*
|
||||
* @param option 图表配置,传 null 表示暂不渲染(由调用方处理空态)
|
||||
*/
|
||||
export function useEChart(option: EChartsOption | null) {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
const chartRef = useRef<echarts.ECharts | null>(null);
|
||||
|
||||
// 初始化(仅一次)+ 容器尺寸监听
|
||||
useEffect(() => {
|
||||
const el = ref.current;
|
||||
if (!el) return;
|
||||
|
||||
const chart = echarts.init(el, undefined, { renderer: 'canvas' });
|
||||
chartRef.current = chart;
|
||||
|
||||
const ro = new ResizeObserver(() => chart.resize());
|
||||
ro.observe(el);
|
||||
|
||||
return () => {
|
||||
ro.disconnect();
|
||||
chart.dispose();
|
||||
chartRef.current = null;
|
||||
};
|
||||
}, []);
|
||||
|
||||
// 数据变化时更新配置(notMerge: 全量替换,保证空态/有数据态切换干净)
|
||||
useEffect(() => {
|
||||
if (!chartRef.current || !option) return;
|
||||
chartRef.current.setOption(option, { notMerge: true });
|
||||
}, [option]);
|
||||
|
||||
// ref 对象由 useRef 保持稳定,直接返回即可
|
||||
return ref;
|
||||
}
|
||||
Reference in New Issue
Block a user