miaopanel
1<script setup lang="ts">
2import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
3
4interface Point {
5 ts: number
6 value: number
7}
8
9const props = withDefaults(
10 defineProps<{
11 points: Point[]
12 height?: number
13 unit?: string
14 decimals?: number
15 color?: string
16 }>(),
17 { height: 150, unit: '', decimals: 0, color: '#34d399' },
18)
19
20const box = ref<HTMLDivElement | null>(null)
21const width = ref(640)
22let observer: ResizeObserver | null = null
23
24onMounted(() => {
25 if (!box.value) return
26 observer = new ResizeObserver((entries) => {
27 const w = entries[0]?.contentRect.width ?? 640
28 width.value = Math.max(260, w)
29 })
30 observer.observe(box.value)
31})
32
33onBeforeUnmount(() => observer?.disconnect())
34
35const pad = { l: 46, r: 10, t: 12, b: 22 }
36
37const maxValue = computed(() => {
38 const max = Math.max(0, ...props.points.map((p) => p.value))
39 if (max <= 0) return 1
40 return max * 1.15
41})
42
43const coords = computed(() => {
44 const n = props.points.length
45 const innerW = width.value - pad.l - pad.r
46 const innerH = props.height - pad.t - pad.b
47 return props.points.map((p, i) => ({
48 x: pad.l + (n <= 1 ? 0 : (i / (n - 1)) * innerW),
49 y: pad.t + innerH - (p.value / maxValue.value) * innerH,
50 }))
51})
52
53const linePath = computed(() =>
54 coords.value.map((c, i) => `${i === 0 ? 'M' : 'L'}${c.x.toFixed(1)},${c.y.toFixed(1)}`).join(' '),
55)
56
57const areaPath = computed(() => {
58 if (coords.value.length === 0) return ''
59 const first = coords.value[0]
60 const last = coords.value[coords.value.length - 1]
61 const bottom = props.height - pad.b
62 return `${linePath.value} L${last.x.toFixed(1)},${bottom} L${first.x.toFixed(1)},${bottom} Z`
63})
64
65const yTicks = computed(() =>
66 [0, 0.5, 1].map((ratio) => ({
67 y: pad.t + (1 - ratio) * (props.height - pad.t - pad.b),
68 text: compact(maxValue.value * ratio),
69 })),
70)
71
72const xTicks = computed(() => {
73 const pts = props.points
74 if (pts.length === 0) return []
75 const idxs = [0, Math.floor((pts.length - 1) / 2), pts.length - 1]
76 return idxs.map((i) => ({ x: coords.value[i].x, text: clock(pts[i].ts) }))
77})
78
79const latest = computed(() => props.points[props.points.length - 1]?.value ?? 0)
80
81function clock(ts: number): string {
82 const d = new Date(ts * 1000)
83 const hh = String(d.getHours()).padStart(2, '0')
84 const mm = String(d.getMinutes()).padStart(2, '0')
85 return `${hh}:${mm}`
86}
87
88function compact(v: number): string {
89 if (v >= 1_000_000) return `${(v / 1_000_000).toFixed(1)}M`
90 if (v >= 1_000) return `${(v / 1_000).toFixed(1)}k`
91 if (v >= 10) return v.toFixed(0)
92 return v.toFixed(1)
93}
94
95function display(v: number): string {
96 const s = v.toFixed(props.decimals)
97 return props.unit ? `${s} ${props.unit}` : s
98}
99
100defineExpose({ latest, display })
101</script>
102
103<template>
104 <div ref="box" class="chart-box">
105 <svg :width="width" :height="height" class="chart-svg">
106 <defs>
107 <linearGradient :id="`grad-${color.replace('#', '')}`" x1="0" y1="0" x2="0" y2="1">
108 <stop offset="0%" :stop-color="color" stop-opacity="0.28" />
109 <stop offset="100%" :stop-color="color" stop-opacity="0.02" />
110 </linearGradient>
111 </defs>
112 <g>
113 <line
114 v-for="tick in yTicks"
115 :key="'g' + tick.y"
116 :x1="pad.l"
117 :x2="width - pad.r"
118 :y1="tick.y"
119 :y2="tick.y"
120 class="grid"
121 />
122 <text v-for="tick in yTicks" :key="'t' + tick.y" :x="pad.l - 8" :y="tick.y + 4" class="axis" text-anchor="end">
123 {{ tick.text }}
124 </text>
125 </g>
126 <path :d="areaPath" :fill="`url(#grad-${color.replace('#', '')})`" />
127 <path :d="linePath" fill="none" :stroke="color" stroke-width="2" stroke-linejoin="round" stroke-linecap="round" />
128 <g v-for="tick in xTicks" :key="'x' + tick.x">
129 <text :x="tick.x" :y="height - 6" class="axis" :text-anchor="tick.x <= pad.l + 2 ? 'start' : tick.x >= width - pad.r - 2 ? 'end' : 'middle'">
130 {{ tick.text }}
131 </text>
132 </g>
133 </svg>
134 </div>
135</template>
136
137<style scoped>
138.chart-box {
139 width: 100%;
140 overflow: hidden;
141}
142
143.chart-svg {
144 display: block;
145}
146
147.grid {
148 stroke: #e6f1f0;
149 stroke-width: 1;
150 stroke-dasharray: 3 4;
151}
152
153.axis {
154 fill: #8aa6ad;
155 font-size: 10.5px;
156 font-family: inherit;
157}
158</style>