-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Make legend layouting respect padding at the start of columns #5776
Conversation
I think I understand the issue but I'm not sure about the fix: What about these changes: var maxHeight = minSize.height - vPadding;
// ...
helpers.each(me.legendItems, function(legendItem, i) {
// ...
if (currentColHeight + itemHeight > maxHeight) {
//... Does that fix the issue? |
Going into this direction would work as well. However, rather than adding the padding to the var maxHeight = minSize.height - vPadding; @simonbrunel Reverting my previous changes and going with this approach fixes this issue as well. What do you think? If you think we should go with this approach, I will commit the necessary changes. |
@jtagscherer you right, I fixed my previous comment, it should be I would go with the approach I suggested. Do you confirm that it fixes #5491? |
@simonbrunel Thank you for your suggestions, I have changed the code accordingly. Using a slightly adjusted version of the chart provided by the issue's author for testing, I can confirm that these changes fix #5491. |
@jtagscherer can you try to add a unit test for this case to prevent regression? |
@simonbrunel Sure thing! Should I add the unit test to this pull request or should I create a new one? |
In this PR but you will need to wait a bit since GitHub have some issues. |
Unfortunately, upon further testing and trying to create a minimal example for the unit test, this issue turned out to be of a larger scale than I expected. My changes did not fix the problem entirely. I'll try to outline it and offer some possibilities. The Is there any reason why we have to calculate the amount of columns twice, once within Alternatively, we could move the @simonbrunel What do you think? How should we go about this? |
I'm really not familiar with the layout code, maybe @etimberg have more ideas on this issue? |
I was originally trying to solve different legend layout issues in #5816, but realised that this PR is addressing a similar one. My solution to the |
@nagix You are right, that seems to be the root cause for both of our issues, while my changes only addressed the symptoms. I tested the version of your pull request against my two test cases, and it fixed the issue in both of them. Therefore, I'd suggest that we go with your approach and fix this issue at its root. Before that, it would be great if a maintainer that is familiar with the layout code could step in and explain why the current architecture with two diverging layout routines was chosen. Maybe there's a good reason for this, and our solution could cause even more problems. Finally, I think it would be a good idea to add another test case against the specific issue that I was addressing. That way, we can ensure that there will be no regression. @nagix I already have a local finished test case that I could add. I could commit it if you give me write access to your pull request's forked repository, or I could create a pull request for your repository with my changes. Whatever works best for you. |
I think the calculation in @jtagscherer Can you create a pull request for my repository? Then, I will merge it. |
@jtagscherer @nagix can we close that PR in favor of #5816? |
@simonbrunel Sorry for the delay. Yes, you can close this PR in favour of #5816. I would have added my unit test for the specific use case that the author of #5491 had an issue with to #5816 today, but I see that you have already merged it. That's not too bad though, since @nagix already added test cases for legends with multiple columns. We just won't have a regression test against that specific edge case in #5491. |
@jtagscherer thanks for the update (and your contribution) :) I totally forgot about the extra unit tests :\ sorry for that. @nagix, does your PR include regression tests for case described in #5491? If not and if it makes sense, @jtagscherer feel free to rebase and update this PR with these tests. |
@simonbrunel No worries! I'll rebase this PR to include my regression tests covering the specific situation from #5491 and then we can decide whether they are worth the addition. I'd say that you can never have enough regression tests :) |
@simonbrunel I have added a regression test for a simplified version of the issue in #5491. I have checked and can confirm that this test fails when run against the old version without #5816, but succeeds on the fixed version. |
Thanks @jtagscherer, this looks good to me. |
Thanks @jtagscherer |
The
draw
function of chart legends introduces a vertical padding at the very beginning of each column:Chart.js/src/plugins/plugin.legend.js
Lines 420 to 424 in 5816770
Before individual items of a legend are being drawn, the
fit
function performs some layouting. This includes the calculation of how many columns of items will be needed to fit all items into a vertical legend. However, this calculation does not consider the aforementioned initial column padding. Instead, all new columns are assigned a height of0
:Chart.js/src/plugins/plugin.legend.js
Lines 272 to 303 in 5816770
In some edge cases with specific amounts of legend items and chart heights, this lead to overflow bugs as seen in #5491. This pull request fixes this issue.
Fixes #5491