Measure text size for autoscale when rotated#5252
Measure text size for autoscale when rotated#5252teroman wants to merge 2 commits intochartjs:masterfrom
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. |
simonbrunel
left a comment
There was a problem hiding this comment.
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.
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.
Great diagram @simonbrunel. I agree that this is the correct formula for the width of the text.
|
@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.