Improve line graph tooltip (#3601)

This commit is contained in:
yubiuser
2025-10-04 08:58:29 +02:00
committed by GitHub
2 changed files with 38 additions and 22 deletions

View File

@@ -260,6 +260,10 @@ function positionTooltip(tooltipEl, tooltip, context) {
const arrowMinIndent = 2 * tooltip.options.cornerRadius; const arrowMinIndent = 2 * tooltip.options.cornerRadius;
const arrowSize = 5; const arrowSize = 5;
// Check if this is a queryOverTimeChart or clientsChart - these should stick to x-axis
const canvasId = context.chart.canvas.id;
const isTimelineChart = canvasId === "queryOverTimeChart" || canvasId === "clientsChart";
let tooltipX = offsetX + caretX; let tooltipX = offsetX + caretX;
let arrowX; let arrowX;
@@ -316,9 +320,18 @@ function positionTooltip(tooltipEl, tooltip, context) {
arrowX = offsetX + caretX - tooltipX; arrowX = offsetX + caretX - tooltipX;
} }
let tooltipY = offsetY + caretY; let tooltipY;
// Compute Y position if (isTimelineChart) {
// For timeline charts, always position tooltip below the chart with caret pointing to x-axis
const chartArea = context.chart.chartArea;
const canvasBottom = chartArea.bottom;
tooltipY = offsetY + canvasBottom + arrowSize + caretPadding;
// Ensure the arrow points to the correct X position
arrowX = tooltip.caretX - (tooltipX - offsetX);
} else {
tooltipY = offsetY + caretY;
switch (tooltip.yAlign) { switch (tooltip.yAlign) {
case "top": { case "top": {
tooltipY += arrowSize + caretPadding; tooltipY += arrowSize + caretPadding;
@@ -338,6 +351,7 @@ function positionTooltip(tooltipEl, tooltip, context) {
} }
// No default // No default
} }
}
// Position tooltip and display // Position tooltip and display
tooltipEl.style.top = `${tooltipY.toFixed(1)}px`; tooltipEl.style.top = `${tooltipY.toFixed(1)}px`;

View File

@@ -518,8 +518,8 @@ function labelWithPercentage(tooltipLabel, skipZero = false) {
// Sum all queries for the current time by iterating over all keys in the // Sum all queries for the current time by iterating over all keys in the
// current dataset // current dataset
let sum = 0; let sum = 0;
for (const value of Object.values(tooltipLabel.parsed._stacks.y)) { for (const [key, value] of Object.entries(tooltipLabel.parsed._stacks.y)) {
if (value === undefined) continue; if (key.startsWith("_") || value === undefined) continue;
const num = Number.parseInt(value, 10); const num = Number.parseInt(value, 10);
if (num) sum += num; if (num) sum += num;
} }
@@ -636,9 +636,11 @@ $(() => {
display: false, display: false,
}, },
tooltip: { tooltip: {
enabled: true, // Disable the on-canvas tooltip
enabled: false,
intersect: false, intersect: false,
yAlign: "bottom", external: customTooltips,
yAlign: "top",
itemSort(a, b) { itemSort(a, b) {
return b.datasetIndex - a.datasetIndex; return b.datasetIndex - a.datasetIndex;
}, },
@@ -653,7 +655,7 @@ $(() => {
return "Queries from " + from + " to " + to; return "Queries from " + from + " to " + to;
}, },
label(tooltipLabel) { label(tooltipLabel) {
return labelWithPercentage(tooltipLabel); return labelWithPercentage(tooltipLabel, true);
}, },
}, },
}, },