@@ -17,22 +17,38 @@ const props = defineProps({
1717const container = ref (null );
1818let graphInstance = null ;
1919let resizeObserver = null ;
20+ let renderTimeout = null ;
21+ let retryCount = 0 ;
22+ const MAX_RETRIES = 5 ;
2023
2124const initGraph = () => {
25+ // 确保容器存在
26+ if (! container .value ) {
27+ console .warn (' GraphContainer: container is not available' );
28+ return ;
29+ }
30+
2231 // 确保容器大小已经稳定
2332 const width = container .value .offsetWidth ;
2433 const height = container .value .offsetHeight ;
2534
26- if (width < 100 ) {
27- // 如果容器宽度太小,可能是抽屉还在展开中,稍后重试
28- setTimeout (initGraph, 100 );
29- return ;
35+ // 如果容器尺寸为0,增加重试次数
36+ if (width === 0 && height === 0 ) {
37+ if (retryCount < MAX_RETRIES ) {
38+ retryCount++ ;
39+ // console.log(`GraphContainer: Container size is 0, retrying (${retryCount}/${MAX_RETRIES})`);
40+ setTimeout (initGraph, 200 );
41+ return ;
42+ } else {
43+ console .warn (' GraphContainer: Container size remains 0 after maximum retries' );
44+ return ;
45+ }
3046 }
3147
48+ retryCount = 0 ; // 重置重试计数
49+
3250 // 确保容器干净,没有残留元素
33- if (container .value ) {
34- container .value .innerHTML = ' ' ;
35- }
51+ container .value .innerHTML = ' ' ;
3652
3753 // 确保没有已存在的实例
3854 if (graphInstance) {
@@ -81,9 +97,19 @@ const initGraph = () => {
8197 },
8298 behaviors: [' drag-element' , ' zoom-canvas' , ' drag-canvas' ],
8399 });
100+
101+ // console.log(`GraphContainer: Graph initialized with dimensions ${width}x${height}`);
84102};
85103
86104const renderGraph = () => {
105+ // 检查数据是否有效
106+ if (! props .graphData || ! props .graphData .nodes || ! props .graphData .edges ) {
107+ console .warn (' GraphContainer: Invalid graphData provided' );
108+ return ;
109+ }
110+
111+ // console.log('GraphContainer: Rendering graph with', props.graphData.nodes.length, 'nodes and', props.graphData.edges.length, 'edges');
112+
87113 // 如果已有实例且容器大小发生明显变化,则销毁重建
88114 if (graphInstance && container .value ) {
89115 const currentWidth = container .value .offsetWidth ;
@@ -110,6 +136,7 @@ const renderGraph = () => {
110136
111137 // 如果宽度变化超过50px,重新初始化图表
112138 if (Math .abs (currentWidth - graphWidth) > 50 || Math .abs (currentHeight - graphHeight) > 50 ) {
139+ // console.log('GraphContainer: Container size changed significantly, reinitializing graph');
113140 graphInstance .destroy ();
114141 graphInstance = null ;
115142
@@ -120,12 +147,14 @@ const renderGraph = () => {
120147 }
121148 }
122149
150+ // 如果没有图表实例,尝试初始化
123151 if (! graphInstance) {
124152 initGraph ();
125153 }
126154
155+ // 如果仍然没有图表实例,直接返回
127156 if (! graphInstance) {
128- // 初始化可能推迟执行,此时直接返回
157+ // console.warn('GraphContainer: Failed to initialize graph instance');
129158 return ;
130159 }
131160
@@ -141,20 +170,26 @@ const renderGraph = () => {
141170 }))
142171 };
143172
173+ // console.log('GraphContainer: Setting data and rendering graph');
144174 graphInstance .setData (formattedData);
175+
176+ // 强制刷新视图
145177 graphInstance .render ();
146178
147179 // 确保图表正确居中和适应视图
148180 setTimeout (() => {
149181 if (graphInstance) {
150182 graphInstance .fitCenter ();
151183 graphInstance .fitView ();
184+ // console.log('GraphContainer: Graph rendered and fitted');
152185 }
153- }, 300 );
186+ }, 100 );
154187};
155188
156189// 添加供父组件调用的刷新方法
157190const refreshGraph = () => {
191+ // console.log('GraphContainer: refreshGraph called');
192+
158193 // 销毁现有图表实例
159194 if (graphInstance) {
160195 graphInstance .destroy ();
@@ -166,10 +201,15 @@ const refreshGraph = () => {
166201 container .value .innerHTML = ' ' ;
167202 }
168203
204+ // 重置重试计数
205+ retryCount = 0 ;
206+
169207 // 延迟重新渲染,确保容器大小已经稳定
170- setTimeout (() => {
208+ // 增加延迟时间确保窗口完全打开
209+ clearTimeout (renderTimeout);
210+ renderTimeout = setTimeout (() => {
171211 renderGraph ();
172- }, 100 );
212+ }, 300 );
173213};
174214
175215// 向父组件暴露方法
@@ -183,6 +223,7 @@ onMounted(() => {
183223 resizeObserver = new ResizeObserver ((entries ) => {
184224 for (const entry of entries) {
185225 if (entry .target === container .value ) {
226+ // console.log('GraphContainer: Container resized to', entry.contentRect.width, 'x', entry.contentRect.height);
186227 renderGraph ();
187228 }
188229 }
@@ -193,14 +234,21 @@ onMounted(() => {
193234 }
194235 }
195236
196- renderGraph ();
237+ // 初始渲染,增加延迟确保容器完全展开
238+ clearTimeout (renderTimeout);
239+ renderTimeout = setTimeout (() => {
240+ renderGraph ();
241+ }, 300 );
242+
197243 window .addEventListener (' resize' , renderGraph);
198244});
199245
200246// 添加组件卸载时的清理
201247onUnmounted (() => {
202248 window .removeEventListener (' resize' , renderGraph);
203249
250+ clearTimeout (renderTimeout);
251+
204252 if (resizeObserver) {
205253 resizeObserver .disconnect ();
206254 resizeObserver = null ;
@@ -220,6 +268,7 @@ onUnmounted(() => {
220268watch (() => props .graphData , (newData , oldData ) => {
221269 // 强制重新渲染图表
222270 if (newData !== oldData) {
271+ // console.log('GraphContainer: graphData changed, refreshing graph');
223272 refreshGraph ();
224273 }
225274}, { deep: true });
@@ -230,7 +279,8 @@ watch(() => props.graphData, (newData, oldData) => {
230279 background : #F7F7F7 ;
231280 border-radius : 16px ;
232281 width : 100% ;
233- height : 600px ;
282+ min-height : 400px ;
283+ height : 100% ;
234284 overflow : hidden ;
235285}
236286 </style >
0 commit comments