import { useMemo } from 'react'
import {
Bar,
BarChart,
CartesianGrid,
Line,
LineChart,
ResponsiveContainer,
Tooltip,
XAxis,
YAxis,
} from 'recharts'
import { useThemeColors } from '../lib/colors'
import { dateTimeLabel, dayLabel, timeLabel } from '../lib/format'
import { useT } from '../lib/i18n'
export function Legend({ series }) {
if (series.length < 2) return null
return (
{series.map((s) => (
-
{s.name}
))}
)
}
function TooltipBox({ title, rows, total }) {
const { t } = useT()
return (
{title}
{rows.map((r) => (
|
{r.name}
|
{r.value} |
))}
{total && (
| {t('chart.total')} |
{total} |
)}
)
}
const axisTick = (c) => ({ fill: c.muted, fontSize: 11 })
// Silences up to this long are drawn through; longer ones (night, outage)
// break the line. Covers a unit reporting every ~5 min plus a missed cycle.
const BRIDGE_SECONDS = 900
const SMOOTHING_RADIUS = { off: 0, light: 1, strong: 3 }
/**
* Chart rows from API points: a gap row where the whole fleet went silent,
* values interpolated where one series reports less often than the bucket
* (INV5 every ~5 min on a 1-min chart would otherwise have no line at all),
* then an optional centred moving average. Interpolated values are flagged
* `__est` so the tooltip can mark them.
*/
function prepare(points, keys, bucket, smoothing) {
const maxGap = Math.max(BRIDGE_SECONDS, (bucket || 0) * 2.5)
const rows = []
points.forEach((p, i) => {
if (i && p.t - points[i - 1].t > maxGap) rows.push({ t: points[i - 1].t + (bucket || 1) })
rows.push({ ...p })
})
for (const key of keys) {
let prev = -1
rows.forEach((r, i) => {
if (r[key] == null) return
if (prev >= 0 && i - prev > 1 && r.t - rows[prev].t <= maxGap) {
const a = rows[prev]
for (let j = prev + 1; j < i; j++) {
rows[j][key] = a[key] + ((r[key] - a[key]) * (rows[j].t - a.t)) / (r.t - a.t)
rows[j][`${key}__est`] = true
}
}
prev = i
})
}
const radius = SMOOTHING_RADIUS[smoothing] ?? 0
if (radius) {
for (const key of keys) {
const raw = rows.map((r) => r[key])
rows.forEach((r, i) => {
if (raw[i] == null) return
// Only neighbours on the same unbroken stretch of line
let sum = raw[i]
let n = 1
for (let j = i - 1; j >= i - radius && j >= 0 && raw[j] != null; j--) {
sum += raw[j]
n++
}
for (let j = i + 1; j <= i + radius && j < raw.length && raw[j] != null; j++) {
sum += raw[j]
n++
}
r[key] = sum / n
})
}
}
return rows
}
/**
* Lines over time. points: [{t: unix seconds, [series.key]: number}]
* series: [{key, name, color}]. Gaps (night, outages) stay gaps.
*/
export function TimeLines({
points,
series,
from,
to,
bucket,
format,
axisFormat,
height = 240,
showTotal = false,
smoothing = 'off',
}) {
const c = useThemeColors()
const span = to - from
const keys = series.map((s) => s.key).join(',')
const data = useMemo(() => prepare(points, keys.split(','), bucket, smoothing), [points, keys, bucket, smoothing])
return (
timeLabel(t, span)}
tick={axisTick(c)}
stroke={c.axis}
tickLine={false}
minTickGap={28}
/>
{
if (!active || !payload?.length) return null
const row = payload[0].payload
const rows = series
.filter((s) => row[s.key] != null)
.map((s) => ({ ...s, value: (row[`${s.key}__est`] ? '≈ ' : '') + format(row[s.key]) }))
const total = showTotal
? format(series.reduce((sum, s) => sum + (payload[0].payload[s.key] ?? 0), 0))
: null
return
}}
/>
{series.map((s) => (
))}
)
}
/**
* Bars per day. days: [{day: 'YYYY-MM-DD', [series.key]: number}].
* Several series are stacked (the stack height is the day's total).
*/
export function DayBars({ days, series, format, axisFormat, height = 220 }) {
const c = useThemeColors()
const last = series.length - 1
return (
dayLabel(d)}
tick={axisTick(c)}
stroke={c.axis}
tickLine={false}
minTickGap={24}
/>
{
if (!active || !payload?.length) return null
const row = payload[0].payload
const rows = series.map((s) => ({ ...s, value: format(row[s.key]) }))
const total =
series.length > 1 ? format(series.reduce((sum, s) => sum + (row[s.key] ?? 0), 0)) : null
return
}}
/>
{series.map((s, i) => (
1 ? c.surface : undefined}
strokeWidth={series.length > 1 ? 1 : 0}
radius={i === last ? [3, 3, 0, 0] : 0}
isAnimationActive={false}
/>
))}
)
}
export function EnergyFootnote({ estimated }) {
const { t } = useT()
return (
{estimated && t('energy.footEstimated')}
{t('energy.axis')}
)
}