Skip to content

Commit f10fab6

Browse files
kurkledigaus
andauthored
Fix tooltip caret position when it is positioned at the corners (#9922)
* Fix tooltip caret position when at the corners * Add test Co-authored-by: Dirk Gausmann <di.gaus@gmx.de>
1 parent 9345250 commit f10fab6

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

src/plugins/plugin.tooltip.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ function getBackgroundPoint(options, size, alignment, chart) {
306306
x -= paddingAndSize;
307307
}
308308
} else if (xAlign === 'left') {
309-
x -= Math.max(topLeft, bottomLeft) + caretPadding;
309+
x -= Math.max(topLeft, bottomLeft) + caretSize;
310310
} else if (xAlign === 'right') {
311-
x += Math.max(topRight, bottomRight) + caretPadding;
311+
x += Math.max(topRight, bottomRight) + caretSize;
312312
}
313313

314314
return {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const data = [];
2+
for (let x = 1; x < 4; x++) {
3+
for (let y = 1; y < 4; y++) {
4+
data.push({x, y});
5+
}
6+
}
7+
8+
module.exports = {
9+
config: {
10+
type: 'scatter',
11+
data: {
12+
datasets: [{
13+
data,
14+
backgroundColor: 'red',
15+
radius: 8,
16+
hoverRadius: 0
17+
}],
18+
},
19+
options: {
20+
scales: {
21+
x: {display: false, min: 0.96, max: 3.04},
22+
y: {display: false, min: 1, max: 3}
23+
},
24+
plugins: {
25+
legend: false,
26+
title: false,
27+
filler: false,
28+
tooltip: {
29+
mode: 'point',
30+
intersect: true,
31+
// spriteText: use white background to hide any gaps between fonts
32+
backgroundColor: 'white',
33+
borderColor: 'black',
34+
borderWidth: 1,
35+
callbacks: {
36+
label: () => 'label',
37+
}
38+
},
39+
},
40+
},
41+
plugins: [{
42+
afterDraw: function(chart) {
43+
const canvas = chart.canvas;
44+
const rect = canvas.getBoundingClientRect();
45+
const meta = chart.getDatasetMeta(0);
46+
let point, event;
47+
48+
for (let i = 0; i < data.length; i++) {
49+
point = meta.data[i];
50+
event = {
51+
type: 'mousemove',
52+
target: canvas,
53+
clientX: rect.left + point.x,
54+
clientY: rect.top + point.y
55+
};
56+
chart._handleEvent(event);
57+
chart.tooltip.handleEvent(event);
58+
chart.tooltip.draw(chart.ctx);
59+
}
60+
}
61+
}]
62+
},
63+
options: {
64+
spriteText: true,
65+
canvas: {
66+
height: 240,
67+
width: 320
68+
}
69+
}
70+
};
13.5 KB
Loading

0 commit comments

Comments
 (0)