Skip to content

Commit 05c1ba3

Browse files
fix(dom): update canvas style on resize to allow chart to grow
When the chart's container grows, the canvas style width/height were not being updated because they were already set (from initial render). This caused the chart to shrink correctly but not grow when the window was enlarged. This fix updates the canvas style width/height during resize when they differ from the current chart dimensions, allowing the chart to properly resize in both directions. Fixes #12177
1 parent a153556 commit 05c1ba3

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/helpers/helpers.dom.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,22 @@ export function retinaScale(
222222
// If no style has been set on the canvas, the render size is used as display size,
223223
// making the chart visually bigger, so let's enforce it to the "correct" values.
224224
// See https://github.com/chartjs/Chart.js/issues/3575
225+
// Also update styles when resizing to ensure the canvas grows with its container.
226+
// See https://github.com/chartjs/Chart.js/issues/12177
225227
if (canvas.style && (forceStyle || (!canvas.style.height && !canvas.style.width))) {
226228
canvas.style.height = `${chart.height}px`;
227229
canvas.style.width = `${chart.width}px`;
230+
} else if (canvas.style) {
231+
// Update styles on resize if they were previously set by Chart.js
232+
// This ensures the canvas can grow when its container grows
233+
const currentStyleHeight = parseFloat(canvas.style.height);
234+
const currentStyleWidth = parseFloat(canvas.style.width);
235+
if (!isNaN(currentStyleHeight) && currentStyleHeight !== chart.height) {
236+
canvas.style.height = `${chart.height}px`;
237+
}
238+
if (!isNaN(currentStyleWidth) && currentStyleWidth !== chart.width) {
239+
canvas.style.width = `${chart.width}px`;
240+
}
228241
}
229242

230243
const canvasHeight = Math.floor(deviceHeight);

0 commit comments

Comments
 (0)