energy.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <template>
  2. <div class="_consume">
  3. <HeadlineTag type="right" value="报警信息统计分析"></HeadlineTag>
  4. <div class="_consume_mains">
  5. <div ref="chartRef" style="width: 100%; height: 100%;"></div>
  6. </div>
  7. </div>
  8. </template>
  9. <script setup>
  10. import { ref, onMounted, onUnmounted } from 'vue';
  11. import * as echarts from 'echarts';
  12. import HeadlineTag from '@/components/HeadlineTag';
  13. const props = defineProps({
  14. resultData: {
  15. type: Object,
  16. default: {}
  17. }
  18. })
  19. const chartRef = ref(null);
  20. let chart = null;
  21. const generateRandomData = (length, max) => {
  22. const randomData = [];
  23. for (let i = 0; i < length; i++) {
  24. randomData.push(Math.floor(Math.random() * max));
  25. }
  26. return randomData;
  27. };
  28. const handleResize = () => {
  29. if (chart) {
  30. chart.resize();
  31. }
  32. };
  33. onMounted(() => {
  34. if (chartRef.value) {
  35. chart = echarts.init(chartRef.value);
  36. // 修改为 24 小时
  37. // const hours = Array.from({ length: 24 }, (_, i) => `${i}时`);
  38. const hours = []
  39. const option = {
  40. xAxis: {
  41. type: 'category',
  42. // 使用 24 小时数据
  43. data: hours,
  44. axisLabel: {
  45. color: '#fff'
  46. },
  47. },
  48. tooltip: {
  49. trigger: 'axis',
  50. axisPointer: {
  51. type: 'cross',
  52. crossStyle: {
  53. color: '#999'
  54. }
  55. },
  56. textStyle: {
  57. color: '#fafafa',
  58. },
  59. borderColor: 'transparent',
  60. backgroundColor: 'rgba(0, 0, 0, 0.5)',
  61. extraCssText: 'backdrop-filter: blur(6px);',
  62. },
  63. grid: {
  64. top: '10%',
  65. bottom: '10%',
  66. right: '0%',
  67. left: '15%',
  68. },
  69. yAxis: {
  70. type: 'value',
  71. axisLabel: {
  72. show: true,
  73. color: '#fff'
  74. },
  75. splitLine: {
  76. show: false,
  77. lineStyle: {
  78. color: '#44585e'
  79. }
  80. },
  81. axisTick: {
  82. show: false
  83. },
  84. },
  85. series: [
  86. {
  87. name: '报警次数',
  88. // data: generateRandomData(7, 50),
  89. data: [],
  90. type: 'line',
  91. showSymbol: false,
  92. smooth: true,
  93. color: '#00F7FF',
  94. lineStyle: {
  95. width: 2,
  96. },
  97. areaStyle: {
  98. color: new echarts.graphic.LinearGradient(
  99. 0,
  100. 0,
  101. 0,
  102. 1,
  103. [{
  104. offset: 0,
  105. color: 'rgba(0, 247, 255, .6)',
  106. },
  107. {
  108. offset: 0.8,
  109. color: 'rgba(0, 247, 255, .2)',
  110. },
  111. ],
  112. false
  113. ),
  114. shadowColor: 'rgba(0, 0, 0, 0.1)',
  115. shadowBlur: 10,
  116. },
  117. symbol: 'circle',
  118. symbolSize: 6,
  119. }
  120. ]
  121. };
  122. chart.setOption(option);
  123. }
  124. });
  125. const getChartData = (electricityCount) => {
  126. const dates = [];
  127. const countData = [];
  128. Object.entries(electricityCount || {}).forEach(([date, value]) => {
  129. dates.push(value.time);
  130. countData.push(value.count);
  131. });
  132. return { dates, countData };
  133. };
  134. watch(() => props.resultData, (newVal) => {
  135. if (chart) {
  136. const { dates, countData } = getChartData(newVal);
  137. chart.setOption({
  138. xAxis: {
  139. data: dates,
  140. },
  141. series: [{
  142. type: 'line',
  143. data: countData,
  144. }],
  145. })
  146. }
  147. }, { deep: true, immediate: true } // 开启深度监听
  148. )
  149. onUnmounted(() => {
  150. window.removeEventListener('resize', handleResize);
  151. if (chart) {
  152. chart.dispose();
  153. }
  154. });
  155. </script>
  156. <style lang="scss">
  157. ._divider {
  158. height: 1px;
  159. border: 1px dashed #168cdb;
  160. flex: 1;
  161. margin: 0 10px;
  162. }
  163. ._consume {
  164. display: flex;
  165. flex-direction: column;
  166. &_mains {
  167. margin: 10px;
  168. flex: 1;
  169. display: flex;
  170. &_item {
  171. display: flex;
  172. justify-content: space-between;
  173. align-items: center;
  174. padding: 10px;
  175. &_name {
  176. width: 30px;
  177. height: 30px;
  178. background: url("@/assets/images/content_circle_num.png");
  179. background-size: 100% 100%;
  180. background-position: center;
  181. background-repeat: no-repeat;
  182. // animation: scanning 4s linear infinite;
  183. border: 1px solid red;
  184. }
  185. &_flag {
  186. display: flex;
  187. align-items: center;
  188. gap: 3px;
  189. &_item {
  190. width: 30px;
  191. height: 30px;
  192. border: 3px dashed #168cdb;
  193. box-sizing: border-box;
  194. background: #0e6ead;
  195. color: #fff;
  196. border-radius: 50%;
  197. display: flex;
  198. align-items: center;
  199. justify-content: center;
  200. font-size: 14px;
  201. }
  202. }
  203. }
  204. &_item:hover {
  205. cursor: pointer;
  206. background-image: linear-gradient(to right, #168cdb, transparent);
  207. }
  208. }
  209. }
  210. </style>