From a9308301e34d3fb39462a8ed24e0fd09c7f388ef Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Sun, 4 Jun 2017 13:34:05 -0400 Subject: [PATCH] Fix vertical alignment of legend labels (#4318) Ensure that disabled legend style is drawn in the center of the text and that the text is correctly centered in the box. --- src/plugins/plugin.legend.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/plugins/plugin.legend.js b/src/plugins/plugin.legend.js index cd52b1f9aee..3d898ad3f14 100644 --- a/src/plugins/plugin.legend.js +++ b/src/plugins/plugin.legend.js @@ -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 @@ -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(); } };