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
| <template>
| <div class="e-chart-root">
| <div class="e-chart-container">
| <div class="e-chart" :id="id" :ref="id"></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";
|
| //markeline
| import "echarts/lib/component/markLine"
|
| // 引入自定义主题
| import "./theme/transparent"
|
| export default {
| props: {
| id: {
| type: String,
| required: true,
| },
| unit: {
| type: String,
| default: '',
| }
| },
| methods:{
| setOption(opt) {
| let unit = this.unit;
| // 整体配置项
| let option = {
| animation: false,
| color: this.getColor(opt),
| title: this.getTitle(opt),
| tooltip: {
| trigger: 'axis',
| axisPointer: { // 坐标轴指示器,坐标轴触发有效
| type: 'line' // 默认为直线,可选为:'line' | 'shadow'
| },
| appendToBody: true,
| 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
| },
| legend: this.getLegend(opt),
| xAxis: [
| {
| type: 'category',
| boundaryGap: 0,
| }
| ],
| yAxis: [
| {
| type: 'value',
| splitLine: {
| show: true,
| },
| min: function(data) {
| let min = data.min;
| if(min == Infinity) {
| return 0;
| }
| if(min>0) {
| return Math.floor(min*0.9);
| }else {
| return Math.floor(min*1.01);
| }
|
| },
| max: function(data) {
| let max = data.max;
| if(max == -Infinity) {
| return 1;
| }
| return Math.ceil(max*1.01);
| }
| }
| ],
| series: this.getSeries(opt),
| };
| // 清理画布
| this.$G.chartManage.get(this.id).clear();
| // 设置配置项
| this.$G.chartManage.get(this.id).setOption(option);
| },
| getColor(opt) { // 配置自定义颜色
| // 未配置自定义颜色
| if(!opt || !opt.color) {
| return []
| }
|
| // 返回颜色
| return opt.color;
| },
| getTitle(opt) { // 配置标题
| // 未配置标题
| if(!opt || !opt.title) {
| return {
| show: false,
| };
| }
|
| // 返回标题
| return opt.title;
| },
| getLegend(opt){
| // 未配置legend
| if(!opt || !opt.legend) {
| return {
| show: false,
| }
| }
|
| return opt.legend;
| },
| getSeries(opt) { // 设置series
| // 未配置series
| if(!opt || !opt.series) {
| return [];
| }
| // 设置配置项
| let series = opt.series.map(item=>{
| // 设置类型
| item.type="line";
| // 设置平滑的线条,关闭圆圈,设置采样点
| item.smooth=true;
| item.symbolSize=0;
| item.sampling='average';
| 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);
| }
| },
| mounted() {
| // 基于准备好的dom,初始化echarts实例
| let chart = ECharts.init(this.$refs[this.id], 'transparent');
| // 将图表添加到图表管理
| this.$G.chartManage.set(this.id, chart);
| // 设置配置项
| this.setOption();
| }
| }
| </script>
|
| <style scoped>
| .e-chart-root,
| .e-chart-container,
| .e-chart {
| height: 100%;
| box-sizing: border-box;
| }
| </style>
|
|