Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix(charts): show isolated points and stop endless spinner on empty values (#2514)

Sven van Ginkelca40b5b38ffa
diff --git a/internal/site/src/components/charts/area-chart.tsx b/internal/site/src/components/charts/area-chart.tsx
index 3daf10928..fd19217a3 100644
--- a/internal/site/src/components/charts/area-chart.tsx
+++ b/internal/site/src/components/charts/area-chart.tsx
@@ -11,7 +11,8 @@ import {
} from "@/components/ui/chart"
import { chartMargin, cn, formatShortDate } from "@/lib/utils"
import type { ChartData, SystemStatsRecord } from "@/types"
-import { useYAxisWidth } from "./hooks"
+import { hasChartValues, useYAxisWidth } from "./hooks"
+import { ChartNoValues } from "./chart-no-values"
import type { AxisDomain } from "recharts/types/util/types"
import { useIntersectionObserver } from "@/lib/use-intersection-observer"
@@ -118,6 +119,9 @@ export default function AreaChartDefault({
if (displayData.length === 0) {
return null
}
+ if (!hasChartValues(displayData, dataPoints)) {
+ return <ChartNoValues ref={ref} />
+ }
// if (logRender) {
// console.log("Rendered", dataPoints?.map((d) => d.label).join(", "), new Date())
// }
diff --git a/internal/site/src/components/charts/chart-no-values.tsx b/internal/site/src/components/charts/chart-no-values.tsx
new file mode 100644
index 000000000..f9f683abe
--- /dev/null
+++ b/internal/site/src/components/charts/chart-no-values.tsx
@@ -0,0 +1,16 @@
+import { t } from "@lingui/core/macro"
+import type { Ref } from "react"
+import Spinner from "@/components/spinner"
+
+/**
+ * Shown in place of a chart whose records contain no values (e.g. every probe failed).
+ * The opacity-100 class hides ChartCard's loading spinner, same as a rendered chart.
+ * Takes the chart's intersection observer ref so the chart keeps updating once values arrive.
+ */
+export function ChartNoValues({ ref }: { ref?: Ref<HTMLDivElement> }) {
+ return (
+ <div ref={ref} className="absolute inset-0">
+ <Spinner msg={t`Waiting for enough records to display`} className="bg-card opacity-100" />
+ </div>
+ )
+}
diff --git a/internal/site/src/components/charts/hooks.ts b/internal/site/src/components/charts/hooks.ts
index 95c962952..0a99eaa23 100644
--- a/internal/site/src/components/charts/hooks.ts
+++ b/internal/site/src/components/charts/hooks.ts
@@ -85,6 +85,14 @@ export function useContainerChartConfigs(containerData: ChartData["containerData
}, [containerData])
}
+/**
+ * Whether any series has a numeric value in the data. Without one, recharts renders no y-axis
+ * ticks, so the y-axis width is never measured and the chart would stay hidden.
+ */
+export function hasChartValues<T>(data: T[], dataPoints?: { dataKey: (data: T) => unknown }[]) {
+ return !!dataPoints?.some(({ dataKey }) => data.some((record) => typeof dataKey(record) === "number"))
+}
+
/** Sets the correct width of the y axis in recharts based on the longest label */
export function useYAxisWidth() {
const [yAxisWidth, setYAxisWidth] = useState(0)
diff --git a/internal/site/src/components/charts/line-chart.tsx b/internal/site/src/components/charts/line-chart.tsx
index 94c0a200c..15faffb36 100644
--- a/internal/site/src/components/charts/line-chart.tsx
+++ b/internal/site/src/components/charts/line-chart.tsx
@@ -11,7 +11,8 @@ import {
} from "@/components/ui/chart"
import { chartMargin, cn, formatShortDate } from "@/lib/utils"
import type { ChartOptions, SystemStatsRecord } from "@/types"
-import { useYAxisWidth } from "./hooks"
+import { hasChartValues, useYAxisWidth } from "./hooks"
+import { ChartNoValues } from "./chart-no-values"
import type { AxisDomain } from "recharts/types/util/types"
import { useIntersectionObserver } from "@/lib/use-intersection-observer"
@@ -141,7 +142,7 @@ export default function LineChartDefault({
dataKey={dataPoint.dataKey}
name={dataPoint.label}
type="monotoneX"
- dot={dataPoint.dot || false}
+ dot={dataPoint.dot ?? (connectNulls ? false : isolatedDot)}
strokeWidth={1.5}
stroke={color}
strokeOpacity={dataPoint.strokeOpacity}
@@ -160,6 +161,9 @@ export default function LineChartDefault({
if (displayData.length === 0) {
return null
}
+ if (!hasChartValues(displayData, dataPoints)) {
+ return <ChartNoValues ref={ref} />
+ }
// if (logRender) {
// console.log("Rendered", dataPoints?.map((d) => d.label).join(", "), new Date())
// }
diff --git a/internal/site/src/components/routes/system/charts/monitors-charts.tsx b/internal/site/src/components/routes/system/charts/monitors-charts.tsx
index 58b83df5c..2776aec9f 100644
--- a/internal/site/src/components/routes/system/charts/monitors-charts.tsx
+++ b/internal/site/src/components/routes/system/charts/monitors-charts.tsx
@@ -1,5 +1,5 @@
import { getMonitorTarget, monitorGapRecord } from "@/lib/network-monitor-utils"
-import LineChartDefault, { isolatedDot } from "@/components/charts/line-chart"
+import LineChartDefault from "@/components/charts/line-chart"
import type { DataPoint } from "@/components/charts/line-chart"
import { decimalString, formatMicroseconds, matchesFilterGroups, parseFilterGroups, toFixedFloat } from "@/lib/utils"
import { $monitorFilter } from "@/lib/stores"
@@ -61,7 +61,8 @@ function MonitorChart({
const points: DataPoint<NetworkMonitorStatsRecord>[] = []
const visibleIDs: string[] = []
const filterGroups = parseFilterGroups(filter)
- const dot = chartData.chartTime === "1m"
+ // show every point at 1m; otherwise the chart default draws only isolated points
+ const dot = chartData.chartTime === "1m" || undefined
for (let i = 0; i < count; i++) {
const p = sortedMonitors[i]
const label = getLabel(p)
@@ -138,7 +139,6 @@ export function AvgMinMaxResponseChart({ monitorStats, monitor, chartData, empty
record.stats?.[monitor?.id ?? ""]?.[metric] ?? null
const avgPoint = {
label: "Avg",
- dot: isolatedDot,
dataKey: dataFn("res_avg"),
color: 1,
order: 0,
@@ -150,7 +150,6 @@ export function AvgMinMaxResponseChart({ monitorStats, monitor, chartData, empty
return [
{
label: "Max",
- dot: isolatedDot,
dataKey: dataFn("res_max"),
color: 3,
order: 0,
@@ -158,7 +157,6 @@ export function AvgMinMaxResponseChart({ monitorStats, monitor, chartData, empty
avgPoint,
{
label: "Min",
- dot: isolatedDot,
dataKey: dataFn("res_min"),
color: 2,
order: 2,