fuliqi
2024-03-12 f72f2916f7606a38232223a4092590034852fd2d
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<template>
  <div class="data-chart-container">
    <el-card class="data-card" :body-style="{ height: '100%' }">
      <div class="card-content">
        <div class="title-container">
          <h1>运维监控报表</h1>
          <div class="select-container">
            <el-select v-model="company" placeholder="请选择运维公司" @change="companyChange">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
          <div class="date-container">
            <el-select v-model="dateRange" placeholder="请选择时间范围">
              <el-option v-for="item in options2" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="chart-container">
          <div id="chartContent" ref="chartContent"></div>
        </div>
      </div>
    </el-card>
  </div>
</template>
 
<script>
import * as echarts from 'echarts';
let lineChart = null;
let observer = null;
export default {
  name: 'DataChart',
  data() {
    return {
      dateRange: '',
      company: '',
      options: [
        { label: '全部', value: '全部' },
        { label: 'XX运维公司1', value: 'XX运维公司1' },
        { label: 'XX运维公司2', value: 'XX运维公司2' },
        { label: 'XX运维公司3', value: 'XX运维公司3' },
        { label: 'XX运维公司4', value: 'XX运维公司4' },
      ],
      options2: [
        { label: '日工单数', value: '日工单数' },
        { label: '周工单数', value: '周工单数' },
        { label: '月工单数', value: '月工单数' },
      ],
      dataList: [
        {
          name: '全部',
          complete: { '1月': 10310, '2月': 23200, '3月': 16430, '4月': 24000, '5月': 31700 },
          waiting: { '1月': 1500, '2月': 2310, '3月': 14530, '4月': 231, '5月': 1235 },
          pending: { '1月': 2530, '2月': 2450, '3月': 4000, '4月': 4300, '5月': 3210 }
        },
        {
          name: 'XX运维公司1',
          complete: { '1月': 1000, '2月': 2131, '3月': 1233, '4月': 2132, '5月': 3211 },
          waiting: { '1月': 123, '2月': 213, '3月': 1412, '4月': 23, '5月': 123 },
          pending: { '1月': 200, '2月': 235, '3月': 300, '4月': 400, '5月': 500 }
        },
        {
          name: 'XX运维公司2',
          complete: { '1月': 213, '2月': 2131, '3月': 1233, '4月': 2132, '5月': 3211 },
          waiting: { '1月': 123, '2月': 123, '3月': 1412, '4月': 23, '5月': 123 },
          pending: { '1月': 135, '2月': 422, '3月': 425, '4月': 323, '5月': 400 }
        },
        {
          name: 'XX运维公司3',
          complete: { '1月': 1000, '2月': 2131, '3月': 1233, '4月': 2132, '5月': 3211 },
          waiting: { '1月': 123, '2月': 213, '3月': 123, '4月': 23, '5月': 123 },
          pending: { '1月': 534, '2月': 435, '3月': 422, '4月': 121, '5月': 313 }
        },
        {
          name: 'XX运维公司4',
          complete: { '1月': 1000, '2月': 2131, '3月': 1233, '4月': 2132, '5月': 3211 },
          waiting: { '1月': 123, '2月': 613, '3月': 1412, '4月': 2336, '5月': 123 },
          pending: { '1月': 123, '2月': 321, '3月': 131, '4月': 321, '5月': 421 }
        },
        {
          name: 'XX运维公司5',
          complete: { '1月': 1000, '2月': 433, '3月': 1233, '4月': 2132, '5月': 8886 },
          waiting: { '1月': 123, '2月': 213, '3月': 1412, '4月': 23, '5月': 123 },
          pending: { '1月': 333, '2月': 245, '3月': 233, '4月': 222, '5月': 111 }
        },
      ],
    }
  },
  methods: {
    initChart() {
      const option = {
        legend: {
          right: 'right',
          top: 'top',
          icon: 'rect',
          orient: "vertical",
          data: [
            {
              name: '完成工单数',
              itemStyle: {
                color: 'rgba(62, 144, 247, 1)'
              }
            },
            {
              name: '待完成工单数',
              itemStyle: {
                color: 'rgba(85, 192, 191, 1)'
              }
            },
            {
              name: '待审批工单数',
              itemStyle: {
                color: 'rgba(255, 165, 0, 1)'
              }
            },
          ],
        },
        grid: {
          left: 0,
          right: 0,
          bottom: 0,
          top: '20%',
          containLabel: true
        },
        tooltip: {},
        xAxis: {
          type: 'category',
          data: Object.keys(this.acitveData.complete),
        },
        yAxis: {},
        series: [
          {
            name: '完成工单数',
            data: Object.entries(this.acitveData.complete).map(([key, value]) => value),
            type: 'line',
            itemStyle: {
              color: 'rgba(62, 144, 247, 1)'
            }
          },
          {
            name: '待完成工单数',
            data: Object.entries(this.acitveData.waiting).map(([key, value]) => value),
            type: 'line',
            itemStyle: {
              color: 'rgba(85, 192, 191, 1)'
            }
          },
          {
            name: '待审批工单数',
            data: Object.entries(this.acitveData.pending).map(([key, value]) => value),
            type: 'line',
            itemStyle: {
              color: 'rgba(255, 165, 0, 1)'
            }
          }
        ]
      };
      lineChart.setOption(option, true);
    },
 
 
    companyChange() {
      this.acitveData = this.dataList.find((item) => {
        return item.name === this.company;
      });
      if (this.acitveData) {
        this.initChart();
      }
    },
 
    // 监听变化
    observe() {
      if (!observer) {
        observer = new ResizeObserver(entries => {
          this.handleResize();
        })
      }
      observer.observe(this.$refs.chartContent);
    },
    // 窗口变换
    handleResize() {
      if (lineChart) {
        lineChart.resize();
      }
    }
 
  },
  mounted() {
    this.acitveData = this.dataList[0];
    this.company = this.acitveData.name;
    lineChart = echarts.init(this.$refs.chartContent);
    this.initChart();
    this.observe();
  },
  beforeDestroy() {
    if (lineChart) {
      lineChart.dispose();
      observer.unobserve(this.$refs.chartContent);
    }
  },
}
</script>
 
<style lang="scss" scoped>
.data-chart-container {
  height: 400px;
  margin-bottom: 20px;
 
  .data-card {
    height: 100%;
 
    .card-content {
      width: 100%;
      height: 100%;
      position: relative;
    }
  }
}
 
.title-container {
  position: absolute;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 2;
 
 
  .more-button {
    cursor: pointer;
    font-size: 16px;
    padding: 0 10px;
  }
}
 
.chart-container {
 
  width: 100%;
  height: 100%;
 
  #chartContent {
    width: 100%;
    height: 100%;
  }
}
 
.select-container {
  margin: 0 20px;
  width: 180px;
}
</style>