Skip to content

Commit 0543e32

Browse files
Merge pull request #760 from leeoniya/master
fix tooltips at chart edges. dont spill over interpolated markers.
2 parents f5e8d21 + bae7b68 commit 0543e32

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

site/static/index.html

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,29 @@
9595

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

98+
let over;
99+
100+
let tooltipVisible = false;
101+
102+
function showTooltip() {
103+
if (!tooltipVisible) {
104+
tooltip.style.display = "block";
105+
over.style.cursor = "pointer";
106+
tooltipVisible = true;
107+
}
108+
}
109+
110+
function hideTooltip() {
111+
if (tooltipVisible) {
112+
tooltip.style.display = "none";
113+
over.style.cursor = null;
114+
tooltipVisible = false;
115+
}
116+
}
117+
98118
function setTooltip(u) {
119+
showTooltip();
120+
99121
let top = u.valToPos(u.data[seriesIdx][dataIdx], 'y');
100122
let lft = u.valToPos(u.data[ 0][dataIdx], 'x');
101123

@@ -109,7 +131,7 @@
109131
let trailer = "";
110132
if (absoluteMode) {
111133
let pctSinceStart = (((u.data[seriesIdx][dataIdx] - u.data[seriesIdx][0]) / u.data[seriesIdx][0]) * 100).toFixed(2);
112-
trailer = uPlot.fmtNum(u.data[seriesIdx][dataIdx]) + "(" +
134+
trailer = uPlot.fmtNum(u.data[seriesIdx][dataIdx]) + " (" +
113135
pctSinceStart + "% since start)";
114136
} else {
115137
trailer = uPlot.fmtNum(u.data[seriesIdx][dataIdx]) + "% since start";
@@ -124,7 +146,8 @@
124146
hooks: {
125147
ready: [
126148
u => {
127-
let over = u.root.querySelector(".u-over");
149+
over = u.root.querySelector(".u-over");
150+
128151
tooltipLeftOffset = parseFloat(over.style.left);
129152
tooltipTopOffset = parseFloat(over.style.top);
130153
u.root.querySelector(".u-wrap").appendChild(tooltip);
@@ -151,11 +174,11 @@
151174
u => {
152175
let c = u.cursor;
153176

154-
if (c.idx != dataIdx) {
177+
if (dataIdx != c.idx) {
155178
dataIdx = c.idx;
156179

157180
if (seriesIdx != null)
158-
setTooltip(u, setTooltip);
181+
setTooltip(u);
159182
}
160183
}
161184
],
@@ -165,11 +188,9 @@
165188
seriesIdx = sidx;
166189

167190
if (sidx == null)
168-
tooltip.style.display = "none";
169-
else if (dataIdx != null) {
170-
tooltip.style.display = "block";
191+
hideTooltip();
192+
else if (dataIdx != null)
171193
setTooltip(u);
172-
}
173194
}
174195
}
175196
],
@@ -236,21 +257,23 @@
236257

237258
const interpolatedColorWithAlpha = "#fcb0f15f";
238259

239-
ctx.save();
240260
ctx.strokeStyle = interpolatedColorWithAlpha;
241261
ctx.beginPath();
242262

243-
u.data[0].forEach((v, j) => {
263+
let [ i0, i1 ] = u.series[0].idxs;
264+
265+
for (let j = i0; j <= i1; j++) {
266+
let v = u.data[0][j];
267+
244268
if (isInterpolated(j)) {
245269
let cx = Math.round(u.valToPos(v, 'x', true));
246270
ctx.moveTo(cx, top);
247271
ctx.lineTo(cx, top + height);
248272
}
249-
});
273+
}
250274

251275
ctx.closePath();
252276
ctx.stroke();
253-
ctx.restore();
254277
},
255278
]
256279
},

0 commit comments

Comments
 (0)