-
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
Measure text size for autoscale when rotated #5252
Conversation
@teroman would you be able to rebase this PR? thanks! |
@teroman a quick reminder to rebase this when you get a chance |
Hi, I've not rebased a patch before, do you know of a good guide? I tried cloning my patch branch locally and adding a Chart.js as remote repo, but got stuck when using the hash from merge-base with the rebase command. I thought it'd "just work" but instead I got some picking options and am not sure what to do. Am I even close to doing the right thing? |
@teroman I always do the following (doesn't involve a hash):
|
Currently this uses the cos() of the rotation to calculate the width of rotated text. cos(90) = 0 so the text size is though to be zero, and no labels are skipped. Added a check to use the sin() of the width and use that if greater. sin(90) = 1, so in this case the text height is used as the width taken of the tick text when drawn.
Well that first commit didn't work! Hopefully the second is okay, if so I'll rebase my other PR. |
Thanks @benmccann for the advice, a bit of ssh key tinkering and I got things working. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to have unit tests to prevent regression.
var longestRotatedLabel = me.longestLabelWidth * cosRotation; | ||
var cosRotation = Math.abs(Math.cos(labelRotationRadians)); | ||
var sinRotation = Math.abs(Math.sin(labelRotationRadians)); | ||
var longestRotatedLabel = Math.max(me.longestLabelWidth * cosRotation, me.options.ticks.fontSize * sinRotation); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that's correct (neither the original formula) because it works only with a line (width or height), not a box (width and height). When angle is not 0, 90, 180, 270, we should take in account both dimensions:
var rot = helpers.toRadians(me.labelRotation);
var cos = Math.abs(Math.cos(rot));
var sin = Math.abs(Math.sin(rot));
var width = me.longestLabelWidth;
var height = me.options.ticks.fontSize;
var skipSize = (width * cos) + (height * sin)
// In case we want to support auto-skip on vertical scales
// var skipSize = (width * sin) + (height * cos)
Also, me.options.ticks.fontSize
works only for single line label, should we also support multilines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great diagram @simonbrunel. I agree that this is the correct formula for the width of the text.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@teroman would you be able to update this PR to address Simon's comments? |
@teroman I'd really love to see this PR get merged. Will you be updating to address the comments? If not, I may have to close this PR at some point |
Currently this uses the cos() of the rotation to calculate the width of rotated text.
cos(90) = 0 so the text size is thought to be zero, and no labels are skipped.
Added a check to use the sin() of the width and use that if greater.
sin(90) = 1, so in this case the text height is used as the width taken of the tick text when drawn.