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

Fix tooltip caret position when it is positioned at the corners #9922

Merged
merged 2 commits into from Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/plugins/plugin.tooltip.js
Expand Up @@ -306,9 +306,9 @@ function getBackgroundPoint(options, size, alignment, chart) {
x -= paddingAndSize;
}
} else if (xAlign === 'left') {
x -= Math.max(topLeft, bottomLeft) + caretPadding;
x -= Math.max(topLeft, bottomLeft) + caretSize;
} else if (xAlign === 'right') {
x += Math.max(topRight, bottomRight) + caretPadding;
x += Math.max(topRight, bottomRight) + caretSize;
}

return {
Expand Down
70 changes: 70 additions & 0 deletions test/fixtures/plugin.tooltip/caret-position.js
@@ -0,0 +1,70 @@
const data = [];
for (let x = 1; x < 4; x++) {
for (let y = 1; y < 4; y++) {
data.push({x, y});
}
}

module.exports = {
config: {
type: 'scatter',
data: {
datasets: [{
data,
backgroundColor: 'red',
radius: 8,
hoverRadius: 0
}],
},
options: {
scales: {
x: {display: false, min: 0.96, max: 3.04},
y: {display: false, min: 1, max: 3}
},
plugins: {
legend: false,
title: false,
filler: false,
tooltip: {
mode: 'point',
intersect: true,
// spriteText: use white background to hide any gaps between fonts
backgroundColor: 'white',
borderColor: 'black',
borderWidth: 1,
callbacks: {
label: () => 'label',
}
},
},
},
plugins: [{
afterDraw: function(chart) {
const canvas = chart.canvas;
const rect = canvas.getBoundingClientRect();
const meta = chart.getDatasetMeta(0);
let point, event;

for (let i = 0; i < data.length; i++) {
point = meta.data[i];
event = {
type: 'mousemove',
target: canvas,
clientX: rect.left + point.x,
clientY: rect.top + point.y
};
chart._handleEvent(event);
chart.tooltip.handleEvent(event);
chart.tooltip.draw(chart.ctx);
}
}
}]
},
options: {
spriteText: true,
canvas: {
height: 240,
width: 320
}
}
};
Binary file added test/fixtures/plugin.tooltip/caret-position.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.