Skip to content

Commit 99484f7

Browse files
authored
LG-4743: Fix resize bug in charts (#2632)
* Fix access issue * Changeset * Remove unnecessary type
1 parent 8fc846d commit 99484f7

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Diff for: .changeset/sour-bottles-sip.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lg-charts/core': patch
3+
---
4+
5+
Fixes bug that prevented chart resizing after initial render

Diff for: charts/core/src/Echart/useEchart.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ export function useEchart({
100100
[setEchartOptions],
101101
);
102102

103-
const resizeEchartInstance = withInstanceCheck(() => {
104-
echartsInstance?.resize();
105-
});
106-
107103
const addToGroup: EChartsInstance['addToGroup'] = useCallback(
108104
withInstanceCheck((groupId: string) => {
109105
// echartsCoreRef.current should exist if instance does, but checking for extra safety
@@ -260,23 +256,31 @@ export function useEchart({
260256
});
261257

262258
/**
259+
* CHART INITIALIZATION ---------------------
263260
* Sets up the echart instance on initial render or if the container changes.
264261
* Additionally, disposes of echart instance and cleans up handlers on unmount.
265262
*/
266263
useEffect(() => {
267264
setError(null);
268265

266+
let resizeCallback: () => void;
267+
269268
initializeEcharts()
270269
.then(echartsCore => {
271270
echartsCoreRef.current = echartsCore;
272271

273272
if (container) {
274273
// Init an echart instance
275274
const newChart = echartsCoreRef.current.init(container);
275+
276276
// Set the initial options on the instance
277277
newChart.setOption(options);
278+
278279
// Resize chart when window resizes because echarts don't be default
279-
window.addEventListener('resize', resizeEchartInstance);
280+
resizeCallback = () => {
281+
newChart.resize();
282+
};
283+
window.addEventListener('resize', resizeCallback);
280284

281285
setEchartsInstance(newChart);
282286
setReady(true);
@@ -293,7 +297,7 @@ export function useEchart({
293297
});
294298

295299
return () => {
296-
window.removeEventListener('resize', resizeEchartInstance);
300+
window.removeEventListener('resize', resizeCallback);
297301
activeHandlers.current.clear();
298302

299303
if (echartsInstance) {
@@ -303,6 +307,7 @@ export function useEchart({
303307
}, [container]);
304308

305309
/**
310+
* SETTING THEME ---------------------
306311
* Sets the theme when the instance is created or the theme changes.
307312
* This is not actually necessary on initial render because the theme
308313
* is also set on the default options. This is primarily necessary

0 commit comments

Comments
 (0)