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

Properly initialize variables if ticks aren't being displayed #6100

Merged
merged 4 commits into from
Mar 4, 2019

Conversation

benmccann
Copy link
Contributor

@benmccann benmccann commented Mar 1, 2019

Closes #6095

_autoSkip depends on _maxLabelLines being defined which only occurs when tickOpts.display && display as discussed in #6095

This is also a performance improvement in the case that !tickOpts.display

nagix
nagix previously approved these changes Mar 2, 2019
@simonbrunel simonbrunel requested review from kurkle and etimberg March 3, 2019 13:58
@simonbrunel
Copy link
Member

Instead of "preventing" accessing a method (_tickSize via _autoSkip) which work only under certains conditions (error prone), we should maybe make sure that _maxLabelLines is correctly initialized? It's the same with longestLabelWidth and I think they should be initialized to 0 if tick labels are not visible. In this case, _tickSize would return 0 in both directions, which should naturally disable auto skipping.

etimberg
etimberg previously approved these changes Mar 3, 2019
@etimberg
Copy link
Member

etimberg commented Mar 3, 2019

I agree with @simonbrunel as well that we should fix the underlying issue as well. This works well as a stop-gap, but if we can ensure everything is correctly initialized that will prevent these errors in the future

@kurkle
Copy link
Member

kurkle commented Mar 3, 2019

  • Currently _tickSize returns NaN when axis or ticks are hidden.
  • _tickSize is only called from _autoSkip.
  • _tickSize result is compared to axisLength here
  • NaN in not greater than any finite number
  • nothing more is done with that result in that case.

So currently there is no bug that needs to be fixed.
This PR however eliminates couple of function calls and calculations, so its a (tiny) performance improvement. If we are looking for performance improvements, I'd look into calling _autoSkip in update rather than draw.

I also agree that those properties used should be initialized, but currently its not critical (does not affect functionality in any way).

I'd rather work out possible issues in a bit more substantial PR, like #5961

Edit: _autoSkip re-creates ticks array anyway, so removed (tiny).

@simonbrunel
Copy link
Member

I agree with the suggested change of not calling _autoSkip when ticks are not displayed, however we should expect methods to be callable in any context without worrying about its implementation. I'm fine with _maxLabelLines and longestLabelWidth to be undefined but then _tickSize should return a valid number (e.g. 0) when called in such cases.

I would maybe change these lines for:

var w = (me.longestLabelWidth + padding) || 0;
//...
var h = (me._maxLabelLines * tickFont.lineHeight + padding) || 0;

Though, I also agree it's not currently critical, just some code sanitation in case someone would like to use _tickSize later and expecting a valid number as returned value.

@kurkle
Copy link
Member

kurkle commented Mar 3, 2019

I would maybe change these lines for:

var w = (me.longestLabelWidth + padding) || 0;
//...
var h = (me._maxLabelLines * tickFont.lineHeight + padding) || 0;

That was my initial suggestion to #6095

kurkle
kurkle previously approved these changes Mar 3, 2019
@benmccann benmccann dismissed stale reviews from kurkle, etimberg, and nagix via 719ab0d March 3, 2019 18:03
@benmccann
Copy link
Contributor Author

I've updated the code. I set me.longestLabelWidth and me._maxLabelLines to 0. That makes sure those variables are correctly set in case someone depends on the value (longestLabelWidth is public) and has the same result of w and h both ending up being 0. I think it's technically correct as well. If there are no labels then it makes sense to me that longestLabelWidth is 0 and _maxLabelLines is 0.

@benmccann benmccann changed the title Don't calculate autoSkip if ticks aren't being displayed Properly initialize variables if ticks aren't being displayed Mar 3, 2019
src/core/core.scale.js Outdated Show resolved Hide resolved
src/core/core.scale.js Outdated Show resolved Hide resolved
Copy link
Member

@kurkle kurkle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems quite solid to me now. If I'm right (and it was allowed), would also fixes _autoSkip for vertical scale with rotate labels, by storing the longestLabelWidth always.

@simonbrunel simonbrunel added this to the Version 2.8 milestone Mar 4, 2019
@simonbrunel simonbrunel merged commit 858cc80 into chartjs:master Mar 4, 2019
@simonbrunel
Copy link
Member

Thanks @benmccann

exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants