Fix #10749 - backdrops with rotated labels (#10759)

* Fix #10749 - backdrops with rotated labels

* remove translation adjustment
Because backdrop now occurs after translation, we don't want to double-adjust the position.

* increase tolerance slightly due to anti-aliasing

Co-authored-by: Charles McNulty <charles.mcnulty@tasconline.com>
This commit is contained in:
cmcnulty 2022-10-12 06:08:22 -05:00 committed by GitHub
parent b0160e138a
commit fbf3427ca2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 7 deletions

View File

@ -1253,8 +1253,8 @@ export default class Scale extends Element {
const height = labelSizes.heights[i]; const height = labelSizes.heights[i];
const width = labelSizes.widths[i]; const width = labelSizes.widths[i];
let top = y + textOffset - labelPadding.top; let top = textOffset - labelPadding.top;
let left = x - labelPadding.left; let left = 0 - labelPadding.left;
switch (textBaseline) { switch (textBaseline) {
case 'middle': case 'middle':
@ -1554,11 +1554,6 @@ export default class Scale extends Element {
const tickFont = item.font; const tickFont = item.font;
const label = item.label; const label = item.label;
if (item.backdrop) {
ctx.fillStyle = item.backdrop.color;
ctx.fillRect(item.backdrop.left, item.backdrop.top, item.backdrop.width, item.backdrop.height);
}
let y = item.textOffset; let y = item.textOffset;
renderText(ctx, label, 0, y, tickFont, item); renderText(ctx, label, 0, y, tickFont, item);
} }

View File

@ -340,6 +340,10 @@ export function renderText(ctx, text, x, y, font, opts = {}) {
for (i = 0; i < lines.length; ++i) { for (i = 0; i < lines.length; ++i) {
line = lines[i]; line = lines[i];
if (opts.backdrop) {
drawBackdrop(ctx, opts.backdrop);
}
if (stroke) { if (stroke) {
if (opts.strokeColor) { if (opts.strokeColor) {
ctx.strokeStyle = opts.strokeColor; ctx.strokeStyle = opts.strokeColor;
@ -408,6 +412,14 @@ function decorateText(ctx, x, y, line, opts) {
} }
} }
function drawBackdrop(ctx, opts) {
const oldColor = ctx.fillStyle;
ctx.fillStyle = opts.color;
ctx.fillRect(opts.left, opts.top, opts.width, opts.height);
ctx.fillStyle = oldColor;
}
/** /**
* Add a path of a rectangle with rounded corners to the current sub-path * Add a path of a rectangle with rounded corners to the current sub-path
* @param {CanvasRenderingContext2D} ctx Context * @param {CanvasRenderingContext2D} ctx Context

View File

@ -0,0 +1,85 @@
const grid = {
display: false
};
const title = {
display: false,
};
module.exports = {
tolerance: 0.0016,
config: {
type: 'line',
options: {
events: [],
scales: {
top: {
type: 'linear',
position: 'top',
ticks: {
display: true,
showLabelBackdrop: true,
minRotation: 45,
backdropColor: 'blue',
backdropPadding: 5,
align: 'start',
crossAlign: 'near',
},
grid,
title
},
left: {
type: 'linear',
position: 'left',
ticks: {
display: true,
showLabelBackdrop: true,
minRotation: 90,
backdropColor: 'green',
backdropPadding: {
x: 2,
y: 5
},
crossAlign: 'center',
},
grid,
title
},
bottom: {
type: 'linear',
position: 'bottom',
ticks: {
display: true,
showLabelBackdrop: true,
backdropColor: 'blue',
backdropPadding: {
x: 5,
y: 5
},
align: 'end',
crossAlign: 'far',
minRotation: 60,
},
grid,
title
},
right: {
type: 'linear',
position: 'right',
ticks: {
display: true,
showLabelBackdrop: true,
backdropColor: 'gray',
},
grid,
title
},
}
}
},
options: {
canvas: {
height: 256,
width: 256
},
spriteText: true,
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB