|
95 | 95 |
|
96 | 96 | const fmtDate = uPlot.fmtDate("{M}/{D}/{YY} {h}:{mm}:{ss} {AA}");
|
97 | 97 |
|
| 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 | + |
98 | 118 | function setTooltip(u) {
|
| 119 | + showTooltip(); |
| 120 | + |
99 | 121 | let top = u.valToPos(u.data[seriesIdx][dataIdx], 'y');
|
100 | 122 | let lft = u.valToPos(u.data[ 0][dataIdx], 'x');
|
101 | 123 |
|
|
109 | 131 | let trailer = "";
|
110 | 132 | if (absoluteMode) {
|
111 | 133 | 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]) + " (" + |
113 | 135 | pctSinceStart + "% since start)";
|
114 | 136 | } else {
|
115 | 137 | trailer = uPlot.fmtNum(u.data[seriesIdx][dataIdx]) + "% since start";
|
|
124 | 146 | hooks: {
|
125 | 147 | ready: [
|
126 | 148 | u => {
|
127 |
| - let over = u.root.querySelector(".u-over"); |
| 149 | + over = u.root.querySelector(".u-over"); |
| 150 | + |
128 | 151 | tooltipLeftOffset = parseFloat(over.style.left);
|
129 | 152 | tooltipTopOffset = parseFloat(over.style.top);
|
130 | 153 | u.root.querySelector(".u-wrap").appendChild(tooltip);
|
|
151 | 174 | u => {
|
152 | 175 | let c = u.cursor;
|
153 | 176 |
|
154 |
| - if (c.idx != dataIdx) { |
| 177 | + if (dataIdx != c.idx) { |
155 | 178 | dataIdx = c.idx;
|
156 | 179 |
|
157 | 180 | if (seriesIdx != null)
|
158 |
| - setTooltip(u, setTooltip); |
| 181 | + setTooltip(u); |
159 | 182 | }
|
160 | 183 | }
|
161 | 184 | ],
|
|
165 | 188 | seriesIdx = sidx;
|
166 | 189 |
|
167 | 190 | 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) |
171 | 193 | setTooltip(u);
|
172 |
| - } |
173 | 194 | }
|
174 | 195 | }
|
175 | 196 | ],
|
|
236 | 257 |
|
237 | 258 | const interpolatedColorWithAlpha = "#fcb0f15f";
|
238 | 259 |
|
239 |
| - ctx.save(); |
240 | 260 | ctx.strokeStyle = interpolatedColorWithAlpha;
|
241 | 261 | ctx.beginPath();
|
242 | 262 |
|
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 | + |
244 | 268 | if (isInterpolated(j)) {
|
245 | 269 | let cx = Math.round(u.valToPos(v, 'x', true));
|
246 | 270 | ctx.moveTo(cx, top);
|
247 | 271 | ctx.lineTo(cx, top + height);
|
248 | 272 | }
|
249 |
| - }); |
| 273 | + } |
250 | 274 |
|
251 | 275 | ctx.closePath();
|
252 | 276 | ctx.stroke();
|
253 |
| - ctx.restore(); |
254 | 277 | },
|
255 | 278 | ]
|
256 | 279 | },
|
|
0 commit comments