utilization.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <script lang="ts" setup>
  2. import { ref, onMounted, onUnmounted, nextTick } from 'vue'
  3. import { IotStatApi } from '@/api/pms/stat'
  4. import dayjs from 'dayjs'
  5. import quarterOfYear from 'dayjs/plugin/quarterOfYear'
  6. import * as echarts from 'echarts'
  7. dayjs.extend(quarterOfYear)
  8. const chartRef = ref(null)
  9. let myChart: echarts.ECharts | null = null
  10. const currentTimeType = ref('month')
  11. const timeOptions = [
  12. { label: '本月', value: 'month' },
  13. { label: '本季度', value: 'quarter' },
  14. { label: '本年', value: 'year' }
  15. ]
  16. const getDateRange = (type: 'year' | 'quarter' | 'month') => {
  17. const now = dayjs()
  18. let start: dayjs.Dayjs, end: dayjs.Dayjs
  19. if (type === 'year') {
  20. start = now.startOf('year')
  21. end = now.endOf('year')
  22. } else if (type === 'quarter') {
  23. start = now.startOf('quarter')
  24. end = now.endOf('quarter')
  25. } else {
  26. start = now.startOf('month')
  27. end = now.endOf('month')
  28. }
  29. return {
  30. start: start.format('YYYY-MM-DD HH:mm:ss'),
  31. end: end.format('YYYY-MM-DD HH:mm:ss')
  32. }
  33. }
  34. const fetchData = async () => {
  35. if (myChart) {
  36. myChart.showLoading({
  37. text: '加载中 ...',
  38. color: '#409eff',
  39. textColor: '#B6C8DA',
  40. maskColor: 'rgba(0, 0, 0, 0.2)'
  41. })
  42. }
  43. const { start, end } = getDateRange(currentTimeType.value as 'year' | 'quarter' | 'month')
  44. const params = {
  45. 'createTime[0]': start,
  46. 'createTime[1]': end,
  47. timeType: currentTimeType.value
  48. }
  49. try {
  50. let list: any[] = []
  51. const res = await IotStatApi.getUtilization(params)
  52. if (res && Array.isArray(res)) list = res
  53. renderChart(list)
  54. } catch (error) {
  55. console.error('Workload API Error:', error)
  56. } finally {
  57. myChart?.hideLoading()
  58. }
  59. }
  60. const renderChart = (data: any[]) => {
  61. if (!myChart) return
  62. const xAxisData = data.map((item) => item.projectDeptName.replace('项目部', ''))
  63. const seriesData = data.map((item) => {
  64. const val = item.utilizationRate
  65. if (val === null || val === undefined || isNaN(val)) return 0
  66. return parseFloat((val * 100).toFixed(2))
  67. })
  68. const option: echarts.EChartsOption = {
  69. tooltip: {
  70. trigger: 'axis',
  71. backgroundColor: 'rgba(0,0,0,0.7)',
  72. borderColor: '#409eff',
  73. textStyle: { color: '#fff' },
  74. axisPointer: {
  75. type: 'shadow',
  76. shadowStyle: { color: 'rgba(255, 255, 255, 0.1)' }
  77. },
  78. // 自定义 Tooltip 内容,加上 %
  79. formatter: (params: any) => {
  80. const item = params[0]
  81. return `${item.name}<br/>
  82. <span style="display:inline-block;margin-right:4px;border-radius:10px;width:10px;height:10px;background-color:${item.color.colorStops ? item.color.colorStops[0].color : item.color};"></span>
  83. ${item.seriesName}: <span style="font-weight:bold; color: #fff">${item.value}%</span>`
  84. }
  85. },
  86. grid: { top: '15%', left: '3%', right: '3%', bottom: '5%', containLabel: true },
  87. xAxis: {
  88. data: xAxisData,
  89. type: 'category',
  90. boundaryGap: true,
  91. axisLabel: { color: '#B6C8DA', interval: 0, fontSize: 12 },
  92. axisLine: { lineStyle: { color: '#B6C8DA' } },
  93. axisTick: { show: false }
  94. },
  95. yAxis: {
  96. type: 'value',
  97. name: '利用率 (%)',
  98. axisLabel: { color: '#B6C8DA', formatter: '{value}' },
  99. nameTextStyle: { color: '#B6C8DA', padding: [0, 0, 0, 10] },
  100. axisLine: { lineStyle: { color: '#B6C8DA' } },
  101. splitLine: { lineStyle: { color: '#457794', type: 'dashed' } }
  102. },
  103. series: {
  104. name: '设备利用率',
  105. type: 'bar',
  106. showBackground: true,
  107. backgroundStyle: {
  108. color: 'rgba(180, 180, 180, 0.1)',
  109. borderRadius: [4, 4, 0, 0]
  110. },
  111. itemStyle: {
  112. borderRadius: [4, 4, 0, 0],
  113. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  114. { offset: 0, color: '#23D0F6' },
  115. { offset: 1, color: '#1A7BF8' }
  116. ])
  117. },
  118. emphasis: {
  119. itemStyle: {
  120. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  121. { offset: 0, color: '#4FFBDF' },
  122. { offset: 1, color: '#23D0F6' }
  123. ])
  124. }
  125. },
  126. data: seriesData
  127. }
  128. }
  129. myChart.setOption(option)
  130. }
  131. const handleTimeChange = () => {
  132. fetchData()
  133. }
  134. const resizeChart = () => myChart?.resize()
  135. onMounted(() => {
  136. nextTick(() => {
  137. myChart = echarts.init(chartRef.value, undefined, { renderer: 'canvas' })
  138. fetchData()
  139. window.addEventListener('resize', resizeChart)
  140. })
  141. })
  142. onUnmounted(() => {
  143. window.removeEventListener('resize', resizeChart)
  144. myChart?.dispose()
  145. })
  146. </script>
  147. <template>
  148. <div class="card size-full rounded-lg p-4 flex flex-col">
  149. <div class="flex justify-between items-center mb-4">
  150. <div class="flex items-center gap-2 items-center">
  151. <div class="w-1 h-4 bg-[#00E5FF] rounded-full shadow-[0_0_8px_#00E5FF]"></div>
  152. <div class="text-[#e0e0e0] text-lg font-bold">设备利用率</div>
  153. </div>
  154. <el-segmented
  155. size="default"
  156. v-model="currentTimeType"
  157. :options="timeOptions"
  158. @change="handleTimeChange"
  159. class="dark-segmented w-50!"
  160. block
  161. />
  162. </div>
  163. <div ref="chartRef" class="flex-1 w-full min-h-0"></div>
  164. </div>
  165. </template>
  166. <style scoped>
  167. .card {
  168. background-color: rgb(0 0 0 / 30%);
  169. box-shadow: 0 2px 12px rgb(0 0 0 / 50%);
  170. transition: all 0.3s ease;
  171. &:hover {
  172. box-shadow: 0 4px 16px rgb(0 0 0 / 8%);
  173. }
  174. }
  175. .dark-segmented {
  176. --el-segmented-item-selected-color: #e5eaf3;
  177. --el-border-radius-base: 16px;
  178. --el-segmented-color: #cfd3dc;
  179. --el-segmented-bg-color: #262727;
  180. --el-segmented-item-selected-bg-color: #409eff;
  181. --el-segmented-item-selected-disabled-bg-color: rgb(42 89 138);
  182. --el-segmented-item-hover-color: #e5eaf3;
  183. --el-segmented-item-hover-bg-color: #39393a;
  184. --el-segmented-item-active-bg-color: #424243;
  185. --el-segmented-item-disabled-color: #8d9095;
  186. }
  187. :deep(.el-segmented__item) {
  188. display: flex;
  189. justify-content: center;
  190. align-items: center;
  191. }
  192. </style>