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

Fixes Label Array Tick alignment and Centering #5248

Merged
merged 4 commits into from Feb 9, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/core/core.scale.js
Expand Up @@ -857,6 +857,11 @@ module.exports = function(Chart) {

var label = itemToDraw.label;
if (helpers.isArray(label)) {
if (label.length % 2 === 0) {
Copy link
Member

Choose a reason for hiding this comment

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

what's the reason for this if? If I understand correctly, -tickFont.size * label.length 2 is half the height of the label so we should just move up by that amount. I think this is working around simply setting a different text baseline that would make the code cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason for that if is to check if there is an even amount of labels. If so then the labels get centered evenly since it would be a different calculation when compared to an odd amount of labels. I think you are meaning -tickFont.size * label.length / 2? I'm fairly new to the project but I think setting a different baseline could work as well like you said. Thank you for the review.

Copy link
Member

Choose a reason for hiding this comment

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

I also don't understand why we need to handle odd/even amount of lines differently and based on your screenshot, it doesn't seem correct when there is 2 lines (I would expect the text centered on the bar):

image

context.translate(0, (-tickFont.size * (label.length / 2)) / 2);
} else {
context.translate(0, -tickFont.size * (label.length / 2));
}
Copy link
Member

Choose a reason for hiding this comment

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

We need to use the line height (not only the font size) and it would be easier and faster to translate y instead of translating the context?

var lineCount = label.length;
var lineHeight = tickFont.size * 1.5;
var y = - lineHeight * lineCount / 2;
var i = 0;

for (; i < lineCount; ++i) {
  context.fillText('' + label[i], 0, y);
  y += lineHeight;
}

for (var i = 0, y = 0; i < label.length; ++i) {
// We just make sure the multiline element is a string here..
context.fillText('' + label[i], 0, y);
Expand Down