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

Measure text size for autoscale when rotated #5252

Closed
wants to merge 2 commits into from

Conversation

teroman
Copy link
Contributor

@teroman teroman commented Feb 8, 2018

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.

@benmccann
Copy link
Contributor

@teroman would you be able to rebase this PR? thanks!

@benmccann
Copy link
Contributor

@teroman a quick reminder to rebase this when you get a chance

@teroman
Copy link
Contributor Author

teroman commented Apr 25, 2018

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?

@benmccann
Copy link
Contributor

@teroman I always do the following (doesn't involve a hash):

git checkout master (switch to main branch)
git remote add upstream git@github.com:chartjs/Chart.js.git (add chartjs as upstream repo)
git fetch upstream (get the latest changes)
git merge upstream/master (apply to your master branch)
git checkout YOURBRANCH (switch to the branch with your changes)
git rebase master (rebase against master)
FIX CONFLICTS
git add . (mark conflicting files as fixed)
git rebase --continue (finish rebase now that conflicts are all fixed)
git push -f (push the changes to this PR)

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.
@teroman
Copy link
Contributor Author

teroman commented Apr 26, 2018

Well that first commit didn't work! Hopefully the second is okay, if so I'll rebase my other PR.

@teroman
Copy link
Contributor Author

teroman commented Apr 26, 2018

Thanks @benmccann for the advice, a bit of ssh key tinkering and I got things working.

Copy link
Member

@simonbrunel simonbrunel left a 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);
Copy link
Member

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:

image

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?

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

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

My suggested skipSize wasn't optimal, correct formula in this pen, implemented in #5922.

@benmccann
Copy link
Contributor

@teroman would you be able to update this PR to address Simon's comments?

@benmccann
Copy link
Contributor

@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

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.

None yet

4 participants