Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
align color boxes - try 2
  • Loading branch information
kurkle committed Dec 24, 2018
commit 1f22e6032668f234d3d93c8433cc0dc174c9912e
24 changes: 14 additions & 10 deletions src/core/core.tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,13 +774,16 @@ var exports = Element.extend({
var bodyFontSize = vm.bodyFontSize;
var bodySpacing = vm.bodySpacing;
var body = vm.body;
var drawColorBoxes = vm.displayColors;
var textColor, textWidth;
var xLinePadding = 0;
var textX = pt.x;

ctx.textAlign = vm._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 @@ -790,28 +793,30 @@ var exports = Element.extend({
ctx.fillStyle = vm.bodyFontColor;
helpers.each(vm.beforeBody, fillLineOfText);

var drawColorBoxes = vm.displayColors;
xLinePadding = drawColorBoxes ? (bodyFontSize + 2) : 0;
xLinePadding = drawColorBoxes && vm._bodyAlign !== 'right'
? vm._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 (vm._bodyAlign) {
case 'center':
xLinePadding /= 2;
pt.x -= ctx.measureText(line).width / 2 + xLinePadding;
pt.x = textX - textWidth / 2 - xLinePadding;
break;
case 'right':
xLinePadding = 0;
xAlignText(pt, vm, 'left');
pt.x = textX - textWidth - bodyFontSize - 2;
break;
default:
pt.x = textX;
break;
}

Expand All @@ -829,8 +834,7 @@ var exports = Element.extend({
ctx.fillRect(pt.x + 1, pt.y + 1, bodyFontSize - 2, bodyFontSize - 2);
ctx.fillStyle = textColor;

// re-align for center / right
xAlignText(pt, vm, vm._bodyAlign);
pt.x = textX;
}

fillLineOfText(line);
Expand Down