-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathechart.util.js
More file actions
376 lines (359 loc) · 10.2 KB
/
echart.util.js
File metadata and controls
376 lines (359 loc) · 10.2 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* 绘图工具
*
* 初始化js条件
* <script src="/js/jquery.min.js" />
* <script src="/js/echarts/echarts.min.js" />
* <script src="/js/echarts/theme/macarons.js" />
* <script src="/customjs/echartUtil/data.format.js" />
*/
$(function() {
var drawChartUtil = {
/**
* 上下文
*/
doc : window.document,
/**
* echart工具
*/
echartUtil : {
/**
* 线图
* @param chart echart实例
* @param data Object 后端数据
* @param isNewChart boolean 是否为新建图表
* @param option {} 选项参数 {
* dataZoom: true | false 是否开启缩放
* dataZoomFun: 缩放回调函数,只有在dataZoom为true时,该参数有效
* }
*/
drawLine: function(chart, data, isNewChart, option) {
var drawData = data.data, legend = [], drawKey = [],
drawValue = [], single = null, tmp = null, svals = null;
for (var i = 0, len = drawData.length; i < len; i ++) {
single = drawData[i];
legend.push(single.name);
svals = single.value;
drawValue = [];
for (var j = 0, jlen = svals.length; j < jlen; j ++) {
tmp = svals[j];
for (var key in tmp) {
if(i == 0) {
drawKey.push(new Date(key * 1000).format('hh:mm:ss'));
}
drawValue.push(tmp[key]);
}
}
single.clipOverflow = false;
single.type = 'line';
single.data = drawValue;
}
var chartOption = {
grid: {
top: 40
},
legend: {
left : 'center',
bottom : 6,
textStyle: {
color: '#CCCCCC'
},
data: legend
},
tooltip: {
trigger: 'axis',
axisPointer: {
animation: false
}
},
xAxis: {
type: 'category',
axisLine: {
lineStyle: {
color: '#CCCCCC'
}
},
data: drawKey
},
yAxis: {
type: 'value',
axisLine: {
lineStyle: {
color: '#CCCCCC'
}
}
},
series: drawData
};
this.chartSubTitle(data.data, chartOption);
this.chartDataZoom(option, chartOption);
chart.setOption(chartOption);
if(isNewChart && option.dataZoom && option.dataZoomFun) {
var dataValue = drawData[0].value,
getKey = this.getKey;
chart.on('dataZoom', function(e) {
var startIndex = 0, endIndex = 0,
batch = e.batch[0];
if(batch.start == 0 && batch.end == 100) { // 还原状态
startIndex = 0;
endIndex = dataValue.length - 1;
} else { // 缩放状态
startIndex = batch.startValue;
endIndex = batch.endValue;
}
option.dataZoomFun(getKey(dataValue[startIndex]), getKey(dataValue[endIndex]));
});
}
},
/**
* 线图
* @param chart echart实例
* @param data Object 后端数据
* @param isNewChart boolean 是否为新建图表
* @param option {} 选项参数 {
* clickFun: 饼图点击回调函数
* }
*/
drawPie: function(chart, data, isNewChart, option) {
var drawData = data.data;
var legend = [];
for(var i = 0, len = drawData.length; i < len; i ++) {
legend.push(drawData[i].name);
}
var chartOption = {
tooltip: {
trigger: 'item',
formatter: "{b} : {d}%"
},
legend: {
type: 'scroll',
orient: 'vertical',
x: 'left',
top: 20,
left: 20,
textStyle: {
color: '#CCCCCC'
},
data: legend
},
series: {
type : 'pie',
center: ['50%', '55%'],
roseType : 'area',
label: {
normal: {
show: false
},
emphasis: {
show: true
}
},
data : drawData
}
};
// this.chartTitle(data, chartOption);
chart.setOption(chartOption);
if(isNewChart && option.clickFun) {
chart.on('click', function(p) {
option.clickFun(p.data);
});
}
},
/**
* 设置图表表头
* @param data 源数据
* @param chartOption 图标参数
*/
chartTitle: function(data, chartOption) {
var starttime = data.starttime,
endtime = data.endtime,
plotName = data.plotName;
chartOption.title = {
text: plotName,
textStyle: {
color: '#CCCCCC'
},
left: 'center',
subtext: new Date(starttime * 1000).format('yyyy-MM-dd hh:mm:ss') +
' 至 ' + new Date(endtime * 1000).format('yyyy-MM-dd hh:mm:ss')
};
},
/**
* 线图添加极值信息
* @param data 源数据
* @param chartOption 图表参数
*/
chartSubTitle: function(data, chartOption) {
var max = 0, min = 0, avg = 0, sum = 0,
sumlen = 0, maxtmp = -1, mintmp = -1, datatmp = null;
for (var i = 0, len = data.length; i < len; i ++) {
datatmp = data[i].data;
maxtmp = Math.max.apply(null, datatmp);
mintmp = Math.min.apply(null, datatmp);
if(maxtmp > max && maxtmp != -1) {
max = maxtmp;
}
if(mintmp < min && mintmp != -1) {
min = mintmp;
}
sumlen += datatmp.length;
for(var j = 0, jlen = datatmp.length; j < jlen; j ++) {
sum += datatmp[j];
}
}
avg = (sum / sumlen).toFixed(2);
chartOption.title = {
left: 'center',
subtext: "最大值: " + max + " 平均值: " + avg + " 最小值: " + min
};
},
/**
* 是否开启图形缩放
* @param option 开启参数
* @param chartOption 图表参数
*/
chartDataZoom: function(option, chartOption) {
if(option.dataZoom) {
chartOption.toolbox = {
feature: {
dataZoom: {
yAxisIndex: false,
title: {
zoom: '缩放',
back: '还原'
}
}
}
}
}
},
/**
* 获取第一个key值
* @param obj 源数据
*/
getKey: function(obj) {
var key = 0;
for(var k in obj) {
key = k;
break;
}
return parseInt(key);
}
},
/**
* 画图总入口
* @param domId String
* @param titleId String
* @param url String
* @param params {}
* @param option {}
*
*/
drawChart : function(domId, titleId, url, params, option) {
var echartUtil = this.echartUtil,
chartDom = this.doc.getElementById(domId),
myChart = echarts.getInstanceByDom(chartDom),
isNewChart = false;
if(!option) { // 参数选项初始化
option = {};
}
if(!myChart) {
myChart = echarts.init(chartDom, 'macarons');
isNewChart = true;
}
myChart.showLoading();
$.ajax({
url : url,
type : 'POST',
async : true,
data : params,
timeout : 5000,
dataType : 'json',
success : function(data) {
switch (data.type) {
case 'line' : echartUtil.drawLine(myChart, data, isNewChart, option); break;
case 'pie' : echartUtil.drawPie(myChart, data, isNewChart, option); break;
}
$("#" + titleId).text(data.plotName);
},
error : function(xhr, status) {
console.error('图形数据出现错误');
console.error(xhr);
},
complete : function() {
myChart.hideLoading();
}
});
},
/**
* 图形销毁
* @param domId String 节点ID
*
*/
chartDispose : function(domId) {
echarts.dispose(this.doc.getElementById(domId));
},
/**
* 图形重置大小
* @param domId String 节点ID
* @param width number|String 宽度
* @param height number|String 高度
*
*/
chartResize : function(domId, width, height) {
var option = {
width: 'auto',
height: 'auto'
};
if(width) {
option.width = width;
}
if(height) {
option.height = height;
}
var chartDom = this.doc.getElementById(domId);
echarts.getInstanceByDom(chartDom).resize({
opts : option
});
}
};
// 封装入jQuery
$.extend({
/**
* 画图总入口
* @param domId String 节点ID
* @param titleId String 表头ID
* @param url String 访问URL
* @param params {} 后端参数
* @param option {
* line: {
* dataZoom: true | false 是否开启缩放
* dataZoomFun: 缩放回调函数,只有在dataZoom为true时,该参数有效
* }
*
* pie: {
* clickFun: 饼图点击回调函数
* }
* }
*/
drawChart: function(domId, titleId, url, params, option) {
drawChartUtil.drawChart(domId, titleId, url, params, option);
},
/**
* 图形销毁
* @param domId 节点ID
*/
chartDispose : function(domId) {
drawChartUtil.chartDispose(domId);
},
/**
* 图形重置大小
* @param domId String 节点ID
* @param width number|String 宽度
* @param height number|String 高度
*/
chartResize : function(domId, width, height) {
drawChartUtil.chartResize(domId, width, height);
}
});
});