From fdafa467a83e030ce66e3805f29f233d1c9ab40d Mon Sep 17 00:00:00 2001 From: Aoife Finnegan Date: Tue, 30 Apr 2019 17:44:14 +0100 Subject: [PATCH] allow multiple lines of text in the line label (#129) allow multiple lines of text in the line label (#129) --- README.md | 2 +- src/types/line.js | 44 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 1de35759f..3005c951c 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ Vertical or horizontal lines are supported. // Whether the label is enabled and should be displayed enabled: false, - // Text to display in label - default is null + // Text to display in label - default is null. Provide an array to display values on a new line content: "Test label" }, diff --git a/src/types/line.js b/src/types/line.js index 937faad79..566652ecb 100644 --- a/src/types/line.js +++ b/src/types/line.js @@ -157,12 +157,25 @@ module.exports = function(Chart) { ctx.font = chartHelpers.fontString(model.labelFontSize, model.labelFontStyle, model.labelFontFamily); var textWidth = ctx.measureText(model.labelContent).width; - var textHeight = ctx.measureText('M').width; + var textHeight = model.labelFontSize; + model.labelHeight = textHeight + (2 * model.labelYPadding); + + if (model.labelContent && chartHelpers.isArray(model.labelContent)) { + var labelContentArray = model.labelContent.slice(0); + var longestLabel = labelContentArray.sort(function(a, b) { + return b.length - a.length; + })[0]; + textWidth = ctx.measureText(longestLabel).width; + + model.labelHeight = (textHeight * model.labelContent.length) + (2 * model.labelYPadding); + // Add padding in between each label item + model.labelHeight += model.labelYPadding * (model.labelContent.length - 1); + } + var labelPosition = calculateLabelPosition(model, textWidth, textHeight, model.labelXPadding, model.labelYPadding); model.labelX = labelPosition.x - model.labelXPadding; model.labelY = labelPosition.y - model.labelYPadding; model.labelWidth = textWidth + (2 * model.labelXPadding); - model.labelHeight = textHeight + (2 * model.labelYPadding); model.borderColor = options.borderColor; model.borderWidth = options.borderWidth; @@ -255,12 +268,27 @@ module.exports = function(Chart) { ); ctx.fillStyle = view.labelFontColor; ctx.textAlign = 'center'; - ctx.textBaseline = 'middle'; - ctx.fillText( - view.labelContent, - view.labelX + (view.labelWidth / 2), - view.labelY + (view.labelHeight / 2) - ); + + if (view.labelContent && chartHelpers.isArray(view.labelContent)) { + var textYPosition = view.labelY + view.labelYPadding; + for (var i = 0; i < view.labelContent.length; i++) { + ctx.textBaseline = 'top'; + ctx.fillText( + view.labelContent[i], + view.labelX + (view.labelWidth / 2), + textYPosition + ); + + textYPosition += view.labelFontSize + view.labelYPadding; + } + } else { + ctx.textBaseline = 'middle'; + ctx.fillText( + view.labelContent, + view.labelX + (view.labelWidth / 2), + view.labelY + (view.labelHeight / 2) + ); + } } ctx.restore();