Skip to content

Commit

Permalink
Fix vertical alignment of legend labels (#4318)
Browse files Browse the repository at this point in the history
Ensure that disabled legend style is drawn in the center of the text and that the text is correctly centered in the box.
  • Loading branch information
etimberg authored and simonbrunel committed Jun 4, 2017
1 parent fd49ac9 commit a930830
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/plugins/plugin.legend.js
Expand Up @@ -320,7 +320,7 @@ module.exports = function(Chart) {

// Canvas setup
ctx.textAlign = 'left';
ctx.textBaseline = 'top';
ctx.textBaseline = 'middle';
ctx.lineWidth = 0.5;
ctx.strokeStyle = fontColor; // for strikethrough effect
ctx.fillStyle = fontColor; // render in correct colour
Expand Down Expand Up @@ -372,14 +372,18 @@ module.exports = function(Chart) {
ctx.restore();
};
var fillText = function(x, y, legendItem, textWidth) {
ctx.fillText(legendItem.text, boxWidth + (fontSize / 2) + x, y);
var halfFontSize = fontSize / 2;
var xLeft = boxWidth + halfFontSize + x;
var yMiddle = y + halfFontSize;

ctx.fillText(legendItem.text, xLeft, yMiddle);

if (legendItem.hidden) {
// Strikethrough the text if hidden
ctx.beginPath();
ctx.lineWidth = 2;
ctx.moveTo(boxWidth + (fontSize / 2) + x, y + (fontSize / 2));
ctx.lineTo(boxWidth + (fontSize / 2) + x + textWidth, y + (fontSize / 2));
ctx.moveTo(xLeft, yMiddle);
ctx.lineTo(xLeft + textWidth, yMiddle);
ctx.stroke();
}
};
Expand Down

0 comments on commit a930830

Please sign in to comment.