Skip to content

Commit 0c9dbc3

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

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

demos/tooltips-closest.html

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,25 @@
6262

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

65+
let tooltipVisible = false;
66+
67+
function showTooltip() {
68+
if (!tooltipVisible) {
69+
tooltip.style.display = "block";
70+
tooltipVisible = true;
71+
}
72+
}
73+
74+
function hideTooltip() {
75+
if (tooltipVisible) {
76+
tooltip.style.display = "none";
77+
tooltipVisible = false;
78+
}
79+
}
80+
6581
function setTooltip(u) {
82+
showTooltip();
83+
6684
let top = u.valToPos(u.data[seriesIdx][dataIdx], 'y');
6785
let lft = u.valToPos(u.data[ 0][dataIdx], 'x');
6886

@@ -108,11 +126,11 @@
108126
u => {
109127
let c = u.cursor;
110128

111-
if (c.idx != dataIdx) {
129+
if (dataIdx != c.idx) {
112130
dataIdx = c.idx;
113131

114132
if (seriesIdx != null)
115-
setTooltip(u, setTooltip);
133+
setTooltip(u);
116134
}
117135
}
118136
],
@@ -122,11 +140,9 @@
122140
seriesIdx = sidx;
123141

124142
if (sidx == null)
125-
tooltip.style.display = "none";
126-
else if (dataIdx != null) {
127-
tooltip.style.display = "block";
143+
hideTooltip();
144+
else if (dataIdx != null)
128145
setTooltip(u);
129-
}
130146
}
131147
}
132148
],
@@ -138,6 +154,11 @@
138154
const interpolatedColorWithAlpha = "#fcb0f17a";
139155

140156
ctx.save();
157+
158+
let clip = new Path2D();
159+
clip.rect(left, top, width, height);
160+
ctx.clip(clip);
161+
141162
ctx.strokeStyle = interpolatedColorWithAlpha;
142163
ctx.beginPath();
143164

0 commit comments

Comments
 (0)