fuliqi
2024-12-25 301532b5c1cad2c1d45080bfef86579b20486bf5
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
<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="params.unitId" placeholder="请选择运维公司" @change="showData">
              <el-option v-for="item in options" :key="item.label" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
          <div class="date-container">
            <el-date-picker type="monthrange" v-model="params.dateRange" range-separator="至" start-placeholder="开始月份"
              end-placeholder="结束月份" @change="showData">
            </el-date-picker>
          </div>
        </div>
        <div class="chart-container">
          <div id="chartContent" ref="chartContent"></div>
        </div>
      </div>
    </el-card>
  </div>
</template>
 
<script>
import * as echarts from 'echarts';
import { ywUnitList, monitor } from "@/api/platform/home";
let lineChart = null;
let observer = null;
export default {
  name: 'DataChart',
  data() {
    return {
      params: {
        dateRange: '',
        unitId: ''
      },
      options: [],
      dataList: [],
    }
  },
  methods: {
    initChart() {
      const sortedKeys  = Object.keys(this.acitveData.state)
        .sort(); // 按照字符串的字典序对键进行排序
      const option = {
        legend: {
          right: 'right',
          top: 'top',
          orient: "vertical",
          icon: 'rect',
          data: [
            {
              name: '正常数',
              itemStyle: {
                color: 'rgba(62, 144, 247, 1)'
              }
            },
            {
              name: '异常数',
              itemStyle: {
                color: 'rgba(85, 192, 191, 1)'
              }
            },
          ],
        },
        grid: {
          left: 0,
          right: 0,
          bottom: 0,
          top: '20%',
          containLabel: true
        },
        tooltip: {},
        xAxis: {
          type: 'category',
          data: sortedKeys,
        },
        yAxis: {},
        series: [
          {
            name: '正常数',
            data: sortedKeys.map(key => this.acitveData.state[key]),
            type: 'line',
            itemStyle: {
              color: 'rgba(62, 144, 247, 1)'
            }
          },
          {
            name: '异常数',
            data: sortedKeys.map(key => this.acitveData.state2[key]),
            type: 'line',
            itemStyle: {
              color: 'rgba(85, 192, 191, 1)'
            }
          }
        ]
      };
      lineChart.setOption(option, true);
    },
 
 
    showData() {
      monitor(this.params).then(res => {
        this.acitveData = res.data;
        if (Object.keys(this.acitveData).length === 0) {
          this.acitveData = {
              "name": "",
              "state": {},
              "state2": {}
          }
        }
        lineChart = echarts.init(this.$refs.chartContent);
        this.initChart();
        this.observe();
      })
    },
 
    // 监听变化
    observe() {
      if (!observer) {
        observer = new ResizeObserver(entries => {
          this.handleResize();
        })
      }
      observer.observe(this.$refs.chartContent);
    },
    // 窗口变换
    handleResize() {
      if (lineChart) {
        lineChart.resize();
      }
    }
 
  },
  mounted() {
    ywUnitList().then(res => {
      this.options = res.data.data.map(item => {
        return {
          label: item.value,
          value: item.id
        }
      })
      if (this.params.unitId === '') {
        this.params.unitId = this.options[0].value;
      }
      this.showData();
    })
  },
  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>