Skip to content

Commit

Permalink
Fix rtl legend text alignment (#9199)
Browse files Browse the repository at this point in the history
* Start fixing RTL

* Broken test

* Fix right alignment for horizontal legends
  • Loading branch information
etimberg committed Jun 1, 2021
1 parent 599e23a commit be01176
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 4 deletions.
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.

0 comments on commit be01176

Please sign in to comment.