| 1 | <template> |
| 2 | <div class="traffic-chart-container" v-loading="loading"> |
| 3 | <div v-if="!loading && chartData.length > 0" class="chart-wrapper"> |
| 4 | <div class="y-axis"> |
| 5 | <div class="y-label">{{ formatFileSize(maxVal) }}</div> |
| 6 | <div class="y-label">{{ formatFileSize(maxVal / 2) }}</div> |
| 7 | <div class="y-label">0</div> |
| 8 |
| </div> |
| 9 |
| 10 | <div class="bars-area"> |
| 11 | <!-- Grid Lines --> |
| 12 | <div class="grid-line top"></div> |
| 13 | <div class="grid-line middle"></div> |
| 14 | <div class="grid-line bottom"></div> |
| 15 |
| 16 | <div v-for="(item, index) in chartData" :key="index" class="day-column"> |
| 17 | <div class="bars-group"> |
| 18 | <el-tooltip |
| 19 | :content="`In: ${formatFileSize(item.in)}`" |
| 20 | placement="top" |
| 21 | > |
| 22 | <div |
| 23 | class="bar bar-in" |
| 24 | :style="{ height: Math.max(item.inPercent, 1) + '%' }" |
| 25 | ></div> |
| 26 | </el-tooltip> |
| 27 | <el-tooltip |
| 28 | :content="`Out: ${formatFileSize(item.out)}`" |
| 29 | placement="top" |
| 30 | > |
| 31 | <div |
| 32 | class="bar bar-out" |
| 33 | :style="{ height: Math.max(item.outPercent, 1) + '%' }" |
| 34 | ></div> |
| 35 | </el-tooltip> |
| 36 | </div> |
| 37 | <div class="date-label">{{ item.date }}</div> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 |
| 42 | <!-- Legend --> |
| 43 | <div v-if="!loading && chartData.length > 0" class="legend"> |
| 44 | <div class="legend-item"><span class="dot in"></span> Traffic In</div> |
| 45 | <div class="legend-item"><span class="dot out"></span> Traffic Out</div> |
| 46 | </div> |
| 47 |
| 48 | <el-empty v-else-if="!loading" description="No traffic data" /> |
| 49 | </div> |
| 50 | </template> |
| 51 |
| 52 | <script setup lang="ts"> |
| 53 | import { ref, onMounted } from 'vue' |
| 54 | import { ElMessage } from 'element-plus' |
| 55 | import { formatFileSize } from '../utils/format' |
| 56 | import { getProxyTraffic } from '../api/proxy' |
| 57 | import type { TrafficResponse } from '../types/proxy' |
| 58 |
| 59 | const props = defineProps<{ |
| 60 | proxyName: string |
| 61 | }>() |
| 62 |
| 63 | const loading = ref(false) |
| 64 | const chartData = ref< |
| 65 | Array<{ |
| 66 | date: string |
| 67 | in: number |
| 68 | out: number |
| 69 | inPercent: number |
| 70 | outPercent: number |
| 71 | }> |
| 72 | >([]) |
| 73 | const maxVal = ref(0) |
| 74 |
| 75 | const formatDateLabel = (date: string) => { |
| 76 | const parts = date.split('-') |
| 77 | if (parts.length !== 3) return date |
| 78 | return `${Number(parts[1])}-${Number(parts[2])}` |
| 79 | } |
| 80 |
| 81 | const processData = (history: TrafficResponse['history'] = []) => { |
| 82 | const points = history || [] |
| 83 | const maxIn = Math.max(0, ...points.map((item) => item.trafficIn)) |
| 84 | const maxOut = Math.max(0, ...points.map((item) => item.trafficOut)) |
| 85 | maxVal.value = Math.max(maxIn, maxOut, 100) // Minimum scale 100 bytes |
| 86 |
| 87 | chartData.value = points.map((item) => ({ |
| 88 | date: formatDateLabel(item.date), |
| 89 | in: item.trafficIn, |
| 90 | out: item.trafficOut, |
| 91 | inPercent: (item.trafficIn / maxVal.value) * 100, |
| 92 | outPercent: (item.trafficOut / maxVal.value) * 100, |
| 93 | })) |
| 94 | } |
| 95 |
| 96 | const fetchData = () => { |
| 97 | loading.value = true |
| 98 | getProxyTraffic(props.proxyName) |
| 99 | .then((json) => { |
| 100 | processData(json.history) |
| 101 | }) |
| 102 | .catch((err) => { |
| 103 | ElMessage({ |
| 104 | showClose: true, |
| 105 | message: 'Get traffic info failed! ' + err, |
| 106 | type: 'warning', |
| 107 | }) |
| 108 | }) |
| 109 | .finally(() => { |
| 110 | loading.value = false |
| 111 | }) |
| 112 | } |
| 113 |
| 114 | onMounted(() => { |
| 115 | fetchData() |
| 116 | }) |
| 117 | </script> |
| 118 |
| 119 | <style scoped> |
| 120 | .traffic-chart-container { |
| 121 | width: 100%; |
| 122 | height: 400px; |
| 123 | display: flex; |
| 124 | flex-direction: column; |
| 125 | padding: 20px; |
| 126 | } |
| 127 |
| 128 | .chart-wrapper { |
| 129 | flex: 1; |
| 130 | display: flex; |
| 131 | gap: 10px; |
| 132 | position: relative; |
| 133 | margin-bottom: 20px; |
| 134 | } |
| 135 |
| 136 | .y-axis { |
| 137 | display: flex; |
| 138 | flex-direction: column; |
| 139 | justify-content: space-between; |
| 140 | text-align: right; |
| 141 | font-size: 12px; |
| 142 | color: #909399; |
| 143 | padding-bottom: 24px; /* Align with bars area excluding date labels */ |
| 144 | height: calc(100% - 24px); /* Subtract date label height approx */ |
| 145 | } |
| 146 |
| 147 | .bars-area { |
| 148 | flex: 1; |
| 149 | display: flex; |
| 150 | justify-content: space-between; |
| 151 | align-items: flex-end; |
| 152 | position: relative; |
| 153 | height: 100%; |
| 154 | padding-bottom: 24px; /* Space for date labels */ |
| 155 | } |
| 156 |
| 157 | .grid-line { |
| 158 | position: absolute; |
| 159 | left: 0; |
| 160 | right: 0; |
| 161 | height: 1px; |
| 162 | background-color: #e4e7ed; |
| 163 | z-index: 0; |
| 164 | } |
| 165 |
| 166 | html.dark .grid-line { |
| 167 | background-color: #3a3d5c; |
| 168 | } |
| 169 |
| 170 | .grid-line.top { |
| 171 | top: 0; |
| 172 | } |
| 173 | .grid-line.middle { |
| 174 | top: 50%; |
| 175 | transform: translateY(-50%); |
| 176 | } |
| 177 | .grid-line.bottom { |
| 178 | bottom: 24px; |
| 179 | } /* Align with bottom of bars */ |
| 180 |
| 181 | .day-column { |
| 182 | flex: 1; |
| 183 | height: 100%; |
| 184 | display: flex; |
| 185 | flex-direction: column; |
| 186 | justify-content: flex-end; |
| 187 | align-items: center; |
| 188 | position: relative; |
| 189 | z-index: 1; |
| 190 | } |
| 191 |
| 192 | .bars-group { |
| 193 | height: 100%; |
| 194 | display: flex; |
| 195 | align-items: flex-end; |
| 196 | gap: 4px; |
| 197 | width: 60%; |
| 198 | } |
| 199 |
| 200 | .bar { |
| 201 | flex: 1; |
| 202 | border-radius: 4px 4px 0 0; |
| 203 | transition: height 0.3s ease; |
| 204 | min-height: 1px; |
| 205 | } |
| 206 |
| 207 | .bar-in { |
| 208 | background-color: #5470c6; |
| 209 | } |
| 210 |
| 211 | .bar-out { |
| 212 | background-color: #91cc75; |
| 213 | } |
| 214 |
| 215 | .bar:hover { |
| 216 | opacity: 0.8; |
| 217 | } |
| 218 |
| 219 | .date-label { |
| 220 | position: absolute; |
| 221 | bottom: -24px; |
| 222 | font-size: 12px; |
| 223 | color: #909399; |
| 224 | width: 100%; |
| 225 | text-align: center; |
| 226 | } |
| 227 |
| 228 | .legend { |
| 229 | display: flex; |
| 230 | justify-content: center; |
| 231 | gap: 24px; |
| 232 | margin-top: 10px; |
| 233 | } |
| 234 |
| 235 | .legend-item { |
| 236 | display: flex; |
| 237 | align-items: center; |
| 238 | gap: 8px; |
| 239 | font-size: 14px; |
| 240 | color: #606266; |
| 241 | } |
| 242 |
| 243 | html.dark .legend-item { |
| 244 | color: #e5e7eb; |
| 245 | } |
| 246 |
| 247 | .dot { |
| 248 | width: 12px; |
| 249 | height: 12px; |
| 250 | border-radius: 50%; |
| 251 | } |
| 252 |
| 253 | .dot.in { |
| 254 | background-color: #5470c6; |
| 255 | } |
| 256 | .dot.out { |
| 257 | background-color: #91cc75; |
| 258 | } |
| 259 | </style> |