whychdw
2019-07-02 d2f48914142e8e40f432aebb987529f08a22c407
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
<template>
    <div class="graph-container">
        <div class="graph-inner" @dblclick="toggleGraph()">
            <div class="graph"></div>
        </div>
        <Modal
            v-model="modal1"
            fullscreen
            footer-hide>
            
        </Modal>
    </div>
</template>
<script>
import echarts from 'echarts';
import $ from 'jquery';
import {
    getMaxFromArr,      // 从数组中获取最大值
    getMinFromArr       // 从数组中获取最小值
} from '../libs/common';
export default {
    name: 'LineGraph',
    props:{
        unit: {     // 单位
            type: String,
            default: ''
        },
        title: {     // 标题
            type: String,
            default: ''
        },
        subtitle: {
            type: String,
            default: ''
        },
        min: {
            type: [Number, String],
            default: 0
        },
        max: {
            type: [Number, String],
            default: 10
        },
        name: {
            type: [String],
            default: ''
        },
        visualMap:{
            type: [Object, Boolean],
            default: false
        },
        markLine: {
            type: [Object, Boolean],
            default: false
        }
    },
    data: function() {
        var unit = this.unit?'('+this.unit+')':'';
        return {
            modal1: false,
            graphEl: '',
            graph: '',
            option: {
                title: {
                    text: this.title,
                    subtext: this.subtitle,
                    x: 'center',
                },
                tooltip: {
                    trigger: 'axis'
                },
                xAxis: {
                    type: 'category',
                    boundaryGap : false,
                    data:[]
                },
                yAxis: {
                    name: 'y'+unit,
                    type: 'value',
                    min: this.min,
                    max: this.max
                },
                grid: {
                    left: '3%',
                    right: '2%',
                    bottom: '3%',
                    containLabel: true
                },
                visualMap: this.visualMap,
                series: {
                    name: this.name,
                    data: [],
                    type: 'line',
                    smooth: true,
                    markLine: this.markLine
                }
            }
        }
    },
    methods: {
        setOption: function(option, times) {
            this.graph.resize();
            // 是否为第二次加载
            if(times) {
                if(this.option.visualMap) {
                    option.visualMap = this.option.visualMap;
                }
 
                if(this.option.series.markLine) {
                    option.series.markLine = this.option.series.markLine;
                }
            }
            
 
            this.option = $.extend({}, this.option, option);
            var min = getMinFromArr(this.option.series.data);
            var max = getMaxFromArr(this.option.series.data);
            this.option.yAxis.min = min>this.min?this.min:min;
            this.option.yAxis.max = max<this.max?this.max:max;
            //console.log(this.option);
            this.graph.setOption(this.option);
        },
        resize: function() {
            this.graph.resize();
            this.graph.setOption(this.option);
        },
        updateLine: function(visualMap, markLine) {
            this.option.visualMap = visualMap;
            this.option.series.markLine = markLine;
            //console.log(this.option);
            this.graph.setOption(this.option);
        },
        getDataURL: function() {
            var result = this.graph.getDataURL({
                type: 'png',
                backgroundColor: '#FFFFFF'
            });
 
            return result;
        },
        toggleGraph: function() {
            //this.modal1 = this.modal1?false:true;
        }
    },
    mounted: function() {
        var _self = this;
        var option = this.option;
 
        var $el = $(this.$el);
        var graph = $el.find('.graph').get(0);
        this.graphEl = graph;
        this.graph = echarts.init(graph);
        this.setOption();
    }
}
</script>
<style scoped>
    .graph-inner {
        height: 100%;
    }
    .graph {
        width: 100%;
        height: 100%;
    }
</style>