PriceProportion.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div class="points-ranking__wrap">
  3. <div class="points-ranking" ref="CategoryProportion"></div>
  4. </div>
  5. </template>
  6. <script>
  7. import echarts from 'echarts'
  8. import { post, timerCommon } from './../http/http'
  9. export default {
  10. name: 'CategoryProportion',
  11. inject: ['parentDutyId', 'hosId', 'dutyId'],
  12. data () {
  13. return {
  14. total: 0,
  15. sign: 'lastWeek',
  16. timer: null,
  17. myChart: null,
  18. gradeRankingList: []
  19. }
  20. },
  21. methods: {
  22. // 画图
  23. draw () {
  24. this.myChart = echarts.init(this.$refs.CategoryProportion)
  25. const option = {
  26. color: ['#006FFF', '#00D5EA'], // 颜色数组
  27. tooltip: {
  28. trigger: 'item'
  29. },
  30. legend: {
  31. top: 16,
  32. left: 8,
  33. textStyle: {
  34. color: '#fff', // 全局文字颜色
  35. fontSize: 14
  36. }
  37. },
  38. grid: {
  39. left: 0,
  40. top: 0,
  41. bottom: 0,
  42. right: 0
  43. },
  44. graphic: [{
  45. type: 'text',
  46. left: 'center',
  47. top: '50%',
  48. style: {
  49. text: `总费用\n${this.total}元`, // 换行显示
  50. textAlign: 'center',
  51. textVerticalAlign: 'middle',
  52. fill: '#949494', // 文字颜色
  53. fontSize: 20
  54. }
  55. }],
  56. series: [
  57. {
  58. name: '',
  59. type: 'pie',
  60. center: ['50%', '58%'],
  61. radius: ['35%', '50%'],
  62. avoidLabelOverlap: true,
  63. label: {
  64. show: true,
  65. color: 'rgba(255, 255, 255, 0.85)',
  66. formatter: (params) => {
  67. return `${params.name}\n${params.value}元`
  68. },
  69. textStyle: {
  70. lineHeight: 14
  71. }
  72. },
  73. labelLine: {
  74. show: true,
  75. lineStyle: {
  76. color: '#265A8A'
  77. }
  78. },
  79. data: this.gradeRankingList
  80. }
  81. ]
  82. }
  83. this.myChart.setOption(option)
  84. },
  85. // 获取数据
  86. async getData () {
  87. this.$emit('signEmit', this.sign)
  88. let startTime
  89. let endTime
  90. if (this.sign === 'lastWeek') {
  91. startTime = this.$moment().subtract(1, 'week').startOf('week').add(1, 'day').format('YYYY-MM-DD HH:mm:ss')
  92. endTime = this.$moment().subtract(1, 'week').endOf('week').add(1, 'day').format('YYYY-MM-DD HH:mm:ss')
  93. } else if (this.sign === 'lastMonth') {
  94. startTime = this.$moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD HH:mm:ss')
  95. endTime = this.$moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD HH:mm:ss')
  96. }
  97. const result = await post(
  98. '/itsm/report/index',
  99. {
  100. parentDutyId: this.parentDutyId,
  101. hosId: this.hosId,
  102. dutyId: this.dutyId,
  103. startDate: startTime,
  104. endDate: endTime,
  105. type: 'itsmPriceProportion'
  106. }
  107. )
  108. result.data = result.data || []
  109. if (result.data.length) {
  110. this.gradeRankingList = [
  111. { name: '总工时', value: result.data[0].work_price || 0, percent: result.data[0].workPercent || '0%' },
  112. { name: '总耗材', value: result.data[0].sum_price || 0, percent: result.data[0].sumPercent || '0%' }
  113. ]
  114. this.total = this.gradeRankingList.reduce((pre, cur) => pre + cur.value, 0)
  115. } else {
  116. this.gradeRankingList = []
  117. this.total = 0
  118. }
  119. this.draw()
  120. this.polling()
  121. },
  122. // 轮询请求
  123. polling () {
  124. clearTimeout(this.timer)
  125. this.timer = setTimeout(() => {
  126. if (this.sign === 'lastWeek') {
  127. this.sign = 'lastMonth'
  128. } else if (this.sign === 'lastMonth') {
  129. this.sign = 'lastWeek'
  130. }
  131. this.getData()
  132. }, timerCommon)
  133. }
  134. },
  135. mounted () {
  136. this.getData()
  137. },
  138. beforeDestroy () {
  139. clearTimeout(this.timer)
  140. }
  141. }
  142. </script>
  143. <style lang="less" scoped>
  144. .points-ranking__wrap {
  145. height: 3.7rem;
  146. overflow: hidden;
  147. position: relative;
  148. .points-ranking {
  149. width: 100%;
  150. height: 100%;
  151. position: absolute;
  152. left: 0;
  153. top: 0;
  154. }
  155. }
  156. </style>