Skip to content

Commit 25873c0

Browse files
committed
[tooltips-closest demo] fix tooltip visibility on plot (re)entry. clip interpolated markers to plot region.
1 parent c1c09a0 commit 25873c0

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

demos/tooltips-closest.html

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,29 @@
6262

6363
const fmtDate = uPlot.fmtDate("{M}/{D}/{YY} {h}:{mm}:{ss} {AA}");
6464

65+
let over;
66+
67+
let tooltipVisible = false;
68+
69+
function showTooltip() {
70+
if (!tooltipVisible) {
71+
tooltip.style.display = "block";
72+
over.style.cursor = "pointer";
73+
tooltipVisible = true;
74+
}
75+
}
76+
77+
function hideTooltip() {
78+
if (tooltipVisible) {
79+
tooltip.style.display = "none";
80+
over.style.cursor = null;
81+
tooltipVisible = false;
82+
}
83+
}
84+
6585
function setTooltip(u) {
86+
showTooltip();
87+
6688
let top = u.valToPos(u.data[seriesIdx][dataIdx], 'y');
6789
let lft = u.valToPos(u.data[ 0][dataIdx], 'x');
6890

@@ -81,7 +103,7 @@
81103
hooks: {
82104
ready: [
83105
u => {
84-
let over = u.root.querySelector(".u-over");
106+
over = u.root.querySelector(".u-over");
85107
tooltipLeftOffset = parseFloat(over.style.left);
86108
tooltipTopOffset = parseFloat(over.style.top);
87109
u.root.querySelector(".u-wrap").appendChild(tooltip);
@@ -108,11 +130,11 @@
108130
u => {
109131
let c = u.cursor;
110132

111-
if (c.idx != dataIdx) {
133+
if (dataIdx != c.idx) {
112134
dataIdx = c.idx;
113135

114136
if (seriesIdx != null)
115-
setTooltip(u, setTooltip);
137+
setTooltip(u);
116138
}
117139
}
118140
],
@@ -122,11 +144,9 @@
122144
seriesIdx = sidx;
123145

124146
if (sidx == null)
125-
tooltip.style.display = "none";
126-
else if (dataIdx != null) {
127-
tooltip.style.display = "block";
147+
hideTooltip();
148+
else if (dataIdx != null)
128149
setTooltip(u);
129-
}
130150
}
131151
}
132152
],
@@ -138,6 +158,11 @@
138158
const interpolatedColorWithAlpha = "#fcb0f17a";
139159

140160
ctx.save();
161+
162+
let clip = new Path2D();
163+
clip.rect(left, top, width, height);
164+
ctx.clip(clip);
165+
141166
ctx.strokeStyle = interpolatedColorWithAlpha;
142167
ctx.beginPath();
143168

0 commit comments

Comments
 (0)