UsageAnalysis.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <template>
  2. <el-card class="dashboard-container" shadow="never">
  3. <div class="top-nav">
  4. <div class="nav-item" :class="{ active: activeTab === 'bandwidth' }" @click="activeTab = 'bandwidth'">
  5. <SvgIcon iconClass="Database-network-point" />
  6. <span>带宽图表</span>
  7. </div>
  8. <div class="nav-item" :class="{ active: activeTab === 'users' }" @click="activeTab = 'users'">
  9. <el-icon>
  10. <User />
  11. </el-icon>
  12. <span>用户概况统计</span>
  13. </div>
  14. </div>
  15. <div class="filter-bar">
  16. <el-radio-group v-model="timeRange" class="dark-radio-group">
  17. <el-radio-button value="3h">3小时</el-radio-button>
  18. <el-radio-button value="12h">12小时</el-radio-button>
  19. <el-radio-button value="1d">1天</el-radio-button>
  20. <el-radio-button value="7d">7天</el-radio-button>
  21. </el-radio-group>
  22. <el-date-picker v-model="dateRange" type="datetimerange" range-separator="至" start-placeholder="开始日期"
  23. end-placeholder="结束日期" class="dark-date-picker" />
  24. </div>
  25. <div class="charts-wrapper">
  26. <template v-if="activeTab === 'bandwidth'">
  27. <div class="chart-card">
  28. <div class="chart-title">收发带宽统计</div>
  29. <v-chart class="chart-instance" :option="bandwidthInOutOption" autoresize />
  30. </div>
  31. <div class="chart-card">
  32. <div class="chart-title">连接数统计</div>
  33. <v-chart class="chart-instance" :option="connectionsOption" autoresize />
  34. </div>
  35. </template>
  36. <template v-else>
  37. <div class="chart-card">
  38. <div class="chart-title">新增用户</div>
  39. <v-chart class="chart-instance" :option="newUsersOption" autoresize />
  40. </div>
  41. <div class="chart-card">
  42. <div class="chart-title">活跃用户</div>
  43. <v-chart class="chart-instance" :option="activeUsersOption" autoresize />
  44. </div>
  45. </template>
  46. </div>
  47. </el-card>
  48. </template>
  49. <script setup lang="ts">
  50. import { User } from '@element-plus/icons-vue'
  51. import { use } from 'echarts/core'
  52. import * as echarts from 'echarts/core'
  53. import { LineChart } from 'echarts/charts'
  54. import { GridComponent, LegendComponent, TooltipComponent } from 'echarts/components'
  55. import { CanvasRenderer } from 'echarts/renderers'
  56. import VChart from 'vue-echarts'
  57. import { isDark } from '@/composables/useTheme'
  58. type UsageTab = 'bandwidth' | 'users'
  59. type TimeRange = '3h' | '12h' | '1d' | '7d'
  60. use([CanvasRenderer, LineChart, GridComponent, TooltipComponent, LegendComponent])
  61. const activeTab = ref<UsageTab>('bandwidth')
  62. const timeRange = ref<TimeRange>('3h')
  63. const dateRange = ref<[Date, Date]>([
  64. new Date(2026, 2, 18, 8, 0, 0),
  65. new Date(2026, 2, 18, 11, 0, 0),
  66. ])
  67. function generateTimeData(count = 10) {
  68. const base = +new Date(2026, 2, 18, 11, 25, 2)
  69. return Array.from({ length: count }, (_, i) => {
  70. const date = new Date(base + i * 360000)
  71. return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}`
  72. })
  73. }
  74. function generateRandomData(count = 10, min = 0, max = 10) {
  75. return Array.from({ length: count }, () => Number((Math.random() * (max - min) + min).toFixed(3)))
  76. }
  77. const timeAxisData = generateTimeData(15)
  78. const chartColors = computed(() => ({
  79. textPrimary: isDark.value ? '#e5eaf3' : '#303133',
  80. textSecondary: isDark.value ? '#a3a6ad' : '#909399',
  81. borderColor: isDark.value ? 'rgba(255, 255, 255, 0.1)' : '#e4e7ed',
  82. }))
  83. const tooltip = {
  84. trigger: 'axis',
  85. backgroundColor: 'transparent',
  86. borderWidth: 0,
  87. padding: 0,
  88. extraCssText:
  89. 'background: linear-gradient(180deg, rgba(101, 70, 255, 0.40) 0.33%, rgba(101, 70, 255, 0.10) 93.25%) !important; border: 1px solid #C6BAFF; border-radius: 4px; backdrop-filter: blur(4px); padding: 10px 16px; text-align: left;',
  90. textStyle: {
  91. color: '#fff',
  92. align: 'left',
  93. },
  94. axisPointer: {
  95. type: 'line',
  96. lineStyle: {
  97. color: '#ffffff',
  98. type: 'dashed',
  99. opacity: 0.5,
  100. },
  101. },
  102. } as const
  103. const bandwidthInOutOption = computed(() => ({
  104. tooltip,
  105. legend: {
  106. data: ['实时上传流量', '实时下载流量'],
  107. textStyle: { color: chartColors.value.textSecondary },
  108. top: 0,
  109. left: 100,
  110. },
  111. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  112. xAxis: {
  113. type: 'category',
  114. boundaryGap: false,
  115. data: timeAxisData,
  116. axisLine: { lineStyle: { color: chartColors.value.borderColor } },
  117. axisLabel: { color: chartColors.value.textSecondary },
  118. },
  119. yAxis: {
  120. type: 'value',
  121. axisLine: { lineStyle: { color: chartColors.value.borderColor } },
  122. splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } },
  123. axisLabel: { color: chartColors.value.textSecondary, formatter: '{value}bps' },
  124. },
  125. series: [
  126. {
  127. name: '实时上传流量',
  128. type: 'line',
  129. smooth: true,
  130. lineStyle: { color: '#C9AAFF' },
  131. itemStyle: { color: '#C9AAFF' },
  132. areaStyle: {
  133. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  134. { offset: 0, color: 'rgba(155,89,182,0.3)' },
  135. { offset: 1, color: 'rgba(155,89,182,0)' },
  136. ]),
  137. },
  138. data: generateRandomData(15, 2, 8),
  139. },
  140. {
  141. name: '实时下载流量',
  142. type: 'line',
  143. smooth: true,
  144. lineStyle: { color: '#60CFFF' },
  145. itemStyle: { color: '#60CFFF' },
  146. areaStyle: {
  147. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  148. { offset: 0, color: 'rgba(52,152,219,0.3)' },
  149. { offset: 1, color: 'rgba(52,152,219,0)' },
  150. ]),
  151. },
  152. data: generateRandomData(15, 2, 8),
  153. },
  154. ],
  155. }))
  156. const connectionsOption = computed(() => ({
  157. tooltip,
  158. legend: {
  159. data: ['连接数量'],
  160. textStyle: { color: chartColors.value.textSecondary },
  161. top: 0,
  162. left: 100,
  163. },
  164. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  165. xAxis: {
  166. type: 'category',
  167. boundaryGap: false,
  168. data: timeAxisData,
  169. axisLine: { lineStyle: { color: chartColors.value.borderColor } },
  170. axisLabel: { color: chartColors.value.textSecondary },
  171. },
  172. yAxis: {
  173. type: 'value',
  174. axisLine: { lineStyle: { color: chartColors.value.borderColor } },
  175. splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } },
  176. axisLabel: { color: chartColors.value.textSecondary },
  177. },
  178. series: [
  179. {
  180. name: '连接数量',
  181. type: 'line',
  182. smooth: true,
  183. lineStyle: { color: '#FFBB5C' },
  184. itemStyle: { color: '#FFBB5C' },
  185. areaStyle: {
  186. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  187. { offset: 0, color: 'rgba(243,156,18,0.3)' },
  188. { offset: 1, color: 'rgba(243,156,18,0)' },
  189. ]),
  190. },
  191. data: generateRandomData(15, 0, 1),
  192. },
  193. ],
  194. }))
  195. const newUsersOption = computed(() => ({
  196. tooltip,
  197. legend: {
  198. data: ['Android', 'iOS', 'Windows', 'Linux', 'Unknown'],
  199. textStyle: { color: chartColors.value.textSecondary },
  200. top: 0,
  201. left: 100,
  202. },
  203. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  204. xAxis: {
  205. type: 'category',
  206. boundaryGap: false,
  207. data: timeAxisData,
  208. axisLine: { lineStyle: { color: chartColors.value.borderColor } },
  209. axisLabel: { color: chartColors.value.textSecondary },
  210. },
  211. yAxis: {
  212. type: 'value',
  213. axisLine: { lineStyle: { color: chartColors.value.borderColor } },
  214. splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } },
  215. axisLabel: { color: chartColors.value.textSecondary },
  216. },
  217. series: [
  218. {
  219. name: 'Android',
  220. type: 'line',
  221. smooth: true,
  222. lineStyle: { color: '#60FFC7' },
  223. itemStyle: { color: '#60FFC7' },
  224. areaStyle: {
  225. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  226. { offset: 0, color: 'rgba(96,255,199,0.3)' },
  227. { offset: 1, color: 'rgba(96,255,199,0)' },
  228. ]),
  229. },
  230. data: generateRandomData(15, 0, 1),
  231. },
  232. {
  233. name: 'iOS',
  234. type: 'line',
  235. smooth: true,
  236. lineStyle: { color: '#FFFAC3' },
  237. itemStyle: { color: '#FFFAC3' },
  238. areaStyle: {
  239. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  240. { offset: 0, color: 'rgba(255,250,195,0.3)' },
  241. { offset: 1, color: 'rgba(255,250,195,0)' },
  242. ]),
  243. },
  244. data: generateRandomData(15, 0, 1),
  245. },
  246. {
  247. name: 'Windows',
  248. type: 'line',
  249. smooth: true,
  250. lineStyle: { color: '#FF611D' },
  251. itemStyle: { color: '#FF611D' },
  252. areaStyle: {
  253. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  254. { offset: 0, color: 'rgba(255,97,29,0.3)' },
  255. { offset: 1, color: 'rgba(255,97,29,0)' },
  256. ]),
  257. },
  258. data: generateRandomData(15, 0, 1),
  259. },
  260. {
  261. name: 'Linux',
  262. type: 'line',
  263. smooth: true,
  264. lineStyle: { color: '#E084FF' },
  265. itemStyle: { color: '#E084FF' },
  266. areaStyle: {
  267. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  268. { offset: 0, color: 'rgba(224,132,255,0.3)' },
  269. { offset: 1, color: 'rgba(224,132,255,0)' },
  270. ]),
  271. },
  272. data: generateRandomData(15, 0, 1),
  273. },
  274. {
  275. name: 'Unknown',
  276. type: 'line',
  277. smooth: true,
  278. lineStyle: { color: '#6863FF' },
  279. itemStyle: { color: '#6863FF' },
  280. areaStyle: {
  281. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  282. { offset: 0, color: 'rgba(104,99,255,0.3)' },
  283. { offset: 1, color: 'rgba(104,99,255,0)' },
  284. ]),
  285. },
  286. data: generateRandomData(15, 0, 1),
  287. },
  288. ],
  289. }))
  290. const activeUsersOption = computed(() => ({
  291. tooltip,
  292. legend: {
  293. data: ['活跃用户'],
  294. textStyle: { color: chartColors.value.textSecondary },
  295. top: 0,
  296. left: 100,
  297. },
  298. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  299. xAxis: {
  300. type: 'category',
  301. boundaryGap: false,
  302. data: timeAxisData,
  303. axisLine: { lineStyle: { color: chartColors.value.borderColor } },
  304. axisLabel: { color: chartColors.value.textSecondary },
  305. },
  306. yAxis: {
  307. type: 'value',
  308. axisLine: { lineStyle: { color: chartColors.value.borderColor } },
  309. splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } },
  310. axisLabel: { color: chartColors.value.textSecondary },
  311. },
  312. series: [
  313. {
  314. name: '活跃用户',
  315. type: 'line',
  316. smooth: true,
  317. lineStyle: { color: '#2980b9' },
  318. itemStyle: { color: '#2980b9' },
  319. areaStyle: {
  320. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  321. { offset: 0, color: 'rgba(41,128,185,0.3)' },
  322. { offset: 1, color: 'rgba(41,128,185,0)' },
  323. ]),
  324. },
  325. data: generateRandomData(15, 0, 1),
  326. },
  327. ],
  328. }))
  329. </script>
  330. <style scoped>
  331. .dashboard-container {
  332. color: var(--admin-text-primary);
  333. }
  334. .top-nav {
  335. display: flex;
  336. gap: 10px;
  337. margin-bottom: 20px;
  338. }
  339. .nav-item {
  340. display: flex;
  341. align-items: center;
  342. gap: 6px;
  343. padding: 10px 20px;
  344. background-color: var(--admin-btn-bg);
  345. border-radius: 4px;
  346. cursor: pointer;
  347. color: var(--admin-text-secondary);
  348. transition: all 0.3s;
  349. }
  350. .nav-item.active {
  351. background-color: var(--el-color-primary);
  352. color: #fff;
  353. }
  354. .filter-bar {
  355. display: flex;
  356. align-items: center;
  357. gap: 20px;
  358. margin-bottom: 20px;
  359. border-radius: 6px;
  360. background-color: var(--admin-card-bg);
  361. }
  362. .charts-wrapper {
  363. display: flex;
  364. flex-direction: column;
  365. gap: 20px;
  366. }
  367. .chart-card {
  368. padding: 20px;
  369. border: 1px solid var(--admin-border-color);
  370. border-radius: 6px;
  371. background-color: var(--admin-card-bg);
  372. }
  373. .chart-title {
  374. margin-bottom: 10px;
  375. color: var(--admin-text-primary);
  376. font-size: 14px;
  377. font-weight: 700;
  378. }
  379. .chart-instance {
  380. width: 100%;
  381. height: 350px;
  382. }
  383. </style>