UsageAnalysis.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <template>
  2. <el-card class="dashboard-container" shadow="never">
  3. <!-- 顶部导航切换 -->
  4. <div class="top-nav">
  5. <div class="nav-item" :class="{ active: activeTab === 'bandwidth' }" @click="activeTab = 'bandwidth'">
  6. <SvgIcon iconClass="Database-network-point" />
  7. <span>带宽图表</span>
  8. </div>
  9. <div class="nav-item" :class="{ active: activeTab === 'users' }" @click="activeTab = 'users'">
  10. <el-icon>
  11. <User />
  12. </el-icon>
  13. <span>用户概况统计</span>
  14. </div>
  15. </div>
  16. <!-- 过滤条件区域 -->
  17. <div class="filter-bar">
  18. <el-radio-group v-model="timeRange" class="dark-radio-group">
  19. <el-radio-button label="3h">3小时</el-radio-button>
  20. <el-radio-button label="12h">12小时</el-radio-button>
  21. <el-radio-button label="1d">近1天</el-radio-button>
  22. <el-radio-button label="7d">近7天</el-radio-button>
  23. </el-radio-group>
  24. <el-date-picker v-model="dateRange" type="datetimerange" range-separator="—" start-placeholder="开始日期"
  25. end-placeholder="结束日期" class="dark-date-picker" />
  26. </div>
  27. <!-- 图表展示区域 -->
  28. <div class="charts-wrapper">
  29. <!-- 带宽图表视图 -->
  30. <template v-if="activeTab === 'bandwidth'">
  31. <div class="chart-card">
  32. <div class="chart-title">收发带宽统计</div>
  33. <v-chart class="chart-instance" :option="bandwidthInOutOption" autoresize />
  34. </div>
  35. <div class="chart-card">
  36. <div class="chart-title">连接数统计</div>
  37. <v-chart class="chart-instance" :option="connectionsOption" autoresize />
  38. </div>
  39. </template>
  40. <!-- 用户概况统计视图 -->
  41. <template v-if="activeTab === 'users'">
  42. <div class="chart-card">
  43. <div class="chart-title">新增用户</div>
  44. <v-chart class="chart-instance" :option="newUsersOption" autoresize />
  45. </div>
  46. <div class="chart-card">
  47. <div class="chart-title">活跃用户</div>
  48. <v-chart class="chart-instance" :option="activeUsersOption" autoresize />
  49. </div>
  50. </template>
  51. </div>
  52. </el-card>
  53. </template>
  54. <script setup>
  55. import { ref, computed } from 'vue'
  56. import { Monitor, User } from '@element-plus/icons-vue'
  57. import { use } from 'echarts/core'
  58. import { CanvasRenderer } from 'echarts/renderers'
  59. import { LineChart } from 'echarts/charts'
  60. import { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'
  61. import VChart from 'vue-echarts'
  62. import * as echarts from 'echarts/core'
  63. import { isDark } from '@/composables/useTheme'
  64. // 注册 ECharts 必须的组件
  65. use([CanvasRenderer, LineChart, GridComponent, TooltipComponent, LegendComponent])
  66. // 状态控制
  67. const activeTab = ref('bandwidth')
  68. const timeRange = ref('3h')
  69. const dateRange = ref([new Date(2022, 9, 25, 14, 19, 39), new Date(2022, 9, 25, 14, 19, 39)])
  70. // --- Mock 数据生成工具 ---
  71. const generateTimeData = (count = 10) => {
  72. let base = +new Date(2026, 2, 18, 11, 25, 2)
  73. return Array.from({ length: count }, (_, i) => {
  74. let d = new Date(base + i * 360000); // 间隔递增
  75. return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')} ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}:${String(d.getSeconds()).padStart(2, '0')}`
  76. })
  77. }
  78. const generateRandomData = (count = 10, min = 0, max = 10) => {
  79. return Array.from({ length: count }, () => (Math.random() * (max - min) + min).toFixed(3))
  80. }
  81. const timeAxisData = generateTimeData(15)
  82. // --- 图表通用配置(响应式跟随主题) ---
  83. const chartColors = computed(() => {
  84. return {
  85. textPrimary: isDark.value ? '#e5eaf3' : '#303133',
  86. textSecondary: isDark.value ? '#a3a6ad' : '#909399',
  87. borderColor: isDark.value ? 'rgba(255, 255, 255, 0.1)' : '#e4e7ed',
  88. cardBg: isDark.value ? '#1d1e1f' : '#ffffff'
  89. }
  90. })
  91. const tooltip =
  92. {
  93. trigger: 'axis',
  94. backgroundColor: 'transparent', // 必须设置为透明,否则会覆盖 extraCssText 的背景
  95. borderWidth: 0, // 移除默认边框,使用 CSS 处理
  96. padding: 0, // 移除内边距,完全由 CSS 控制
  97. extraCssText: '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;',
  98. textStyle: {
  99. color: '#fff',
  100. align: 'left'
  101. },
  102. axisPointer: {
  103. type: 'line',
  104. lineStyle: {
  105. color: '#ffffff',
  106. type: 'dashed',
  107. opacity: 0.5
  108. }
  109. }
  110. }
  111. // 1. 收发带宽统计图表 Option
  112. const bandwidthInOutOption = computed(() => ({
  113. tooltip: tooltip,
  114. legend: { data: ['实时上传流量', '实时下载流量'], textStyle: { color: chartColors.value.textSecondary }, top: 0, left: 100 },
  115. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  116. xAxis: { type: 'category', boundaryGap: false, data: timeAxisData, axisLine: { lineStyle: { color: chartColors.value.borderColor } }, axisLabel: { color: chartColors.value.textSecondary } },
  117. yAxis: { type: 'value', axisLine: { lineStyle: { color: chartColors.value.borderColor } }, splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } }, axisLabel: { color: chartColors.value.textSecondary, formatter: '{value}bps' } },
  118. series: [
  119. {
  120. name: '实时上传流量', type: 'line', smooth: true,
  121. lineStyle: { color: '#C9AAFF' }, itemStyle: { color: '#C9AAFF' },
  122. areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(155,89,182,0.3)' }, { offset: 1, color: 'rgba(155,89,182,0)' }]) },
  123. data: generateRandomData(15, 2, 8)
  124. },
  125. {
  126. name: '实时下载流量', type: 'line', smooth: true,
  127. lineStyle: { color: '#60CFFF' }, itemStyle: { color: '#60CFFF' },
  128. areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(52,152,219,0.3)' }, { offset: 1, color: 'rgba(52,152,219,0)' }]) },
  129. data: generateRandomData(15, 2, 8)
  130. }
  131. ]
  132. }))
  133. // 2. 连接数统计图表 Option
  134. const connectionsOption = computed(() => ({
  135. tooltip: tooltip,
  136. legend: { data: ['连接数量'], textStyle: { color: chartColors.value.textSecondary }, top: 0, left: 100 },
  137. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  138. xAxis: { type: 'category', boundaryGap: false, data: timeAxisData, axisLine: { lineStyle: { color: chartColors.value.borderColor } }, axisLabel: { color: chartColors.value.textSecondary } },
  139. yAxis: { type: 'value', axisLine: { lineStyle: { color: chartColors.value.borderColor } }, splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } }, axisLabel: { color: chartColors.value.textSecondary } },
  140. series: [
  141. {
  142. name: '连接数量', type: 'line', smooth: true,
  143. lineStyle: { color: '#FFBB5C' }, itemStyle: { color: '#FFBB5C' },
  144. areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(243,156,18,0.3)' }, { offset: 1, color: 'rgba(243,156,18,0)' }]) },
  145. data: generateRandomData(15, 0, 1)
  146. }
  147. ]
  148. }))
  149. // 3. 新增用户 Option
  150. const newUsersOption = computed(() => ({
  151. tooltip: tooltip,
  152. legend: { data: ['Android', 'IOS', 'Windows', 'Linux', 'Unknown'], textStyle: { color: chartColors.value.textSecondary }, top: 0, left: 100 },
  153. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  154. xAxis: { type: 'category', boundaryGap: false, data: timeAxisData, axisLine: { lineStyle: { color: chartColors.value.borderColor } }, axisLabel: { color: chartColors.value.textSecondary } },
  155. yAxis: { type: 'value', axisLine: { lineStyle: { color: chartColors.value.borderColor } }, splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } }, axisLabel: { color: chartColors.value.textSecondary } },
  156. series: [
  157. {
  158. name: 'Android', type: 'line', smooth: true,
  159. lineStyle: { color: '#60FFC7' }, itemStyle: { color: '#60FFC7' },
  160. areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(96,255,199,0.3)' }, { offset: 1, color: 'rgba(96,255,199,0)' }]) },
  161. data: generateRandomData(15, 0, 1)
  162. },
  163. {
  164. name: 'IOS', type: 'line', smooth: true,
  165. lineStyle: { color: '#FFFAC3' }, itemStyle: { color: '#FFFAC3' },
  166. areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(255,250,195,0.3)' }, { offset: 1, color: 'rgba(255,250,195,0)' }]) },
  167. data: generateRandomData(15, 0, 1)
  168. },
  169. {
  170. name: 'Windows', type: 'line', smooth: true,
  171. lineStyle: { color: '#FF611D' }, itemStyle: { color: '#FF611D' },
  172. areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(255,97,29,0.3)' }, { offset: 1, color: 'rgba(255,97,29,0)' }]) },
  173. data: generateRandomData(15, 0, 1)
  174. },
  175. {
  176. name: 'Linux', type: 'line', smooth: true,
  177. lineStyle: { color: '#E084FF' }, itemStyle: { color: '#E084FF' },
  178. areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(224,132,255,0.3)' }, { offset: 1, color: 'rgba(224,132,255,0)' }]) },
  179. data: generateRandomData(15, 0, 1)
  180. },
  181. {
  182. name: 'Unknown', type: 'line', smooth: true,
  183. lineStyle: { color: '#6863FF' }, itemStyle: { color: '#6863FF' },
  184. areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(104,99,255,0.3)' }, { offset: 1, color: 'rgba(104,99,255,0)' }]) },
  185. data: generateRandomData(15, 0, 1)
  186. }
  187. ]
  188. }))
  189. // 4. 活跃用户 Option
  190. const activeUsersOption = computed(() => ({
  191. tooltip: tooltip,
  192. legend: { data: ['活跃用户'], textStyle: { color: chartColors.value.textSecondary }, top: 0, left: 100 },
  193. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  194. xAxis: { type: 'category', boundaryGap: false, data: timeAxisData, axisLine: { lineStyle: { color: chartColors.value.borderColor } }, axisLabel: { color: chartColors.value.textSecondary } },
  195. yAxis: { type: 'value', axisLine: { lineStyle: { color: chartColors.value.borderColor } }, splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } }, axisLabel: { color: chartColors.value.textSecondary } },
  196. series: [
  197. {
  198. name: '活跃用户', type: 'line', smooth: true,
  199. lineStyle: { color: '#2980b9' }, itemStyle: { color: '#2980b9' },
  200. areaStyle: { color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: 'rgba(41,128,185,0.3)' }, { offset: 1, color: 'rgba(41,128,185,0)' }]) },
  201. data: generateRandomData(15, 0, 1)
  202. }
  203. ]
  204. }))
  205. </script>
  206. <style scoped>
  207. .dashboard-container {
  208. color: var(--admin-text-primary);
  209. }
  210. /* 顶部导航 Tabs */
  211. .top-nav {
  212. display: flex;
  213. gap: 10px;
  214. margin-bottom: 20px;
  215. }
  216. .nav-item {
  217. display: flex;
  218. align-items: center;
  219. gap: 6px;
  220. padding: 10px 20px;
  221. background-color: var(--admin-btn-bg);
  222. border-radius: 4px;
  223. cursor: pointer;
  224. color: var(--admin-text-secondary);
  225. transition: all 0.3s;
  226. }
  227. .nav-item.active {
  228. background-color: var(--el-color-primary);
  229. color: #fff;
  230. }
  231. /* 过滤栏 */
  232. .filter-bar {
  233. background-color: var(--admin-card-bg);
  234. border-radius: 6px;
  235. display: flex;
  236. align-items: center;
  237. gap: 20px;
  238. margin-bottom: 20px;
  239. }
  240. /* 图表区域 */
  241. .charts-wrapper {
  242. display: flex;
  243. flex-direction: column;
  244. gap: 20px;
  245. }
  246. .chart-card {
  247. background-color: var(--admin-card-bg);
  248. border-radius: 6px;
  249. padding: 20px;
  250. border: 1px solid var(--admin-border-color);
  251. }
  252. .chart-title {
  253. font-size: 14px;
  254. color: var(--admin-text-primary);
  255. margin-bottom: 10px;
  256. font-weight: bold;
  257. }
  258. .chart-instance {
  259. height: 350px;
  260. width: 100%;
  261. }
  262. </style>