hanbaoshan
2020-07-23 95fc665771abeb335b29e6d59fe74d69cf9c446f
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<template>
  <div ref="cpuMenery" style="height:100%;"></div>
</template>
 
<script>
import echarts from "echarts";
 
export default {
  name: "eChartsCpu",
  props: {
    title: {
      type: String,
      defalut: ""
    },
    value: {
      type: Number,
      defalut: 0
    },
    yAxisData: {
      type: Array,
      default: () => {
        return ["硬盘", "CPU", "算力", "内存"]
      }
    },
    xAxisData: {
      type: Array,
      default: () => {
        return []
      }
    },
    width: {
      type: Number,
      default: 500
    },
    size: {
      type: Number,
      defalut: 145
    }
  },
  computed: {
 
  },
  data() {
    return {
      eChartsObj: {}
    }
  },
  mounted() {
    // console.log(this.$refs.cpuMenery.offsetWidth,'this.$refs.cpuMenery',this.$refs.cpuMenery.offsetHeight)
    if (this.$refs.cpuMenery.offsetWidth && this.$refs.cpuMenery.offsetHeight) {
      this.initChart()
    }
    window.addEventListener('resize', this.windowSizeChange)
  },
  watch: {
    value: function (newVal, oldVal) {
      if (newVal !== oldVal) {
        this.initChart()
      }
    }
  },
  methods: {
    initChart() {
      let options = {
        title: {
          // 标题
          text: this.title
        },
        tooltip: {
          formatter: (data) => {
            return data.value + '%'
          }
        },
        grid: {
          top: 10,
          right: 30,
          bottom: 20,
          left: 40,
        },
        xAxis: {
          show: false,
          type: 'value',
          max: 100,
          splitLine: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#dfe6ec'
            }
          },
          axisTick: {
            lineStyle: {
              color: '#606266'
            }
          },
          axisLabel: {
            color: '#606266'
          },
        },
        yAxis: {
          show: true,
          type: 'category',
          data: this.yAxisData,
          axisLabel: {
            interval: 0,
            rotate: 0
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: '#dfe6ec'
            }
          },
          axisTick: {
            show: false,
            lineStyle: {
              color: '#606266'
            }
          },
          axisLabel: {
            color: '#606266',
            align: 'right'
          },
        },
        series: [
          {
            type: 'bar',
            stack: 'chart',
            z: 3,
            itemStyle: {
              normal: {
                color: '#3D68E1'
              },
              emphasis: {
                color: '#3D68E1'
              }
            },
            data: this.xAxisData,
            label: {
              show: true,
              position: 'right',
              formatter: (data) => {
                return data.value + '%'
              }
            },
          },
          {
            type: 'bar',
            stack: 'chart',
            silent: true,
            itemStyle: {
              normal: {
                color: 'rgb(248, 248, 248)'
              }
            },
            data: this.xAxisData.map(function (i) {
              return 100 - i;
            }),
            barMaxWidth: 15
          }
        ]
      }
      this.eChartsObj = echarts.init(this.$refs.cpuMenery);
      this.eChartsObj.setOption(options);
    },
    windowSizeChange() {
      this.eChartsObj.resize();
    },
  }
};
</script>
<style lang="scss">
</style>