consumptionWater.vue 6.6 KB

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