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

Fix rtl legend text alignment #9199

Merged
merged 3 commits into from Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/helpers/helpers.extras.js
Expand Up @@ -81,6 +81,10 @@ export const _alignStartEnd = (align, start, end) => align === 'start' ? start :
* @param {string} align start, end, center
* @param {number} left value for start
* @param {number} right value for end
* @param {boolean} rtl Is this an RTL draw
* @private
*/
export const _textX = (align, left, right) => align === 'right' ? right : align === 'center' ? (left + right) / 2 : left;
export const _textX = (align, left, right, rtl) => {
const check = rtl ? 'left' : 'right';
return align === check ? right : align === 'center' ? (left + right) / 2 : left;
};
6 changes: 3 additions & 3 deletions src/plugins/plugin.legend.js
Expand Up @@ -371,7 +371,7 @@ export class Legend extends Element {
const fillText = function(x, y, legendItem) {
renderText(ctx, legendItem.text, x, y + (itemHeight / 2), labelFont, {
strikethrough: legendItem.hidden,
textAlign: legendItem.textAlign
textAlign: rtlHelper.textAlign(legendItem.textAlign)
});
};

Expand Down Expand Up @@ -402,7 +402,7 @@ export class Legend extends Element {

const textWidth = ctx.measureText(legendItem.text).width;
const textAlign = rtlHelper.textAlign(legendItem.textAlign || (legendItem.textAlign = labelOpts.textAlign));
const width = boxWidth + (fontSize / 2) + textWidth;
const width = boxWidth + halfFontSize + textWidth;
let x = cursor.x;
let y = cursor.y;

Expand All @@ -424,7 +424,7 @@ export class Legend extends Element {

drawLegendBox(realX, y, legendItem);

x = _textX(textAlign, x + boxWidth + halfFontSize, me.right);
x = _textX(textAlign, x + boxWidth + halfFontSize, isHorizontal ? x + width : me.right, opts.rtl);

// Fill the actual label
fillText(rtlHelper.x(x), y, legendItem);
Expand Down
30 changes: 30 additions & 0 deletions test/fixtures/plugin.legend/label-textAlign/horizontal-left.js
@@ -0,0 +1,30 @@
module.exports = {
config: {
type: 'pie',
data: {
labels: ['aaaa', 'bb', 'c'],
datasets: [
{
data: [1, 2, 3]
}
]
},
options: {
plugins: {
legend: {
position: 'top',
labels: {
textAlign: 'left'
}
}
}
}
},
options: {
spriteText: true,
canvas: {
width: 256,
height: 256
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions test/fixtures/plugin.legend/label-textAlign/horizontal-right.js
@@ -0,0 +1,30 @@
module.exports = {
config: {
type: 'pie',
data: {
labels: ['aaaa', 'bb', 'c'],
datasets: [
{
data: [1, 2, 3]
}
]
},
options: {
plugins: {
legend: {
position: 'top',
labels: {
textAlign: 'right'
}
}
}
}
},
options: {
spriteText: true,
canvas: {
width: 256,
height: 256
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions test/fixtures/plugin.legend/label-textAlign/horizontal-rtl-left.js
@@ -0,0 +1,31 @@
module.exports = {
config: {
type: 'pie',
data: {
labels: ['aaaa', 'bb', 'c'],
datasets: [
{
data: [1, 2, 3]
}
]
},
options: {
plugins: {
legend: {
position: 'top',
rtl: true,
labels: {
textAlign: 'left'
}
}
}
}
},
options: {
spriteText: true,
canvas: {
width: 256,
height: 256
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,31 @@
module.exports = {
config: {
type: 'pie',
data: {
labels: ['aaaa', 'bb', 'c'],
datasets: [
{
data: [1, 2, 3]
}
]
},
options: {
plugins: {
legend: {
rtl: true,
position: 'top',
labels: {
textAlign: 'right'
}
}
}
}
},
options: {
spriteText: true,
canvas: {
width: 256,
height: 256
}
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/plugin.legend/label-textAlign/rtl-left.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/fixtures/plugin.legend/label-textAlign/rtl-right.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.