@@ -87,6 +87,20 @@ def render_notebook(self) -> Html:
8787 var plot_{ plot .plot_id } = new G2Plot.{ plot .plot_type } ("{ plot .plot_id } ", { plot .js_options } )
8888 plot_{ plot .plot_id } .render();
8989 }});
90+ // 确保DOM加载完成后再初始化
91+ if (document.readyState === 'loading') {{
92+ document.addEventListener('DOMContentLoaded', function() {{
93+ require(['G2Plot'], function (G2Plot) {{
94+ var plot_{ plot .plot_id } = new G2Plot.{ plot .plot_type } ("{ plot .plot_id } ", { plot .js_options } )
95+ plot_{ plot .plot_id } .render();
96+ }});
97+ }});
98+ }} else {{
99+ require(['G2Plot'], function (G2Plot) {{
100+ var plot_{ plot .plot_id } = new G2Plot.{ plot .plot_type } ("{ plot .plot_id } ", { plot .js_options } )
101+ plot_{ plot .plot_id } .render();
102+ }});
103+ }}
90104 </script>
91105 """
92106
@@ -111,16 +125,25 @@ def render_jupyterlab(self) -> Html:
111125 // load javascript
112126
113127 { plot .extra_js }
114- new Promise(function(resolve, reject) {{
115- var script = document.createElement("script");
116- script.onload = resolve;
117- script.onerror = reject;
118- script.src = "{ plot .js_url } ";
119- document.head.appendChild(script);
120- }}).then(() => {{
121- var plot_{ plot .plot_id } = new G2Plot.{ plot .plot_type } ("{ plot .plot_id } ", { plot .js_options } )
122- plot_{ plot .plot_id } .render();
123- }});
128+ function initChart_{ plot .plot_id } () {{
129+ new Promise(function(resolve, reject) {{
130+ var script = document.createElement("script");
131+ script.onload = resolve;
132+ script.onerror = reject;
133+ script.src = "{ plot .js_url } ";
134+ document.head.appendChild(script);
135+ }}).then(() => {{
136+ var plot_{ plot .plot_id } = new G2Plot.{ plot .plot_type } ("{ plot .plot_id } ", { plot .js_options } )
137+ plot_{ plot .plot_id } .render();
138+ }});
139+ }}
140+
141+ // 确保DOM加载完成后再初始化
142+ if (document.readyState === 'loading') {{
143+ document.addEventListener('DOMContentLoaded', initChart_{ plot .plot_id } );
144+ }} else {{
145+ initChart_{ plot .plot_id } ();
146+ }}
124147 </script>
125148 """
126149 return Html (html )
0 commit comments