|
23 | 23 | border-color: #2563eb; |
24 | 24 | color: #ffffff; |
25 | 25 | } |
| 26 | +
|
| 27 | + .stats-tooltip { |
| 28 | + pointer-events: none; |
| 29 | + transform: translate(-50%, calc(-100% - 12px)); |
| 30 | + } |
26 | 31 | </style> |
27 | 32 | </head> |
28 | 33 |
|
|
187 | 192 | `).join(""); |
188 | 193 | } |
189 | 194 |
|
| 195 | + function getChartX(labels, margins, plotWidth, index) { |
| 196 | + return labels.length === 1 |
| 197 | + ? margins.left + plotWidth / 2 |
| 198 | + : margins.left + (plotWidth * index / (labels.length - 1)); |
| 199 | + } |
| 200 | +
|
| 201 | + function getOrCreateTooltip(canvas) { |
| 202 | + const parent = canvas.parentElement; |
| 203 | + let tooltip = parent.querySelector(".stats-tooltip"); |
| 204 | + if (!tooltip) { |
| 205 | + tooltip = document.createElement("div"); |
| 206 | + tooltip.className = "stats-tooltip hidden absolute z-20 max-w-xs rounded-lg bg-gray-900 px-3 py-2 text-xs text-white shadow-lg"; |
| 207 | + parent.appendChild(tooltip); |
| 208 | + } |
| 209 | + return tooltip; |
| 210 | + } |
| 211 | +
|
| 212 | + function bindChartTooltip(canvas, labels, datasets, revenueDataset, margins, plotWidth, plotHeight) { |
| 213 | + const tooltip = getOrCreateTooltip(canvas); |
| 214 | + const allDatasets = revenueDataset ? [...datasets, revenueDataset] : datasets; |
| 215 | + const formatValue = (dataset, value) => dataset === revenueDataset |
| 216 | + ? moneyFormatter.format(value) |
| 217 | + : numberFormatter.format(value); |
| 218 | +
|
| 219 | + canvas.onmousemove = (event) => { |
| 220 | + const rect = canvas.getBoundingClientRect(); |
| 221 | + const x = event.clientX - rect.left; |
| 222 | + const y = event.clientY - rect.top; |
| 223 | +
|
| 224 | + if ( |
| 225 | + x < margins.left || |
| 226 | + x > margins.left + plotWidth || |
| 227 | + y < margins.top || |
| 228 | + y > margins.top + plotHeight || |
| 229 | + labels.length === 0 |
| 230 | + ) { |
| 231 | + tooltip.classList.add("hidden"); |
| 232 | + return; |
| 233 | + } |
| 234 | +
|
| 235 | + const rawIndex = labels.length === 1 |
| 236 | + ? 0 |
| 237 | + : Math.round(((x - margins.left) / plotWidth) * (labels.length - 1)); |
| 238 | + const index = Math.min(Math.max(rawIndex, 0), labels.length - 1); |
| 239 | + const tooltipX = getChartX(labels, margins, plotWidth, index); |
| 240 | +
|
| 241 | + tooltip.innerHTML = ` |
| 242 | + <div class="mb-1 font-semibold text-gray-100">${labels[index]}</div> |
| 243 | + <div class="space-y-1"> |
| 244 | + ${allDatasets.map((dataset) => ` |
| 245 | + <div class="flex min-w-0 items-center justify-between gap-4"> |
| 246 | + <span class="flex min-w-0 items-center gap-2"> |
| 247 | + <span class="inline-block h-2.5 w-2.5 flex-shrink-0 rounded-full" style="background:${dataset.color}"></span> |
| 248 | + <span class="truncate">${dataset.name}</span> |
| 249 | + </span> |
| 250 | + <span class="font-mono">${formatValue(dataset, dataset.values[index] || 0)}</span> |
| 251 | + </div> |
| 252 | + `).join("")} |
| 253 | + </div> |
| 254 | + `; |
| 255 | + tooltip.style.left = `${tooltipX}px`; |
| 256 | + tooltip.style.top = `${margins.top + 8}px`; |
| 257 | + tooltip.classList.remove("hidden"); |
| 258 | + }; |
| 259 | +
|
| 260 | + canvas.onmouseleave = () => { |
| 261 | + tooltip.classList.add("hidden"); |
| 262 | + }; |
| 263 | + } |
| 264 | +
|
190 | 265 | function drawLineChart(canvasId, legendId, labels, datasets, options = {}) { |
191 | 266 | const canvas = document.getElementById(canvasId); |
192 | 267 | const ctx = canvas.getContext("2d"); |
|
250 | 325 | ctx.textBaseline = "top"; |
251 | 326 | labels.forEach((label, index) => { |
252 | 327 | if (index % labelStep !== 0 && index !== labels.length - 1) return; |
253 | | - const x = labels.length === 1 ? margins.left + plotWidth / 2 : margins.left + (plotWidth * index / (labels.length - 1)); |
| 328 | + const x = getChartX(labels, margins, plotWidth, index); |
254 | 329 | ctx.fillText(label, x, margins.top + plotHeight + 14); |
255 | 330 | }); |
256 | 331 |
|
|
260 | 335 | ctx.setLineDash(dashed ? [6, 5] : []); |
261 | 336 | ctx.beginPath(); |
262 | 337 | dataset.values.forEach((value, index) => { |
263 | | - const x = labels.length === 1 ? margins.left + plotWidth / 2 : margins.left + (plotWidth * index / (labels.length - 1)); |
| 338 | + const x = getChartX(labels, margins, plotWidth, index); |
264 | 339 | const y = margins.top + plotHeight - (plotHeight * value / Math.max(maxValue, 1)); |
265 | 340 | if (index === 0) ctx.moveTo(x, y); |
266 | 341 | else ctx.lineTo(x, y); |
|
273 | 348 | if (revenueDataset) drawDataset(revenueDataset, rightMax, true); |
274 | 349 |
|
275 | 350 | drawLegend(legendId, datasets, revenueDataset); |
| 351 | + bindChartTooltip(canvas, labels, datasets, revenueDataset, margins, plotWidth, plotHeight); |
276 | 352 | } |
277 | 353 |
|
278 | 354 | function seriesFromRows(rows, labels, keyField, nameField, valueField, knownItems = []) { |
|
0 commit comments