Files
2026-11-14 21:04:23 +01:00

514 lines
14 KiB
Vue

<script setup lang="ts">
import type { Point } from '#shared/types'
import { formatNumber, formatShortDate, formatTime } from '~/utils/format'
const props = defineProps<{
observed: Point[]
forecast: Point[]
}>()
interface ChartTick {
x: number
line1: string
line2?: string
anchor: 'start' | 'middle' | 'end'
}
interface ChartGeometry {
width: number
height: number
pad: { top: number; right: number; bottom: number; left: number }
hasData: boolean
observedPath: string
forecastPath: string
bandPath: string
singleBand: { x: number; top: number; bottom: number } | null
observedSingle: { x: number; y: number } | null
forecastSingle: { x: number; y: number } | null
nowX: number
nowAnchor: 'start' | 'middle' | 'end'
peak: Point | null
peakX: number
peakY: number
peakLabel: string
peakLabelX: number
peakLabelY: number
peakLabelWidth: number
yTicks: Array<{ y: number; label: string }>
xTicks: ChartTick[]
}
function niceMax(value: number): number {
if (!Number.isFinite(value) || value <= 0) return 1
const magnitude = 10 ** Math.floor(Math.log10(value))
const residual = value / magnitude
const steps = [1, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10]
const step = steps.find((candidate) => residual <= candidate) ?? 10
return step * magnitude
}
function sorted(points: Point[]): Point[] {
return [...points].sort((a, b) => Date.parse(a.t) - Date.parse(b.t))
}
function buildGeometry(width: number, height: number, compact: boolean): ChartGeometry {
const observed = sorted(props.observed)
const forecast = sorted(props.forecast)
const pad = compact
? { top: 26, right: 14, bottom: 42, left: 38 }
: { top: 28, right: 18, bottom: 44, left: 48 }
const plotWidth = width - pad.left - pad.right
const plotHeight = height - pad.top - pad.bottom
const times = [...observed, ...forecast]
.map((point) => Date.parse(point.t))
.filter((time) => Number.isFinite(time))
let start = times.length ? Math.min(...times) : Date.now()
let end = times.length ? Math.max(...times) : start + 3_600_000
if (end <= start) {
start -= 1_800_000
end += 1_800_000
}
const peakSource = [...observed.map((point) => point.v), ...forecast.map((point) => Math.max(point.v, point.hi ?? point.v))]
const yMax = niceMax(Math.max(...peakSource, 1) * 1.05)
const x = (iso: string) =>
pad.left + ((Date.parse(iso) - start) / (end - start)) * plotWidth
const y = (value: number) =>
pad.top + plotHeight - (Math.max(0, value) / yMax) * plotHeight
const linePath = (points: Point[]) =>
points
.map((point, index) => {
const command = index === 0 ? 'M' : 'L'
return `${command} ${x(point.t).toFixed(2)} ${y(point.v).toFixed(2)}`
})
.join(' ')
const bandPoints = forecast.map((point) => ({
x: x(point.t),
top: y(point.hi ?? point.v),
bottom: y(point.lo ?? point.v),
}))
let bandPath = ''
if (bandPoints.length >= 2) {
const top = bandPoints.map((point) => `${point.x.toFixed(2)} ${point.top.toFixed(2)}`)
const bottom = [...bandPoints]
.reverse()
.map((point) => `${point.x.toFixed(2)} ${point.bottom.toFixed(2)}`)
bandPath = `M ${top[0]} L ${top.slice(1).join(' L ')} L ${bottom.join(' L ')} Z`
}
let peak: Point | null = null
for (const point of forecast) {
if (!peak || point.v > peak.v) peak = point
}
const peakX = peak ? x(peak.t) : pad.left
const peakY = peak ? y(peak.v) : pad.top
const peakLabel = peak ? `Peak ${formatTime(peak.t)}` : ''
const peakLabelWidth = peak ? peakLabel.length * 5.6 + 14 : 0
const peakLabelX = peak
? Math.min(
Math.max(peakX - peakLabelWidth / 2, pad.left),
width - pad.right - peakLabelWidth,
)
: 0
const peakLabelY = peak ? Math.max(pad.top + 2, peakY - 25) : 0
const nowX = forecast.length
? x(forecast[0]!.t)
: observed.length
? x(observed.at(-1)!.t)
: pad.left + plotWidth
const nowAnchor: 'start' | 'middle' | 'end' =
nowX > width - pad.right - 24 ? 'end' : nowX < pad.left + 24 ? 'start' : 'middle'
const yTickCount = compact ? 3 : 4
const yTicks = Array.from({ length: yTickCount }, (_, index) => {
const value = (yMax / (yTickCount - 1)) * index
const labelValue = yMax <= 5 ? Math.round(value * 10) / 10 : Math.round(value)
return { y: y(value), label: formatNumber(labelValue) }
})
const spanHours = (end - start) / 3_600_000
const xTickCount = compact ? 4 : 6
const xTicks = Array.from({ length: xTickCount }, (_, index) => {
const fraction = index / (xTickCount - 1)
const time = start + (end - start) * fraction
const iso = new Date(time).toISOString()
const anchor: ChartTick['anchor'] =
index === 0 ? 'start' : index === xTickCount - 1 ? 'end' : 'middle'
return {
x: pad.left + plotWidth * fraction,
line1: spanHours <= 26 ? formatTime(iso) : formatShortDate(iso),
line2: spanHours <= 26 ? undefined : formatTime(iso),
anchor,
}
})
return {
width,
height,
pad,
hasData: observed.length > 0 || forecast.length > 0,
observedPath: linePath(observed),
forecastPath: linePath(forecast),
bandPath,
singleBand:
bandPoints.length === 1
? {
x: bandPoints[0]!.x,
top: bandPoints[0]!.top,
bottom: bandPoints[0]!.bottom,
}
: null,
observedSingle:
observed.length === 1
? { x: x(observed[0]!.t), y: y(observed[0]!.v) }
: null,
forecastSingle:
forecast.length === 1
? { x: x(forecast[0]!.t), y: y(forecast[0]!.v) }
: null,
nowX,
nowAnchor,
peak,
peakX,
peakY,
peakLabel,
peakLabelX,
peakLabelY,
peakLabelWidth,
yTicks,
xTicks,
}
}
const mobile = computed(() => buildGeometry(360, 236, true))
const desktop = computed(() => buildGeometry(760, 286, false))
</script>
<template>
<div class="w-full min-w-0">
<svg
class="w-full h-auto sm:hidden"
:viewBox="`0 0 ${mobile.width} ${mobile.height}`"
role="img"
aria-label="Observed history and forecast estimated devices per hour with an 80 percent band"
>
<g v-if="mobile.hasData">
<text x="8" y="13" fill="#94A3B8" font-size="9" font-family="Inter, sans-serif">
devices/hour
</text>
<g>
<line
v-for="tick in mobile.yTicks"
:key="`my-${tick.y}`"
:x1="mobile.pad.left"
:x2="mobile.width - mobile.pad.right"
:y1="tick.y"
:y2="tick.y"
stroke="#1e3a52"
stroke-dasharray="3 3"
/>
<text
v-for="tick in mobile.yTicks"
:key="`myl-${tick.y}`"
:x="mobile.pad.left - 7"
:y="tick.y + 3"
fill="#94A3B8"
font-size="9"
font-family="Inter, sans-serif"
text-anchor="end"
>
{{ tick.label }}
</text>
</g>
<path v-if="mobile.bandPath" :d="mobile.bandPath" fill="#00C3CD" fill-opacity="0.16" />
<rect
v-if="mobile.singleBand"
:x="mobile.singleBand.x - 3"
:y="mobile.singleBand.top"
width="6"
:height="Math.max(2, mobile.singleBand.bottom - mobile.singleBand.top)"
rx="2"
fill="#00C3CD"
fill-opacity="0.25"
/>
<path
v-if="mobile.observedPath"
:d="mobile.observedPath"
fill="none"
stroke="#FFFFFF"
stroke-width="2"
stroke-linejoin="round"
stroke-linecap="round"
/>
<circle
v-if="mobile.observedSingle"
:cx="mobile.observedSingle.x"
:cy="mobile.observedSingle.y"
r="3"
fill="#FFFFFF"
/>
<path
v-if="mobile.forecastPath"
:d="mobile.forecastPath"
fill="none"
stroke="#00C3CD"
stroke-width="2.4"
stroke-dasharray="6 4"
stroke-linejoin="round"
stroke-linecap="round"
/>
<circle
v-if="mobile.forecastSingle"
:cx="mobile.forecastSingle.x"
:cy="mobile.forecastSingle.y"
r="4"
fill="#00C3CD"
/>
<line
:x1="mobile.nowX"
:x2="mobile.nowX"
:y1="mobile.pad.top"
:y2="mobile.height - mobile.pad.bottom"
stroke="#94A3B8"
stroke-dasharray="3 4"
stroke-opacity="0.8"
/>
<text
:x="mobile.nowX"
:y="mobile.height - 4"
fill="#00C3CD"
font-size="9"
font-family="Inter, sans-serif"
:text-anchor="mobile.nowAnchor"
>
Now
</text>
<g v-if="mobile.peak">
<rect
:x="mobile.peakLabelX"
:y="mobile.peakLabelY - 11"
:width="mobile.peakLabelWidth"
height="16"
rx="4"
fill="#132b40"
stroke="#1e3a52"
/>
<text
:x="mobile.peakLabelX + mobile.peakLabelWidth / 2"
:y="mobile.peakLabelY"
fill="#00C3CD"
font-size="9"
font-family="Inter, sans-serif"
font-weight="600"
text-anchor="middle"
>
{{ mobile.peakLabel }}
</text>
<circle
:cx="mobile.peakX"
:cy="mobile.peakY"
r="4"
fill="#FFFFFF"
stroke="#00C3CD"
stroke-width="2"
/>
</g>
<g>
<text
v-for="tick in mobile.xTicks"
:key="`mx-${tick.x}`"
:x="tick.x"
:y="mobile.height - mobile.pad.bottom + (tick.line2 ? 15 : 20)"
fill="#94A3B8"
font-size="9"
font-family="Inter, sans-serif"
:text-anchor="tick.anchor"
>
{{ tick.line1 }}
<tspan
v-if="tick.line2"
:x="tick.x"
:dy="11"
fill="#64748B"
>
{{ tick.line2 }}
</tspan>
</text>
</g>
</g>
</svg>
<svg
class="hidden sm:block w-full h-auto"
:viewBox="`0 0 ${desktop.width} ${desktop.height}`"
role="img"
aria-label="Observed history and forecast estimated devices per hour with an 80 percent band"
>
<g v-if="desktop.hasData">
<text x="10" y="13" fill="#94A3B8" font-size="9" font-family="Inter, sans-serif">
devices/hour
</text>
<g>
<line
v-for="tick in desktop.yTicks"
:key="`dy-${tick.y}`"
:x1="desktop.pad.left"
:x2="desktop.width - desktop.pad.right"
:y1="tick.y"
:y2="tick.y"
stroke="#1e3a52"
stroke-dasharray="3 3"
/>
<text
v-for="tick in desktop.yTicks"
:key="`dyl-${tick.y}`"
:x="desktop.pad.left - 8"
:y="tick.y + 3"
fill="#94A3B8"
font-size="10"
font-family="Inter, sans-serif"
text-anchor="end"
>
{{ tick.label }}
</text>
</g>
<path v-if="desktop.bandPath" :d="desktop.bandPath" fill="#00C3CD" fill-opacity="0.16" />
<rect
v-if="desktop.singleBand"
:x="desktop.singleBand.x - 4"
:y="desktop.singleBand.top"
width="8"
:height="Math.max(2, desktop.singleBand.bottom - desktop.singleBand.top)"
rx="2"
fill="#00C3CD"
fill-opacity="0.25"
/>
<path
v-if="desktop.observedPath"
:d="desktop.observedPath"
fill="none"
stroke="#FFFFFF"
stroke-width="2.2"
stroke-linejoin="round"
stroke-linecap="round"
/>
<circle
v-if="desktop.observedSingle"
:cx="desktop.observedSingle.x"
:cy="desktop.observedSingle.y"
r="3"
fill="#FFFFFF"
/>
<path
v-if="desktop.forecastPath"
:d="desktop.forecastPath"
fill="none"
stroke="#00C3CD"
stroke-width="2.4"
stroke-dasharray="6 4"
stroke-linejoin="round"
stroke-linecap="round"
/>
<circle
v-if="desktop.forecastSingle"
:cx="desktop.forecastSingle.x"
:cy="desktop.forecastSingle.y"
r="4"
fill="#00C3CD"
/>
<line
:x1="desktop.nowX"
:x2="desktop.nowX"
:y1="desktop.pad.top"
:y2="desktop.height - desktop.pad.bottom"
stroke="#94A3B8"
stroke-dasharray="3 4"
stroke-opacity="0.8"
/>
<text
:x="desktop.nowX"
:y="desktop.height - 4"
fill="#00C3CD"
font-size="9"
font-family="Inter, sans-serif"
:text-anchor="desktop.nowAnchor"
>
Now
</text>
<g v-if="desktop.peak">
<rect
:x="desktop.peakLabelX"
:y="desktop.peakLabelY - 11"
:width="desktop.peakLabelWidth"
height="16"
rx="4"
fill="#132b40"
stroke="#1e3a52"
/>
<text
:x="desktop.peakLabelX + desktop.peakLabelWidth / 2"
:y="desktop.peakLabelY"
fill="#00C3CD"
font-size="9"
font-family="Inter, sans-serif"
font-weight="600"
text-anchor="middle"
>
{{ desktop.peakLabel }}
</text>
<circle
:cx="desktop.peakX"
:cy="desktop.peakY"
r="4.5"
fill="#FFFFFF"
stroke="#00C3CD"
stroke-width="2"
/>
</g>
<g>
<text
v-for="tick in desktop.xTicks"
:key="`dx-${tick.x}`"
:x="tick.x"
:y="desktop.height - desktop.pad.bottom + (tick.line2 ? 15 : 20)"
fill="#94A3B8"
font-size="10"
font-family="Inter, sans-serif"
:text-anchor="tick.anchor"
>
{{ tick.line1 }}
<tspan
v-if="tick.line2"
:x="tick.x"
:dy="12"
fill="#64748B"
>
{{ tick.line2 }}
</tspan>
</text>
</g>
</g>
</svg>
</div>
</template>