whychdw
2020-07-11 d6723ba3e825bd3dae5a02e9af78861281b51cc0
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
<template>
    <div class="chart-container" :style="getStyle">
        <div class="chart-content-container" :class="{'chart-content-fixed':isFull}">
            <div class="chart-content" :id="id"></div>
            <!-- <div class="chart-tools">
                <i class="iconfont icon-biaoge"></i>
                <i class="iconfont icon-gongdanguanli-gongda"></i>
                <i class="iconfont"
                @click="changeFullState" 
                :class="{'icon-webicon311': !isFull, 'icon-suoxiao': isFull}"></i>
            </div> -->
        </div>
    </div>
</template>
<script>
// 引入 ECharts 主模块
import ECharts from "echarts/lib/echarts";
//引入折线图
import "echarts/lib/chart/line";
//引入提示框
import "echarts/lib/component/tooltip";
//引入标题
import "echarts/lib/component/title";
//引入图例标志
import "echarts/lib/component/legend";
//区域缩放
import "echarts/lib/component/dataZoom";
// 引入自定义主题
import "./theme/transparent"
 
import {
    getMaxFromArr
} from '../../assets/js/common.js'
 
 
export default {
    props: {
        title: {
            type: String,
            default: ''
        },
        height: {
            type: [Number, String],
            default: 300
        },
        id: {
            type: String,
            default: ''
        },
        end: {
            type: Number,
            default: 100,
        }
    },
    data() {
        return {
            chart: "",
            dataZoom: {
                start: 0,
                end: this.end,
            },
            isFull: false
        }
    },
    methods: {
        setOption(opt) {
            var dataZoom = this.dataZoom;
            // 默认配置项
            var option = {
                animation: false,
                title: {
                    text: this.title,
                    x: 'left',
                    textStyle: {
                        fontSize: 12
                    },
                },
                grid: {
                    left: '0',
                    right: '0',
                    bottom: '1',
                    top: '0',
                    containLabel: true 
                },
                legend: {
                    show: false,
                },
                tooltip: {
                    trigger: 'axis',
                    formatter: function(params) {
                        var res = params[0].name;
                        var cols = Math.ceil(params.length/5);
                        for(var i=0; i<params.length; i++) {
                            if(i%cols == 0) {
                                res += '<br>';
                            }
                            res += params[i].marker+params[i].seriesName
                            +': '+params[i].value+"<span style='display: inline-block;margin-right: 8px'></span>";
                        }
                        return res;
                    }
                },
                xAxis: {
                    show: false,
                    type: 'category',
                    data: []
                },
                yAxis: {
                    show: false,
                    max: null,
                },
                dataZoom: [
                    {
                        type: 'inside',
                        xAxisIndex: [0],
                        start: 0,
                        end: dataZoom.end,
                    },
                ],
                series: []
            };
            // 修改xAxis内容
            if(opt.xAxis == undefined || opt.xAxis.data == undefined) {
                option.xAxis.data = [];
            }else {
                option.xAxis.data = opt.xAxis.data;
            }
 
            // 修改yAxis
            if(opt.series != undefined) {
                option.series = opt.series;
            }
            
            var values = option.series[0]?option.series[0].data:[1];
            var max = getMaxFromArr(values)*2;
            option.yAxis.max = max;
            // 绘制图表
            this.chart.setOption(option);
        },
        changeFullState: function() {
            this.isFull = this.isFull?false:true;
            this.$nextTick(()=>{
                this.chart.resize();
            });
        }
    },
    computed: {
        getStyle(){
            let height = this.height;
            if(typeof height == 'number') {
                height += 'px';
            }
            return {
                height: height,
            }
        }
    },
    mounted() {
        var ele = document.getElementById(this.id);
        this.chart = ECharts.init(ele, 'transparent');
        // 设置配置
        this.setOption({});
        // 监听拖动
        // this.chart.on('dataZoom', (data)=>{
        //     console.log(data);
        // });
    },
    destroyed() {
        // 销毁echarts
        this.chart.dispose();
    },
}
</script>
 
<style scoped>
.chart-content-container,
.chart-content {
    height: 100%;
}
.chart-content-container {
    position: relative;
}
.chart-content-container.chart-content-fixed {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    z-index: 99999999;
    background: url('../../assets/images/dw_bg.png') no-repeat;
    background-size: 100% 100%;
}
.chart-tools {
    position: absolute;
    top: 0.2rem;
    right: 0.2rem;
    z-index: 99;
}
.chart-content-container.chart-content-fixed .chart-tools {
    z-index: 99999999;
}
.chart-tools .iconfont {
    font-size: 0.22rem;
    margin-right: 0.1rem;
}
.chart-tools .iconfont:hover {
    color: #b4afaf;
}
.chart-tools .iconfont:active {
    color: #FF0000;
}
</style>