Skip to content

Only clamp responsive height when the container height is independent of the canvas#12277

Open
kirthi-b wants to merge 1 commit into
chartjs:masterfrom
kirthi-b:fix/responsive-resize-v4
Open

Only clamp responsive height when the container height is independent of the canvas#12277
kirthi-b wants to merge 1 commit into
chartjs:masterfrom
kirthi-b:fix/responsive-resize-v4

Conversation

@kirthi-b

Copy link
Copy Markdown

Fixes #11005.

The clamp at the end of getMaximumSize (the maintainHeight block in src/helpers/helpers.dom.ts) treats the container's current height as a constraint even when that height is just the canvas's own height, for example a plain wrapper div with no explicit height. On grow, the proposed height (newWidth / aspectRatio) always exceeds it, so getMaximumSize returns exactly the old size and the resize is a no-op. onResize never fires because nothing changed. On shrink the clamp passes and the wrapper's height ratchets down with the chart. A one way ratchet: shrink works, grow never does.

For the record, the clamp came from #10646 (commit 0c51ecd, documented in the v4 migration guide as "maintainAspectRatio respects container height"). Commit 52cf8e8 (#10693), which this thread bisected to, only narrowed it.

Measured with a Playwright harness driving a container 600 to 300 to 900 wide, chart at responsive true and default maintainAspectRatio, on 4.5.1:

container init 600 shrink 300 grow 900
plain div 600x300 300x150 300x150, stuck
explicit height 200 400x200 300x150 400x200, clamps as documented
flex parent 600x300 300x150 300x150, stuck
nbsp sibling workaround 600x300 300x150 900x450, crawls back

On 3.9.1 all four cases return to 900x450.

The fix keeps the #10646 behavior for containers with a real height and removes the ratchet: before clamping, measure the container with the canvas taken out of flow (display none, restored synchronously, nothing paints), and only clamp if the container height holds up on its own. The extra layout read happens only on the throttled resize path and only when the clamp would otherwise apply.

With the patch: plain div and flex cases grow back to 900x450, the explicit height case still clamps to 400x200, and the helpers.dom (24), platform.dom (21) and core.controller (85) suites pass, including the tests added by #10646 and #10693. Two regression tests added for the grow case and the display style restoration.

…the canvas

Since chartjs#10646, getMaximumSize clamps the computed height to the container
height when maintainAspectRatio is true. When the container has no
explicit height, its height is derived from the canvas itself, so the
chart's current height becomes a ceiling: the chart can shrink but can
never grow back.

Measure the container with the canvas removed from the layout and only
apply the clamp when the container height does not collapse, i.e. when
it is defined independently of the canvas. Containers with an explicit
height keep the documented v4 fit-within behavior.

Fixes chartjs#11005
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"responsive" option problem since v4

1 participant