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 3 commits
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
8 changes: 6 additions & 2 deletions src/core/core.scale.js
Expand Up @@ -857,11 +857,15 @@ module.exports = function(Chart) {

var label = itemToDraw.label;
if (helpers.isArray(label)) {
for (var i = 0, y = 0; i < label.length; ++i) {
var lineCount = 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.

Should be var lineCount = label.length; and re-used in the for loop.

var lineHeight = tickFont.size * 1.5;
var y = me.isHorizontal() ? 0 : -lineHeight * lineCount / 1.2;
Copy link
Member

Choose a reason for hiding this comment

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

I think the correct calculation depends on the actual text baseline:

a. if textBaseline: 'top' then y = - lineHeight * lineCount / 2;
b. if textBaseline: 'middle' then y = - lineHeight * (lineCount - 1) / 2;
c. if textBaseline: 'bottom' then y = - lineHeight * (lineCount - 2) / 2;

By default, textBaseline === 'middle' (line 738), so b. should work in our case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh ok makes sense, b does work in this case. Is it necessary to adjust the code for each textBaseline value?

Copy link
Member

Choose a reason for hiding this comment

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

Since it's always middle for vertical scale, I would only implement b. for now.


for (var i = 0; i < label.length; ++i) {
Copy link
Member

Choose a reason for hiding this comment

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

We should use lineCount instead of label.length

// We just make sure the multiline element is a string here..
context.fillText('' + label[i], 0, y);
// apply same lineSpacing as calculated @ L#320
y += (tickFont.size * 1.5);
y += lineHeight;
}
} else {
context.fillText(label, 0, 0);
Expand Down