BAOLH
2026-11-10 4d730af0447c686133130d0116a0fa3dd82c2dca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>
 
<script>
import echarts from 'echarts'
// require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import crudProject from '@/api/project/project'
 
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '600px'
    },
    targetYear: {
      type: String,
      default: null
    },
    projectType: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
 
      crudProject.getProjectCost({ targetYear: this.targetYear }).then(res => {
        let projectNames = []
        let taskHours = []
        let consumedHours = []
        res.forEach(data => {
          projectNames.push(data.projectName)
          taskHours.push(data.taskHours)
          consumedHours.push(data.consumedHours)
        })
 
        // 绘制图表
        this.chart.setOption({
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              crossStyle: {
                color: '#999'
              }
            },
            formatter: '{b} <br/>{a0} : {c0}小时<br/>{a1} : {c1}小时'
          },
          grid: {
            //调整图表位置
            show: false, //是否显示直角坐标系网格
            top: '5%', // 距离图例顶部的位置,可写百分比,可以写数字
            right: '8%',
            bottom: '30%',
            left: '8%'
          },
          legend: {
            data: ['分配工时', '消耗工时']
          },
          xAxis: {
            type: 'category',
            data: projectNames,
            axisLabel: {
                rotate: 90 // 将标签旋转45度,以便更多的文本能够显示
            }
          },
          yAxis: {
            type: 'value',
            axisLabel: {
              formatter: '{value} 小时'
            },
          },
          series: [
            {
              name: '分配工时',
              type: 'bar',
              data: taskHours
            },
            {
              name: '消耗工时',
              type: 'bar',
              data: consumedHours
            }
          ]
        })
      })
    }
  }
}
</script>