Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

feat(dashboard): bring back hovering a chart to read it

Juan Denisc3e0c9ebf6c7
diff --git a/frontend/src/components/dashboard/widgets/renderers.jsx b/frontend/src/components/dashboard/widgets/renderers.jsx
index 4871d46d..3812b0dc 100644
--- a/frontend/src/components/dashboard/widgets/renderers.jsx
+++ b/frontend/src/components/dashboard/widgets/renderers.jsx
@@ -105,8 +105,14 @@ function pillKind(state) {
* Gradient ids are per-instance (useId) so two charts on one board cannot
* capture each other's fills.
*/
-function WidgetChart({ series, colors, fill = true, grid = true, height = '100%', domain = null, lineStyle = 'smooth' }) {
+function WidgetChart({
+ series, colors, fill = true, grid = true, height = '100%', domain = null,
+ lineStyle = 'smooth', interactive = false, stamps = [], labels = [], units = [],
+}) {
const uid = useId().replace(/:/g, '');
+ // Index of the sample under the pointer, or null. Kept as an index rather
+ // than a pixel so it survives a resize.
+ const [hover, setHover] = useState(null);
const data = (series || []).filter((s) => Array.isArray(s) && s.length > 0);
const all = data.flat();
if (!all.length) return null;
@@ -139,7 +145,38 @@ function WidgetChart({ series, colors, fill = true, grid = true, height = '100%'
}).join(' ');
};
- return (
+ const longest = data.reduce((n, sx) => Math.max(n, sx.length), 0);
+
+ // Nearest sample to the pointer. The chart is `preserveAspectRatio="none"`,
+ // so viewBox x maps linearly onto the container width — a ratio is enough.
+ const onMove = (event) => {
+ if (!interactive || longest < 2) return;
+ const box = event.currentTarget.getBoundingClientRect();
+ if (!box.width) return;
+ const ratio = (event.clientX - box.left) / box.width;
+ const index = Math.round(Math.max(0, Math.min(1, ratio)) * (longest - 1));
+ setHover(index);
+ };
+
+ const hovered = hover === null ? null : data.map((sx, i) => {
+ // Series can differ in length; clamp rather than read past the end.
+ const idx = Math.min(hover, sx.length - 1);
+ const value = sx[idx];
+ return {
+ value,
+ color: colors[i % colors.length],
+ label: labels[i] || '',
+ unit: units[i] || '',
+ left: toX(hover, longest),
+ top: toY(value),
+ };
+ });
+ const stampAt = hover === null ? null : (stamps[Math.min(hover, stamps.length - 1)] || null);
+ const hoverPct = hover === null ? 0 : toX(hover, longest);
+ // Flip the tooltip to the left of the crosshair once it would overflow.
+ const flip = hoverPct > 60;
+
+ const chart = (
<svg
viewBox={`0 0 ${w} ${h}`}
preserveAspectRatio="none"
@@ -183,6 +220,46 @@ function WidgetChart({ series, colors, fill = true, grid = true, height = '100%'
))}
</svg>
);
+
+ if (!interactive) return chart;
+
+ return (
+ <div
+ className="skw-chart"
+ onPointerMove={onMove}
+ onPointerLeave={() => setHover(null)}
+ >
+ {chart}
+ {hovered && (
+ <>
+ <span className="skw-chart__cross" style={{ left: `${hoverPct}%` }} />
+ {hovered.map((point, i) => (
+ <span
+ key={i}
+ className="skw-chart__dot"
+ style={{ left: `${point.left}%`, top: `${point.top}%`, background: point.color }}
+ />
+ ))}
+ <div className={`skw-chart__tip${flip ? ' skw-chart__tip--flip' : ''}`} style={{ left: `${hoverPct}%` }}>
+ {stampAt && (
+ <div className="skw-chart__tip-when mono">
+ {new Date(stampAt).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
+ </div>
+ )}
+ {hovered.map((point, i) => (
+ <div className="skw-chart__tip-row" key={i}>
+ <span className="skw-chart__tip-dot" style={{ background: point.color }} />
+ <span className="skw-chart__tip-label">{point.label}</span>
+ <span className="skw-chart__tip-value mono" style={{ color: point.color }}>
+ {formatValue(point.value, point.unit)}
+ </span>
+ </div>
+ ))}
+ </div>
+ </>
+ )}
+ </div>
+ );
}
// Phrase why a metric has no series on this resource, using real reasons.
@@ -290,6 +367,10 @@ function WTimeseries({ cfg, ctx }) {
fill={cfg.fill !== false}
domain={chartDomain(withData.map((r) => r.metric.id))}
lineStyle={cfg.lineStyle || 'smooth'}
+ interactive
+ stamps={withData[0]?.stamps || []}
+ labels={withData.map((r) => r.metric.label)}
+ units={withData.map((r) => r.metric.unit)}
/>
</div>
{cfg.legend !== false && (
diff --git a/frontend/src/components/dashboard/widgets/useWidgetData.js b/frontend/src/components/dashboard/widgets/useWidgetData.js
index a528227d..75c60b38 100644
--- a/frontend/src/components/dashboard/widgets/useWidgetData.js
+++ b/frontend/src/components/dashboard/widgets/useWidgetData.js
@@ -198,6 +198,9 @@ export function useMetricSeriesSet(entries, range, tick) {
supported,
color: entry?.color || '',
series: supported ? seriesFrom(payloads.get(resource), meta.id) : [],
+ // Carried so the chart can label a hovered point with when it
+ // was actually sampled rather than its index.
+ stamps: supported ? stampsFrom(payloads.get(resource), meta.id) : [],
};
});
});
diff --git a/frontend/src/styles/components/_dashboard-widgets.scss b/frontend/src/styles/components/_dashboard-widgets.scss
index 1988f800..3c757be5 100644
--- a/frontend/src/styles/components/_dashboard-widgets.scss
+++ b/frontend/src/styles/components/_dashboard-widgets.scss
@@ -1728,3 +1728,93 @@ $skw-z-tv: 200;
letter-spacing: 0.05em;
}
}
+
+// ============================================================
+// CHART HOVER — crosshair, per-series dots, readout.
+// ============================================================
+// Overlay is HTML, not SVG: the chart is drawn with
+// `preserveAspectRatio="none"`, which stretches the viewBox to the container
+// and would turn every circle into an ellipse. Percentage-positioned elements
+// sit on the same coordinates without inheriting the distortion.
+.skw-chart {
+ position: relative;
+ flex: 1;
+ min-height: 0;
+ display: flex;
+
+ > svg { flex: 1; min-height: 0; }
+
+ &__cross {
+ position: absolute;
+ top: 0;
+ bottom: 0;
+ width: 0;
+ border-left: 1px dashed var(--border-strong);
+ pointer-events: none;
+ }
+
+ &__dot {
+ position: absolute;
+ width: 7px;
+ height: 7px;
+ margin: -3.5px 0 0 -3.5px;
+ border: 1.5px solid var(--surface);
+ border-radius: 50%;
+ pointer-events: none;
+ }
+
+ // Readout. Offset from the crosshair so the dots stay visible, and flipped
+ // to the other side once it would run past the right edge.
+ &__tip {
+ position: absolute;
+ top: 8px;
+ z-index: 2;
+ min-width: 132px;
+ margin-left: 12px;
+ padding: 8px 10px;
+ border: 1px solid var(--border);
+ border-radius: var(--radius);
+ background: var(--surface-2);
+ box-shadow: var(--shadow-md);
+ pointer-events: none;
+
+ &--flip {
+ margin-left: 0;
+ transform: translateX(calc(-100% - 12px));
+ }
+ }
+
+ &__tip-when {
+ margin-bottom: 6px;
+ color: var(--text-faint);
+ font-size: 10.5px;
+ text-align: center;
+ }
+
+ &__tip-row {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ padding: 2px 0;
+ font-size: 11.5px;
+ }
+
+ &__tip-dot {
+ width: 8px;
+ height: 8px;
+ border-radius: 50%;
+ flex-shrink: 0;
+ }
+
+ &__tip-label {
+ color: var(--text-dim);
+ white-space: nowrap;
+ }
+
+ // Values right-aligned in their own column so a stack of them reads down.
+ &__tip-value {
+ margin-left: auto;
+ font-weight: 600;
+ font-variant-numeric: tabular-nums;
+ }
+}