ConfidentialityPositionChart.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <div class="confidentiality-position-chart">
  3. <!-- 标题区域 -->
  4. <div class="chart-header">
  5. <div class="main-title">
  6. <img src="@/assets/images/warning.png" alt="warning" class="warning-icon" />
  7. <span class="title-text">今日抓拍记录</span>
  8. <span class="record-count">{{ todayCount }}</span>
  9. <span class="title-text">条</span>
  10. </div>
  11. <!-- <div class="subtitle">近7日抓拍记录变化折线图</div> -->
  12. </div>
  13. <!-- 图表区域 -->
  14. <div ref="chartRef" class="chart-container"></div>
  15. </div>
  16. </template>
  17. <script setup lang="ts">
  18. import { ref, onMounted, onUnmounted, nextTick } from 'vue';
  19. import * as echarts from 'echarts';
  20. import { getTodayCaptureRecordCount, getRecentDaysCaptureRecordCount } from '@/api/security-confidentiality-position';
  21. const chartRef = ref<HTMLDivElement | null>(null);
  22. let chartInstance: echarts.ECharts | null = null;
  23. const todayCount = ref<number>(0);
  24. // 初始化图表
  25. const initChart = async () => {
  26. if (!chartRef.value) return;
  27. // 获取数据
  28. try {
  29. const [todayRes, recentRes] = await Promise.all([
  30. getTodayCaptureRecordCount(),
  31. getRecentDaysCaptureRecordCount(7),
  32. ]);
  33. todayCount.value = todayRes || 0;
  34. // 销毁旧实例
  35. if (chartInstance) {
  36. chartInstance.dispose();
  37. }
  38. // 初始化图表实例
  39. chartInstance = echarts.init(chartRef.value);
  40. // 处理数据
  41. const dates = recentRes.map((item) => {
  42. const date = new Date(item.date);
  43. const month = String(date.getMonth() + 1).padStart(2, '0');
  44. const day = String(date.getDate()).padStart(2, '0');
  45. return `${month}.${day}`;
  46. });
  47. const counts = recentRes.map((item) => item.count);
  48. // 找到最大值用于高亮显示
  49. const maxCount = Math.max(...counts, 0);
  50. const maxIndex = counts.indexOf(maxCount);
  51. const option: echarts.EChartsOption = {
  52. grid: {
  53. left: '2%',
  54. right: '2%',
  55. top: '25%',
  56. bottom: '0%',
  57. containLabel: true,
  58. },
  59. xAxis: {
  60. type: 'category',
  61. data: dates,
  62. axisLine: {
  63. show: false,
  64. },
  65. axisTick: {
  66. show: false,
  67. },
  68. axisLabel: {
  69. color: '#666',
  70. fontSize: 12,
  71. margin: 10,
  72. },
  73. },
  74. yAxis: {
  75. name: '近7日抓拍记录变化折线图',
  76. nameGap: 26,
  77. type: 'value',
  78. axisLine: {
  79. show: false,
  80. },
  81. axisTick: {
  82. show: false,
  83. },
  84. axisLabel: {
  85. color: '#666',
  86. fontSize: 12,
  87. },
  88. splitLine: {
  89. lineStyle: {
  90. color: '#f0f0f0',
  91. type: 'dashed',
  92. },
  93. },
  94. nameTextStyle: {
  95. verticalAlign: 'bottom',
  96. padding: [0, 0, 5, 80],
  97. },
  98. },
  99. series: [
  100. {
  101. name: '抓拍记录',
  102. type: 'line',
  103. data: counts,
  104. smooth: true,
  105. symbol: 'circle',
  106. symbolSize: 6,
  107. lineStyle: {
  108. color: '#ff8c00',
  109. width: 2,
  110. },
  111. itemStyle: {
  112. color: '#ff8c00',
  113. },
  114. markPoint:
  115. maxCount > 0
  116. ? {
  117. data: [
  118. {
  119. type: 'max',
  120. name: '最大值',
  121. coord: [maxIndex, maxCount],
  122. symbolSize: 0,
  123. label: {
  124. show: true,
  125. position: 'top',
  126. color: '#ff8c00',
  127. fontSize: 14,
  128. fontWeight: 'bold',
  129. formatter: maxCount.toString(),
  130. },
  131. },
  132. ],
  133. }
  134. : undefined,
  135. },
  136. ],
  137. tooltip: {
  138. trigger: 'axis',
  139. backgroundColor: 'rgba(0, 0, 0, 0.6)',
  140. borderColor: 'transparent',
  141. textStyle: {
  142. color: '#fff',
  143. },
  144. formatter: (params: any) => {
  145. const data = params[0];
  146. return `${data.axisValue}<br/>抓拍记录: ${data.value}条`;
  147. },
  148. },
  149. };
  150. chartInstance.setOption(option);
  151. // 响应式处理
  152. window.addEventListener('resize', handleResize);
  153. } catch (error) {
  154. console.error('Failed to load chart data:', error);
  155. todayCount.value = 0;
  156. }
  157. };
  158. // 处理窗口大小变化
  159. const handleResize = () => {
  160. if (chartInstance) {
  161. chartInstance.resize();
  162. }
  163. };
  164. onMounted(async () => {
  165. await nextTick();
  166. initChart();
  167. });
  168. onUnmounted(() => {
  169. window.removeEventListener('resize', handleResize);
  170. if (chartInstance) {
  171. chartInstance.dispose();
  172. }
  173. });
  174. </script>
  175. <style scoped lang="scss">
  176. .confidentiality-position-chart {
  177. width: 100%;
  178. height: 100%;
  179. display: flex;
  180. flex-direction: column;
  181. align-items: center;
  182. justify-content: center;
  183. padding: 5px 20px;
  184. .chart-header {
  185. width: 100%;
  186. // display: flex;
  187. // flex-direction: column;
  188. // align-items: center;
  189. margin-bottom: 8px;
  190. .main-title {
  191. display: flex;
  192. align-items: baseline;
  193. justify-content: center;
  194. height: 30px;
  195. gap: 8px;
  196. .warning-icon {
  197. position: relative;
  198. top: 3px;
  199. width: 20px;
  200. height: 20px;
  201. }
  202. .title-text {
  203. line-height: 20px;
  204. font-size: 16px;
  205. font-weight: 600;
  206. color: #303133;
  207. }
  208. .record-count {
  209. line-height: 30px;
  210. font-size: 24px;
  211. font-weight: 600;
  212. color: #ff5f53;
  213. }
  214. }
  215. }
  216. .chart-container {
  217. width: 100%;
  218. height: 153px;
  219. flex: 1;
  220. }
  221. }
  222. </style>