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

allow multiple lines of text in the line label #129

Merged
merged 8 commits into from Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -139,7 +139,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. Use \n to take a new line
content: "Test label"
},

Expand Down
45 changes: 37 additions & 8 deletions src/types/line.js
Expand Up @@ -155,12 +155,24 @@ 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 !== null && model.labelContent.indexOf("\n") !== -1) {
aoifef marked this conversation as resolved.
Show resolved Hide resolved
var labelContentArray = model.labelContent.split("\n");

var longestLabel = labelContentArray.sort(function (a, b) { return b.length - a.length; })[0];
aoifef marked this conversation as resolved.
Show resolved Hide resolved
textWidth = ctx.measureText(longestLabel).width;

model.labelHeight = (textHeight * labelContentArray.length) + (2 * model.labelYPadding);
//Add padding for in between each label item
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a space after // and remove extraneous word "for"

model.labelHeight += (5 * (labelContentArray.length - 1));
aoifef marked this conversation as resolved.
Show resolved Hide resolved
}

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;
Expand Down Expand Up @@ -253,12 +265,29 @@ 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 !== null && view.labelContent.indexOf("\n") !== -1) {
aoifef marked this conversation as resolved.
Show resolved Hide resolved
var textYPosition = view.labelY + view.labelYPadding;
var labelContentArray = view.labelContent.split("\n");

for(var i = 0; i < labelContentArray.length; i++) {
aoifef marked this conversation as resolved.
Show resolved Hide resolved
ctx.textBaseline = 'top';
ctx.fillText(
labelContentArray[i],
view.labelX + (view.labelWidth / 2),
textYPosition
);

textYPosition += (view.labelFontSize + 5);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these parentheses are unnecessary

}
} else {
ctx.textBaseline = 'middle';
ctx.fillText(
view.labelContent,
view.labelX + (view.labelWidth / 2),
view.labelY + (view.labelHeight / 2)
);
}
}

ctx.restore();
Expand Down