Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart shrinking uncontrollably on browser zoom #11089

Open
TrevorThiessen57 opened this issue Jan 24, 2023 · 57 comments · May be fixed by #11132
Open

Chart shrinking uncontrollably on browser zoom #11089

TrevorThiessen57 opened this issue Jan 24, 2023 · 57 comments · May be fixed by #11132

Comments

@TrevorThiessen57
Copy link

Expected behavior

I expect the chart canvas to remain an appropriate size when changing the zoom in the browser (Ctrl + Mousewheel).

Current behavior

The chart initially renders correctly, but then slowly shrinks toward its top-left corner until it disappears, then re-renders at its initial size. This loop repeats indefinitely.

Reproducible sample

https://codepen.io/leelenaleee/pen/WNyJXEe

Optional extra steps/info to reproduce

To reproduce, change the browser zoom to 90% (Ctrl + Mousewheel down).
The chart behavior is different at various zoom levels. At 110% zoom, for example, it only shrinks to a certain point and then stops.
Note that this bug is also active on the chart.js documentation site on every chart example.
This bug does not happen in Firefox 109.0 (64-bit), but it does happen in Google Chrome Version 109.0.5414.75 (Official Build) (64-bit).

Possible solution

No response

Context

No response

chart.js version

v4.2.0

Browser name and version

Google Chrome Version 109.0.5414.75 (Official Build) (64-bit)

Link to your project

No response

@LeeLenaleee
Copy link
Collaborator

Souds like a duplicate of #10932 which was already fixed and should not happen in this version, I also don't see the behaviour you specified in this version.

Are you sure you are using v4.2.0?

@TrevorThiessen57
Copy link
Author

Bar Chart Border Radius _ Chart js - Google Chrome 2023-01-25 08-21-39

This gif shows the issue I mentioned. It is on the chart.js documentation site. I am assuming the site would be using the latest version of chart.js, and the documentation indicates it is v4.2.0 (visible in the gif). The site is opened using Google Chrome, at 90% zoom.

I reviewed #10932 and it looks like it is exactly this problem.

From what I've gathered, it may be an issue isolated to the combination of a Windows OS and Google Chrome browser. In particular, someone else running Linux does not have the same issue on their Chrome browser, but it does happen on other Windows machines. As I mentioned earlier as well, the behavior does not happen when I try to reproduce it on Firefox.

To be thorough, I double-checked the chart.js version in my project. It was at v4.1.2, which has now been upgraded to v4.2.0, but the issue remains. Considering the example I shared in the gif, however, I don't think my project setup is relevant to the issue.

@fly74
Copy link

fly74 commented Jan 26, 2023

Same for me in 4.2.0 with browser zoom is set to 116%. It zooms to zero and again when the mouse lay over the chart. I'm using the umd variant.

@dangreen
Copy link
Collaborator

@TrevorThiessen57 @fly74 Hi. Could you please share OS and browser versions?

@fly74
Copy link

fly74 commented Jan 27, 2023

@TrevorThiessen57 Sure, Windows 11 latest updates and Edge Version 109.0.1518.69.

@TrevorThiessen57
Copy link
Author

@dangreen Windows 10 Pro 64-bit Version 21H2 OS build 19044.1645, and Google Chrome Version 109.0.5414.120 (Official Build) (64-bit)

@dangreen
Copy link
Collaborator

@fly74 @TrevorThiessen57 Could you also share screen resolution?

@TrevorThiessen57
Copy link
Author

@dangreen 3840 x 2160

@fly74
Copy link

fly74 commented Jan 27, 2023

@fly74 @TrevorThiessen57 Could you also share screen resolution?

3840x2160 (4K)

@gbaron
Copy link
Contributor

gbaron commented Jan 28, 2023

I don't have access to a screen with that resolution. Anyone knows of a trick to simulate that?

@fly74
Copy link

fly74 commented Jan 28, 2023

You can use Remote Desktop to another machine and set the resolution via cli. You have then scrollbars in the rdp window.

@dangreen
Copy link
Collaborator

@gbaron You can use browserstack.com, but I'm already tried and can't reproduce this bug.

@fly74
Copy link

fly74 commented Jan 28, 2023

It is strange, it only happens when zoom is 116% in my case, 110/125 % works. :/
Maybe a function with round problem ?!?

@gbaron
Copy link
Contributor

gbaron commented Jan 28, 2023 via email

@fly74
Copy link

fly74 commented Jan 29, 2023

Could you please share what is the value of Window.devicePixelRatio

How can I get the Window.devicePixelRatio?

@kurkle
Copy link
Member

kurkle commented Jan 29, 2023

I can easily reproduce this on a mac with chart.js 4.1.0, but with 4.2.0 I can't. Played around with a lot of different display settings. But with mac it seems I can only get 1 or 2 as the "base" devicePixelRatio on screens, while in windows it can be something like 1.25 etc

@gbaron
Copy link
Contributor

gbaron commented Jan 30, 2023

Could you please share what is the value of Window.devicePixelRatio

How can I get the Window.devicePixelRatio?

Open the Developer Tools, then choose the Console and type window.devicePixelRatio. Thanks!

I can easily reproduce this on a mac with chart.js 4.1.0, but with 4.2.0 I can't. Played around with a lot of different display settings. But with mac it seems I can only get 1 or 2 as the "base" devicePixelRatio on screens, while in windows it can be something like 1.25 etc

On my MacBookPro, using an external display and Zoom %110 window.devicePixelRatio is 1.100000023841858

I'm asking because previously, the root cause was mishandling these non-integer values. I'm not sure if this is the case now.

@fly74
Copy link

fly74 commented Jan 30, 2023

Open the Developer Tools, then choose the Console and type window.devicePixelRatio. Thanks!

Mine is:

Screenshot 2023-01-30 065609

Another detail, my dpi zoom is 200% at 4k resolution.

@gbaron
Copy link
Contributor

gbaron commented Feb 1, 2023

Open the Developer Tools, then choose the Console and type window.devicePixelRatio. Thanks!

Mine is:

Screenshot 2023-01-30 065609

Another detail, my dpi zoom is 200% at 4k resolution.

Thank you; I ran some tests but unfortunately, I couldn't reproduce the issue.

How do you set the zoom level to 116%? I only see 110% and 125%...

@fly74
Copy link

fly74 commented Feb 1, 2023

It seems 116% will only be offered when the resolution is 4k/200%dpi. But I found an edge extension

https://microsoftedge.microsoft.com/addons/detail/vergr%C3%B6%C3%9Fern-f%C3%BCr-microsoft-/akclpjahoedloodjomjhnlmmblikemjj

with this I can reproduce the behavior at any resolution with 116% zoom.

the window.devicePixelRatio
at 125% = 2.5
at 110% = 2.200000047683716
at 100% = 2

maybe a rounding issue
at 116% = 2.31112003326416

@gbaron
Copy link
Contributor

gbaron commented Feb 2, 2023

Thank you. I'm now also able to reproduce the issue. The simplest way would be to set a style="zoom %116" on the <body> or <HTML> elements (depends on the setup, some other values may be needed to reproduce the issue).

It is probably a rounding issue.

@gbaron
Copy link
Contributor

gbaron commented Feb 6, 2023

I am able to reproduce it on Chrome too using "non-standard" zoom levels (i.e 117%)

Here is what I'm seeing:

getMaximumSize @ helpers.dom.ts is calculating the maximum size available for the chart. It starts with the available width and calculates the height based on the chart aspectRatio.

Then, the code below will check if the container's height is large enough. If it isn't, it will set the height to the container height (the width is also updated).

if (maintainHeight && aspectRatio && containerSize.height && height > containerSize.height) {

After the chart size is updated the browser will fire the ResizeObserver again. However, the container height is slightly less than the chart height (when zoom is active).

For example, when setting the chart height to be 454, the following invocation of ResizeObserver will return a new height of 453.984375.
As a result, the resize code will run again resulting in a new height of 453... ResizeObserver fires with 452.984375...
🤦‍♂️

At the moment I can't think of an elegant fix.

@kurkle @dangreen LMK if you can think of anything.

@fly74
Copy link

fly74 commented Feb 6, 2023

@gbaron Nice analysis, but what is the difference to v3.9.1 were it works?

@dangreen
Copy link
Collaborator

dangreen commented Feb 6, 2023

At the moment I can't think of an elegant fix.

@gbaron Maybe just round values from resize observer ?

@fly74
Copy link

fly74 commented Feb 10, 2023

@gbaron I don't really know if something is happen with this issue, but Chart.js v4.2.1 don't fix it.

@gbaron
Copy link
Contributor

gbaron commented Feb 11, 2023

@fly74 that is true. v4.2.1 doesn't include any fixes.
I just submitted a PR (#11132).

@fly74
Copy link

fly74 commented Feb 11, 2023

@gbaron Can you provide an umd dist file for some tests?

@lukos
Copy link

lukos commented Mar 24, 2023

It sounds like you've fixed it. In most of these edge cases with pixels, a good approach is to round down to the nearest e.g. 10 pixels, otherwise it can be easy for 1 pixel either way to cause a problem. You can do that with a simple mod 10.

@fly74
Copy link

fly74 commented Mar 24, 2023

Can this fix go into the official build?

@abdelouahabb
Copy link

does this being resolved with the latest release ?

@fly74
Copy link

fly74 commented May 2, 2023

Nothing seen in the release notes. Maybe anyone can do a test.

@abdelouahabb
Copy link

Nothing seen in the release notes. Maybe anyone can do a test.

indeed, I was waiting for the new release to update a solution, right now I can't test it on my laptop (not a high dpi screen), this happened with me on a client laptop (an XPS with high dpi) so I had to use @gbaron patch, so I guess i will wait for an official release notes ?!

@fly74
Copy link

fly74 commented May 2, 2023

You can test it with a browser plugin for zoom like described here some posts above or a browser you can set zoom to 116%. It is not depending on a hidpi screen.

@fly74
Copy link

fly74 commented May 3, 2023

I've tested the 4.3.0 it is not fixed. @gbaron

@Tomasmillar
Copy link

This happens to me with 4.3.0.

Settings that produce effect:
display is 3240 x 2160
Scale and Layout in Windows 10 set to 150%
zoom in chrome set to 90% or 110%

It doesn't happen if I change the scaling in chrome or in the windows settings to something other than above...

@fly74
Copy link

fly74 commented Jun 1, 2023

That's what I said. The fix ist not in the official release.

@lincolnthree
Copy link

Would love to see this included in the official release. Signing up for notifications.

@shuuuuun
Copy link

shuuuuun commented Jul 20, 2023

What are the blockers to releasing this fix? ( #11132 )

Also, what is the any workaround until it is released?

@fly74
Copy link

fly74 commented Jul 20, 2023

The only workaround for me was to compile the sorce from @gbaron above or use the resulting binary in my zip above. @shuuuuun

@shuuuuun
Copy link

Thank you for your reply.
I'm looking for a way to make it as maintainable as possible.
I hope it will be released officially soon...

@fly74
Copy link

fly74 commented Jul 25, 2023

Is it fixed in 4.3.1?

@peshanghiwa
Copy link

I am still experiencing this with 4.3

@fly74
Copy link

fly74 commented Aug 24, 2023

@gbaron @lukos Is this fixed in 4.4.0? The solution is from @gbaron in this thread.

@avisek-official
Copy link

options={{ responsive: true, maintainAspectRatio: false }} <-- these options solved my issue

@lincolnthree
Copy link

@avisek-official But doesn't that mean the chart will no longer keep its aspect ratio if proportions go askew?

@avisek-official
Copy link

@lincolnthree I can't say this a fix for the issue, but it worked for me. Try it once in your project too and test anything breaks in any resolution. In my case it didn't break anytime with any data.

@mudassirzr
Copy link

Still experiencing this in 4.3.3, any fixes expected anytime soon?

@fly74
Copy link

fly74 commented Dec 5, 2023

Any new for this issue in the 4.4.1 release ?

@violuke
Copy link

violuke commented Dec 5, 2023

I really hope so 🙏

@fly74
Copy link

fly74 commented Jan 3, 2024

Sorry can't wait any longer. Move on to Grafana. :/

But I'm not angry, I used it for a long time.

@abdelouahabb
Copy link

The fix is still being reviewed ?
#11132

@Marian-Kechlibar
Copy link

I am experiencing the problem with the latest version of Chart as well.

@mnadalin
Copy link

mnadalin commented Feb 9, 2024

Version 4.4.1 and the problem is still there.
Disabling maintainAspectRatio it actually works. However, loading the page at 25% and zooming to 100% produces a way-too-blurry Chart. Not an option to me.

@v6ak
Copy link

v6ak commented Feb 9, 2024

If you want a workaround, look at #11224 (comment)

@jtomaszewski
Copy link

To me a workaround was to force the pixel ratio for charts to 1, by setting the devicePixelRatio: 1 option ( https://www.chartjs.org/docs/latest/configuration/device-pixel-ratio.html ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.