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
| <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>
| <!-- <object ref="resizeBox" tabindex="-1" type="text/html" aria-hidden='true' data="about:blank" class="resize-box"></object> -->
| </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"
| //监听div变化改变chart图大小
| //import EleResize from "./theme/EleResize.js";
|
| export default {
| props: {
| id: {
| type: String,
| required: true,
| },
| unit: {
| type: String,
| default: '',
| }
| },
| data() {
| return {
| fullScreenState: false,
| name: '',
| names: [],
| }
| },
| methods: {
| getOption(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;
| }
| },
| dataZoom: [
| {
| start: 0,
| end: 5,
| handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
| handleSize: '80%',
| handleStyle: {
| color: '#fff',
| shadowBlur: 3,
| shadowColor: 'rgba(0, 0, 0, 0.6)',
| shadowOffsetX: 2,
| shadowOffsetY: 2
| }
| }
| ],
| grid: {
| left: '3%',
| right: '4%',
| bottom: 60,
| },
| legend: this.getLegend(opt),
| xAxis: this.getXAxis(opt),
| 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),
| };
| return option;
| },
| setOption(opt) {
| this.setNames(opt);
| let option = this.getOption(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,
| }
| }
|
| let legend = opt.legend;
| legend.show = false;
| legend.data = this.names;
| let selected = {};
| this.names.forEach(function(item) {
| selected[item]=false;
| });
|
| // 默认全部不渲染
| legend.selected = selected;
| return opt.legend;
| },
| getXAxis(opt) {
| // 未配置x轴
| if (!opt || !opt.xAxis) {
| return [{
| type: 'category',
| boundaryGap: 0,
| }]
| }
|
| return opt.xAxis;
| },
| // 设置series
| getSeries(opt) {
| // 未配置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;
| });
| // 没有内容
| if(series.length == 0) {
| return [{
| name: '初始化内容',
| type: 'line',
| data: []
| }];
| }
| // 返回
| 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);
| },
| resize() {
| this.$G.chartManage.get(this.id).resize();
| },
| fullScreen() {
| this.fullScreenState = this.fullScreenState ? false : true;
| this.$nextTick(() => {
| // 重置大小
| this.$G.chartManage.get(this.id).resize();
| });
| },
| setLegend(name) {
| let chart = this.$G.chartManage.get(this.id);
| // 关闭上一个显示的线条
| chart.dispatchAction({
| type: 'legendUnSelect',
| name: this.name
| });
| // 设置显示的线条
| chart.dispatchAction({
| type: 'legendSelect',
| name: name
| });
| // 存储线条
| this.name = name;
| },
| appendData(list) {
| let chart = this.$G.chartManage.get(this.id);
| list.forEach(function(item, index){
| chart.appendData({
| seriesIndex: index,
| data: item
| });
| });
| chart.resize();
| },
| setNames(opt) {
| // 未配置series
| if(!opt || !opt.series) {
| this.names = [];
| return;
| }
|
| // 设置名称列表
| this.names = opt.series.map(function(item){
| return item.name;
| });
| }
| },
| 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;
| }
|
| .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;
| }
|
| .resize-box {
| display: block;
| position: absolute;
| top: 0px;
| left: 0px;
| width: 100%;
| height: 100%;
| border: none;
| padding: 0px;
| margin: 0px;
| opacity: 0;
| z-index: -1000;
| pointer-events: none
| }
| </style>
|
|