whyczyk
2021-09-30 a6cf5353e907767fd16d7fd37e033794321eebaf
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<template>
    <div class="e-chart-root" @dblclick="fullScreen" :class="{'full-screen': fullScreenState}">
        <div class="e-chart-container">
            <div class="e-chart" :id="id" :ref="id"></div>
            <div class="e-chart-tools" v-if="showTools">
                <i class="iconfont el-icon-yanjingkejian" :class="eleClass" @click="changeEyeState"></i>
            </div>
        </div>
    </div>
</template>
 
<script>
// 引入 ECharts 主模块
import ECharts from "echarts/lib/echarts";
//引入折线图
import "echarts/lib/chart/bar";
//引入提示框
import "echarts/lib/component/tooltip";
//引入标题
import "echarts/lib/component/title";
//引入图例标志
import "echarts/lib/component/legend";
//区域缩放
import "echarts/lib/component/dataZoom";
 
//markeline
import "echarts/lib/component/markLine"
 
 
// 引入自定义主题
import "./theme/transparent"
 
export default {
    props: {
        id: {
            type: String,
            required: true,
        },
        unit: {
            type: String,
            default: '',
        },
        showTools: {
            type: Boolean,
            default: false,
        },
        showLabel: {
            type: Boolean,
            default: true,
        },
        maxColor: {
            type: String,
            default: 'green',
        },
        minColor: {
            type: String,
            default: 'red',
        },
        rightMenu: {
            type: Boolean,
            default: false,
        }
    },
    data() {
        return {
            fullScreenState: false,
            eye: true,
        }
    },
    methods: {
        getOption(opt) {
            let unit = this.unit;
            let alarmVol = this.getAlarmVal(opt);
            // 整体配置项
            let option = {
                animation: false,
                color: this.getColor(opt),
                title: this.getTitle(opt),
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {            // 坐标轴指示器,坐标轴触发有效
                        type: 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
                    },
                    formatter(params) {
                        var res = params[0].name + '<br/>';
                        params.forEach(item => {
                            res += item.marker;
                            res += item.seriesName;
                            res += ' : ' + item.data[1] + unit + '</br>';
                        });
                        return res;
                    }
                },
                grid: {
                    left: '1%',
                    right: '1%',
                    bottom: '2%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        axisLine: {
                            show: true,
                            lineStyle: {
                                color: '#b3b3b3',
                            }
                        },
                        axisLabel: {
                            color: '#737474',
                            textStyle: {
                                fontSize: 10
                            },
                        },
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        axisLine: {
                            show: true,
                            lineStyle: {
                                color: '#b3b3b3',
                            }
                        },
                        axisLabel: {
                            color: '#737474',
                            textStyle: {
                                fontSize: 10
                            },
                        },
                        splitLine: {
                            show: true,
                            lineStyle: {
                                color: '#e8eff8',
                            }
                        },
                        min: function (data) {
                            let min = data.min;
                            if (min == Infinity) {
                                return 0;
                            }
                            min = alarmVol.low == false ? min : alarmVol.low < min ? alarmVol.low : min;
                            return Number((min - min * 0.2).toFixed(2));
                        },
                        max: function (data) {
                            let max = data.max;
                            if (max == -Infinity) {
                                max = 1;
                            }
                            max = alarmVol.high == false ? max : alarmVol.high > max ? alarmVol.high : max;
                            return Number((max + max * 0.2).toFixed(2));
                        }
                    }
                ],
                series: this.getSeries(opt),
            };
            return option;
        },
        setOption(opt) {
            let option = this.getOption(opt);
            // 清理画布
            this.$G.chartManage.get(this.id).clear();
            // 设置配置项
            this.$G.chartManage.get(this.id).setOption(option);
        },
        getAlarmVal(opt) {
            let result = {
                low: false,
                high: false
            };
            if (opt && opt.series && opt.series.length != 0) {
 
                let markLine = opt.series[0].markLine;
                if (opt.series && markLine && markLine.data) {
                    result.low = markLine.data[0].yAxis;
                    result.high = markLine.data[1].yAxis;
                }
            }
 
            return result;
        },
        getColor(opt) {     // 配置自定义颜色
            // 未配置自定义颜色
            if (!opt || !opt.color) {
                return []
            }
 
            // 返回颜色
            return opt.color;
        },
        getTitle(opt) {     // 配置标题
            // 未配置标题
            if (!opt || !opt.title) {
                return {
                    show: false,
                };
            }
            // 返回标题
            return opt.title;
        },
        getSeries(opt) {    // 设置series
            // 未配置series
            if (!opt || !opt.series) {
                return [];
            }
            let minColor = this.minColor;
            let maxColor = this.maxColor;
            // 设置配置项
            let series = opt.series.map(item => {
                let max = this.getMax(item.data);
                let min = this.getMin(item.data);
                item.type = "bar";
 
                // 显示数据
                item.label = {
                    // show: this.showChartLabel,
                    show: false,
                    position: 'top',
                    color: '#737474',
                };
                // 设置颜色
                if (item.hColor) {
                    // 设置背景
                    item.itemStyle = {
                        color: item.hColor,
                    };
                } else {
                    // 设置背景
                    item.itemStyle = {
                        color: function (value) {
                            let val = value.value[1];
                            if (val == max) {
                                return maxColor;
                            } else if (val == min) {
                                return minColor;
                            }
                        }
                    };
                }
                return item;
            });
            // 返回
            return series;
        },
        getMax(list) {
            let arr = list.map(item => {
                return item[1];
            });
            return Math.max.apply(null, arr);
        },
        getMin(list) {
            let arr = list.map(item => {
                return item[1];
            });
            return Math.min.apply(null, arr);
        },
        fullScreen() {
            this.fullScreenState = this.fullScreenState ? false : true;
            this.$nextTick(() => {
                // 重置大小
                this.$G.chartManage.get(this.id).resize();
            });
        },
        resize() {
            // 重置大小
            this.$G.chartManage.get(this.id).resize();
        },
        changeEyeState() {
            this.eye = this.eye ? false : true;
            let option = this.$G.chartManage.get(this.id).getOption();
            this.setOption(option);
        }
    },
    computed: {
        eleClass() {
            return this.eye ? "el-icon-yanjingkejian" : "el-icon-yanjing-bukejian";
        },
        showChartLabel() {
            return this.showLabel && this.eye ? true : false;
        }
    },
    mounted() {
        let self = this;
        this.$refs[this.id].oncontextmenu = function () {
            return false;
        }
        // 基于准备好的dom,初始化echarts实例
        let chart = ECharts.init(this.$refs[this.id], 'transparent');
        // 将图表添加到图表管理
        this.$G.chartManage.set(this.id, chart);
        // 设置配置项
        this.setOption();
 
        // 点击事件
        chart.getZr().on('mousedown', function (params) {
            if (params.which == 3) {
                let pointInPixel = [params.offsetX, params.offsetY];
                if (chart.containPixel('grid', pointInPixel)) {
                    /*单击图标X轴数据,打开详情*/
                    let xIndex = chart.convertFromPixel({ seriesIndex: 0 }, pointInPixel)[0];
                    self.$emit('right-click', {
                        x: params.event.clientX + 16,
                        y: params.event.clientY + 16,
                        xIndex: xIndex
                    });
                }
            }
 
        });
 
        // 根据功能屏蔽右键菜单
        if (this.rightMenu) {
            document.getElementById(this.id).oncontextmenu = function () {
                return false;
            };
        }
    }
}
</script>
 
<style scoped>
.e-chart-root,
.e-chart-container,
.e-chart {
    height: 100%;
    box-sizing: border-box;
}
 
.e-chart-root.full-screen .e-chart-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-size: 100% 100%;
    z-index: 9999;
}
 
.e-chart-tools {
    position: absolute;
    top: 16px;
    right: 16px;
    z-index: 9;
}
 
.e-chart-tools .iconfont {
    margin-left: 8px;
    font-size: 24px;
    cursor: pointer;
    color: #00fefe;
}
 
.e-chart-tools .iconfont:hover {
    color: #04b1b1;
}
 
.e-chart-tools .iconfont:active {
    color: #ff0000;
}
</style>