whyczyk
2021-03-23 104e60913624bad05979d1827334b3b1694353cb
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
<template>
    <div class="echarts-wrapper">
        <div class="echarts-content" ref="chart"></div>
        <div class="echarts-text-wrapper">
            <div class="color-strip" :style="{'backgroundColor':resColor}"></div>
            <div class="echarts-text">
                {{title}}
            </div>
        </div>
    </div>
</template>
 
<script>
    import * as echarts from "echarts";
 
    export default {
        name: "AcInput",
        chart: "",
        chartData: "",
        data() {
            return {
                acColor: '#813a74',
                resColor: '#ff649d',
                title: ''
            }
        },
        methods: {
            setOption(opt) {
                this.$options.chart.setOption(opt);
            },
            setData(sendData) {
                this.$options.chartData = sendData;
                this.acColor = sendData.acColor;
                this.resColor = sendData.resColor;
                this.title = sendData.title;
                let option = {
                    title: {
                        text: sendData.data + '%',
                        x: 'center',
                        y: 'center',
                        textStyle: {
                            color: "#fff",
                            fontSize: 40,
                        }
                    },
                    series: [{
                        name: "",
                        type: 'gauge',
                        radius: '90%',
                        startAngle: 180.5,
                        endAngle: -0.5,
                        min: 0,
                        max: 100,
                        title: {
                            show: false
                        },
                        detail: {
                            show: false
                        },
                        axisLine: {
                            show: true,
                            lineStyle: {
                                width: 40,
                                color: [
                                    [
                                        sendData.data / 100, this.acColor
                                    ],
                                    [
                                        1, this.resColor
                                    ]
                                ]
                            }
                        },
                        axisTick: {
                            show: false,
                        },
                        splitLine: {
                            show: false,
                        },
                        axisLabel: {
                            show: false
                        },
                        pointer: {
                            show: false,
                        },
                        itemStyle: {
                            normal: {
                                color: '#FF0000',
                            }
                        },
                        data: [{
                            value: sendData,
                            name: '年售电量情况'
                        }]
                    }]
                };
 
                // 设置配置项
                this.setOption(option);
            },
            resize() {
                setTimeout(() => {
                    this.$options.chart.resize();
                    if (JSON.stringify(this.$options.chartData) != '{}') {
                        this.setData(this.$options.chartData);
                    }
                }, 300)
            }
        },
        mounted() {
            // 基于准备好的dom,初始化echarts实例
            this.$options.chart = echarts.init(this.$refs.chart);
 
            window.addEventListener('resize', this.resize);
        },
        destroyed() {
            window.removeEventListener('resize', this.resize);
        }
    }
</script>
 
<style scoped>
    .echarts-text-wrapper {
        position: absolute;
        top: 56%;
        left: 22.5%;
        width: 55%;
        text-align: center;
    }
 
    .color-strip {
        height: 4px;
    }
 
    .echarts-text {
        display: inline-block;
        margin-top: 8px;
        padding: 8px 20px;
        background-color: #083880;
        border: 1px solid #01b1e0;
        border-radius: 16px;
        font-size: 12px;
    }
</style>