UsageAnalysis.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // 1. 收发带宽统计图表 Option
  92. const bandwidthInOutOption = computed(() => ({
  93. tooltip: { trigger: 'axis', backgroundColor: chartColors.value.cardBg, borderColor: chartColors.value.borderColor, textStyle: { color: chartColors.value.textPrimary } },
  94. legend: { data: ['实时上传流量', '实时下载流量'], textStyle: { color: chartColors.value.textSecondary }, top: 0, left: 100 },
  95. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  96. xAxis: { type: 'category', boundaryGap: false, data: timeAxisData, axisLine: { lineStyle: { color: chartColors.value.borderColor } }, axisLabel: { color: chartColors.value.textSecondary } },
  97. 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' } },
  98. series: [
  99. {
  100. name: '实时上传流量', type: 'line', smooth: true,
  101. lineStyle: { color: '#C9AAFF' }, itemStyle: { color: '#C9AAFF' },
  102. 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)' }]) },
  103. data: generateRandomData(15, 2, 8)
  104. },
  105. {
  106. name: '实时下载流量', type: 'line', smooth: true,
  107. lineStyle: { color: '#60CFFF' }, itemStyle: { color: '#60CFFF' },
  108. 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)' }]) },
  109. data: generateRandomData(15, 2, 8)
  110. }
  111. ]
  112. }))
  113. // 2. 连接数统计图表 Option
  114. const connectionsOption = computed(() => ({
  115. tooltip: { trigger: 'axis', backgroundColor: chartColors.value.cardBg, borderColor: chartColors.value.borderColor, textStyle: { color: chartColors.value.textPrimary } },
  116. legend: { data: ['连接数量'], textStyle: { color: chartColors.value.textSecondary }, top: 0, left: 100 },
  117. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  118. xAxis: { type: 'category', boundaryGap: false, data: timeAxisData, axisLine: { lineStyle: { color: chartColors.value.borderColor } }, axisLabel: { color: chartColors.value.textSecondary } },
  119. yAxis: { type: 'value', axisLine: { lineStyle: { color: chartColors.value.borderColor } }, splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } }, axisLabel: { color: chartColors.value.textSecondary } },
  120. series: [
  121. {
  122. name: '连接数量', type: 'line', smooth: true,
  123. lineStyle: { color: '#FFBB5C' }, itemStyle: { color: '#FFBB5C' },
  124. 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)' }]) },
  125. data: generateRandomData(15, 0, 1)
  126. }
  127. ]
  128. }))
  129. // 3. 新增用户 Option
  130. const newUsersOption = computed(() => ({
  131. tooltip: { trigger: 'axis', backgroundColor: chartColors.value.cardBg, borderColor: chartColors.value.borderColor, textStyle: { color: chartColors.value.textPrimary } },
  132. legend: { data: ['Android', 'IOS', 'Windows', 'Linux', 'Unknown'], textStyle: { color: chartColors.value.textSecondary }, top: 0, left: 100 },
  133. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  134. xAxis: { type: 'category', boundaryGap: false, data: timeAxisData, axisLine: { lineStyle: { color: chartColors.value.borderColor } }, axisLabel: { color: chartColors.value.textSecondary } },
  135. yAxis: { type: 'value', axisLine: { lineStyle: { color: chartColors.value.borderColor } }, splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } }, axisLabel: { color: chartColors.value.textSecondary } },
  136. series: [
  137. { name: 'Android', type: 'line', smooth: true, data: generateRandomData(15, 0, 1) },
  138. { name: 'IOS', type: 'line', smooth: true, data: generateRandomData(15, 0, 1) },
  139. { name: 'Windows', type: 'line', smooth: true, data: generateRandomData(15, 0, 1) },
  140. { name: 'Linux', type: 'line', smooth: true, data: generateRandomData(15, 0, 1) },
  141. { name: 'Unknown', type: 'line', smooth: true, data: generateRandomData(15, 0, 1) }
  142. ]
  143. }))
  144. // 4. 活跃用户 Option
  145. const activeUsersOption = computed(() => ({
  146. tooltip: { trigger: 'axis', backgroundColor: chartColors.value.cardBg, borderColor: chartColors.value.borderColor, textStyle: { color: chartColors.value.textPrimary } },
  147. legend: { data: ['活跃用户'], textStyle: { color: chartColors.value.textSecondary }, top: 0, left: 100 },
  148. grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  149. xAxis: { type: 'category', boundaryGap: false, data: timeAxisData, axisLine: { lineStyle: { color: chartColors.value.borderColor } }, axisLabel: { color: chartColors.value.textSecondary } },
  150. yAxis: { type: 'value', axisLine: { lineStyle: { color: chartColors.value.borderColor } }, splitLine: { show: true, lineStyle: { color: chartColors.value.borderColor, type: 'dashed' } }, axisLabel: { color: chartColors.value.textSecondary } },
  151. series: [
  152. {
  153. name: '活跃用户', type: 'line', smooth: true,
  154. lineStyle: { color: '#2980b9' }, itemStyle: { color: '#2980b9' },
  155. 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)' }]) },
  156. data: generateRandomData(15, 0, 1)
  157. }
  158. ]
  159. }))
  160. </script>
  161. <style scoped>
  162. .dashboard-container {
  163. color: var(--admin-text-primary);
  164. }
  165. /* 顶部导航 Tabs */
  166. .top-nav {
  167. display: flex;
  168. gap: 10px;
  169. margin-bottom: 20px;
  170. }
  171. .nav-item {
  172. display: flex;
  173. align-items: center;
  174. gap: 6px;
  175. padding: 10px 20px;
  176. background-color: var(--admin-btn-bg);
  177. border-radius: 4px;
  178. cursor: pointer;
  179. color: var(--admin-text-secondary);
  180. transition: all 0.3s;
  181. }
  182. .nav-item.active {
  183. background-color: var(--el-color-primary);
  184. color: #fff;
  185. }
  186. /* 过滤栏 */
  187. .filter-bar {
  188. background-color: var(--admin-card-bg);
  189. border-radius: 6px;
  190. display: flex;
  191. align-items: center;
  192. gap: 20px;
  193. margin-bottom: 20px;
  194. }
  195. /* 图表区域 */
  196. .charts-wrapper {
  197. display: flex;
  198. flex-direction: column;
  199. gap: 20px;
  200. }
  201. .chart-card {
  202. background-color: var(--admin-card-bg);
  203. border-radius: 6px;
  204. padding: 20px;
  205. border: 1px solid var(--admin-border-color);
  206. }
  207. .chart-title {
  208. font-size: 14px;
  209. color: var(--admin-text-primary);
  210. margin-bottom: 10px;
  211. font-weight: bold;
  212. }
  213. .chart-instance {
  214. height: 350px;
  215. width: 100%;
  216. }
  217. </style>