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

Align title, body and footer inside tooltip #5925

Merged
merged 9 commits into from Jan 8, 2019
Merged
Changes from 6 commits
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
59 changes: 51 additions & 8 deletions src/core/core.tooltip.js
Expand Up @@ -463,6 +463,25 @@ function getBackgroundPoint(vm, size, alignment, chart) {
};
}

/**
* Align pt.x to match given align (and using xPadding)
kurkle marked this conversation as resolved.
Show resolved Hide resolved
* @param {object} pt - point to align {x}
* @param {object} vm - rectangle text should be aligned in (x, y, width, height, xPadding)
* @param {string} align - left, center, right
kurkle marked this conversation as resolved.
Show resolved Hide resolved
*/
function xAlignText(pt, vm, align) {
switch (align) {
case 'center':
pt.x = vm.x + vm.width / 2;
break;
case 'right':
pt.x = vm.x + vm.width - vm.xPadding;
break;
default:
pt.x = vm.x + vm.xPadding;
}
}

/**
* Helper to build before and after body lines
*/
Expand Down Expand Up @@ -754,14 +773,19 @@ var exports = Element.extend({
drawBody: function(pt, vm, ctx) {
var bodyFontSize = vm.bodyFontSize;
var bodySpacing = vm.bodySpacing;
var bodyAlign = vm._bodyAlign;
var body = vm.body;
var drawColorBoxes = vm.displayColors;
var labelColors = vm.labelColors;
var textColor, textWidth;
var xLinePadding = 0;
var textX = pt.x;

ctx.textAlign = vm._bodyAlign;
ctx.textAlign = bodyAlign;
ctx.textBaseline = 'top';
ctx.font = helpers.fontString(bodyFontSize, vm._bodyFontStyle, vm._bodyFontFamily);

// Before Body
var xLinePadding = 0;
var fillLineOfText = function(line) {
ctx.fillText(line, pt.x + xLinePadding, pt.y);
pt.y += bodyFontSize + bodySpacing;
Expand All @@ -771,31 +795,48 @@ var exports = Element.extend({
ctx.fillStyle = vm.bodyFontColor;
helpers.each(vm.beforeBody, fillLineOfText);

var drawColorBoxes = vm.displayColors;
xLinePadding = drawColorBoxes ? (bodyFontSize + 2) : 0;
xLinePadding = drawColorBoxes && bodyAlign !== 'right'
? bodyAlign === 'center' ? (bodyFontSize / 2 + 1) : (bodyFontSize + 2)
: 0;

// Draw body lines now
helpers.each(body, function(bodyItem, i) {
var textColor = vm.labelTextColors[i];
pt.x = textX;
textColor = vm.labelTextColors[i];
ctx.fillStyle = textColor;
helpers.each(bodyItem.before, fillLineOfText);

helpers.each(bodyItem.lines, function(line) {
// Draw Legend-like boxes if needed
if (drawColorBoxes) {
textWidth = ctx.measureText(line).width;
switch (bodyAlign) {
case 'center':
pt.x = textX - textWidth / 2 - xLinePadding;
break;
case 'right':
pt.x = textX - textWidth - bodyFontSize - 2;
break;
default:
pt.x = textX;
break;
}

// Fill a white rect so that colours merge nicely if the opacity is < 1
ctx.fillStyle = vm.legendColorBackground;
ctx.fillRect(pt.x, pt.y, bodyFontSize, bodyFontSize);

// Border
ctx.lineWidth = 1;
ctx.strokeStyle = vm.labelColors[i].borderColor;
ctx.strokeStyle = labelColors[i].borderColor;
ctx.strokeRect(pt.x, pt.y, bodyFontSize, bodyFontSize);

// Inner square
ctx.fillStyle = vm.labelColors[i].backgroundColor;
ctx.fillStyle = labelColors[i].backgroundColor;
ctx.fillRect(pt.x + 1, pt.y + 1, bodyFontSize - 2, bodyFontSize - 2);
ctx.fillStyle = textColor;

pt.x = textX;
kurkle marked this conversation as resolved.
Show resolved Hide resolved
}

fillLineOfText(line);
Expand Down Expand Up @@ -905,16 +946,18 @@ var exports = Element.extend({
this.drawBackground(pt, vm, ctx, tooltipSize);

// Draw Title, Body, and Footer
pt.x += vm.xPadding;
pt.y += vm.yPadding;

// Titles
xAlignText(pt, vm, vm._titleAlign);
this.drawTitle(pt, vm, ctx);

// Body
xAlignText(pt, vm, vm._bodyAlign);
this.drawBody(pt, vm, ctx);

// Footer
xAlignText(pt, vm, vm._footerAlign);
this.drawFooter(pt, vm, ctx);

ctx.restore();
Expand Down