queryform.js
10.4 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
import miment from 'miment'
import {getData} from '@/api/bigscreen'
export default {
data() {
return {
// form select-input key
selectInput: {
keyname: '',
keyword: ''
},
//日期时间快捷选项
datetimeRangePickerOptions: {
shortcuts: [{
text: '今天',
onClick(picker) {
const end = new Date();
const start = new Date(new Date(new Date().getTime()).setHours(0, 0, 0, 0));
picker.$emit('pick', [start, end]);
}
}, {
text: '昨天',
onClick(picker) {
const start = new Date(new Date(new Date().getTime() - 24 * 60 * 60 * 1000).setHours(0, 0, 0, 0));
const end = new Date(new Date(new Date().getTime() - 24 * 60 * 60 * 1000).setHours(23, 59, 59, 999));
picker.$emit('pick', [start, end]);
}
}, {
text: '最近一周',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(miment().add(-1, 'ww').stamp());
picker.$emit('pick', [start, end]);
}
}, {
text: '最近一个月',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(miment().add(-1, 'MM').stamp());
picker.$emit('pick', [start, end]);
}
}, {
text: '最近三个月',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setTime(miment().add(-3, 'MM').stamp());
picker.$emit('pick', [start, end]);
}
}],
// disabledDate(time){
// return time.getTime() > Date.now()
// }
},
}
},
computed: {},
created() {
},
mounted() {
},
destroyed() {
},
methods: {
// 搜索重置搜索页码
search() {
this.params.currentPage = 1;
this.queryByPage();
},
// 把selectInput中的值赋到params查询对象中
parseParamsBySelectInput(keyname, keyword) {
if (this.params === undefined || this.params === null) {
console.warn('query form must bind to params object in vue data')
return
}
// if (!this.params.hasOwnProperty(keyname)) {
// console.warn('params has no field:' + keyname)
// return
// }
if (keyword !== undefined) {
this.params[keyname] = keyword.trim()
}
},
resetForm(data) {
let formKeys = Object.keys(data)
for (let k of formKeys) {
data[k] = null
}
},
handlerInputchange(val) {
this.parseParamsBySelectInput(this.selectInput.keyname, val)
},
// 查询echarts 数据
queryEchartsData(params) {
return new Promise(async (resolve) => {
const {code, data} = await getData(params);
if (code != 200) return
const analysisData = this.analysisChartsData(params, data);
resolve(analysisData)
})
},
// 解析不同图标的数据
analysisChartsData(params, data) {
// widget-barchart 柱线图、widget-linechart 折线图、 widget-barlinechart 柱线图
// widget-piechart 饼图、widget-funnel 漏斗图
// widget-text 文本框
// widge-table 表格(数据不要转)
// widget-stackchart 堆叠图
const chartType = params.chartType
if (
chartType == "widget-barchart" ||
chartType == "widget-linechart" ||
chartType == "widget-barlinechart"
) {
return this.barOrLineChartFn(params.chartProperties, data);
} else if (
chartType == "widget-piechart" ||
chartType == "widget-funnel"
) {
return this.piechartFn(params.chartProperties, data);
} else if (chartType == "widget-text") {
return this.widgettext(params.chartProperties, data)
} else if (chartType == "widget-stackchart") {
return this.stackChartFn(params.chartProperties, data)
}else if(chartType == "custom-linechart"){
return this.barOrLineChartFnCustom(params.chartProperties, data);
} else if(chartType == "monitor-stackchart"){
return this.stackChartFnCustom(params.chartProperties, data)
}else {
return data
}
},
// 柱状图、折线图、柱线图-自定义 // Start LSQ 2022/2/21
barOrLineChartFnCustom(chartProperties, data) {
const ananysicData = {};
let xAxisList = [];
let series = [];
if(data && data[0]){
if(data[0].data.names && data[0].data.names.length>0){
xAxisList=data[0].data.names;
}else{
xAxisList=['00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00']
}
if(data[0].data.series && data[0].data.series.length>0){
series = data[0].data.series;
}else{
series = [{
name: "",
datas: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}];
}
}
ananysicData["xAxis"] = xAxisList;
ananysicData["series"] = series;
return ananysicData;
},
// 柱状图、折线图、柱线图
barOrLineChartFn(chartProperties, data) {
const ananysicData = {};
const xAxisList = [];
const series = [];
for (const key in chartProperties) {
const obj = {};
const seriesData = [];
const value = chartProperties[key];
obj["type"] = value;
for (let i = 0; i < data.length; i++) {
if (value.startsWith("xAxis")) {
// 代表为x轴
xAxisList[i] = data[i][key];
} else {
// 其他的均为series展示数据
seriesData[i] = data[i][key];
}
}
obj["data"] = seriesData;
if (!obj["type"].startsWith("xAxis")) {
series.push(obj);
}
}
ananysicData["xAxis"] = xAxisList;
ananysicData["series"] = series;
return ananysicData;
},
//堆叠图自定义
stackChartFnCustom(chartProperties, data){
const ananysicData = {};
const series = [];
//全部字段字典值
const types = Object.values(chartProperties)
let newData=data[0];
//x轴字段、y轴字段名
const xAxisField = Object.keys(chartProperties)[types.indexOf('xAxis')]
const yAxisField = Object.keys(chartProperties)[types.indexOf('yAxis')]
const dataField = Object.keys(chartProperties)[types.indexOf('bar')]
//x轴数值去重,y轴去重
const xAxisList = this.setUnique(data.map(item => item[xAxisField]))
const yAxisList = this.setUnique(data.map(item => item[yAxisField]))
const dataGroup = this.setGroupBy(data, yAxisField)
let yAxisListNew=yAxisList[0].series;
if(yAxisListNew && yAxisListNew.length>0){
yAxisListNew.map(item=>{
series.push({
name: item.name,
type: item.type,
data: item.data,
})
})
}
ananysicData["xAxis"] = xAxisList[0];
ananysicData["series"] = series;
ananysicData["kpiMap"] = newData.kpiMap;
return ananysicData;
},
//堆叠图
stackChartFn(chartProperties, data) {
const ananysicData = {};
const series = [];
//全部字段字典值
const types = Object.values(chartProperties)
//x轴字段、y轴字段名
const xAxisField = Object.keys(chartProperties)[types.indexOf('xAxis')]
const yAxisField = Object.keys(chartProperties)[types.indexOf('yAxis')]
const dataField = Object.keys(chartProperties)[types.indexOf('bar')]
//x轴数值去重,y轴去重
const xAxisList = this.setUnique(data.map(item => item[xAxisField]))
const yAxisList = this.setUnique(data.map(item => item[yAxisField]))
const dataGroup = this.setGroupBy(data, yAxisField)
for (const key in chartProperties) {
if (chartProperties[key] !== 'yAxis' && !chartProperties[key].startsWith('xAxis')) {
Object.keys(dataGroup).forEach(item => {
const data = new Array(yAxisList.length).fill(0)
dataGroup[item].forEach(res => {
data[xAxisList.indexOf(res[xAxisField])]= res[key]
})
series.push({
name: yAxisList[item],
type: chartProperties[key],
data: data,
})
})
}
}
ananysicData["xAxis"] = xAxisList;
ananysicData["series"] = series;
return ananysicData;
},
// 饼图、漏斗图
piechartFn(chartProperties, data) {
const ananysicData = [];
for (let i = 0; i < data.length; i++) {
const obj = {};
for (const key in chartProperties) {
const value = chartProperties[key];
if (value === "name") {
obj["name"] = data[i][key];
} else {
obj["value"] = data[i][key];
}
}
ananysicData.push(obj);
}
return ananysicData;
},
widgettext(chartProperties, data) {
const ananysicData = [];
for (let i = 0; i < data.length; i++) {
const obj = {};
for (const key in chartProperties) {
const value = chartProperties[key];
if (value === "name") {
} else {
obj["value"] = data[i][key];
}
}
ananysicData.push(obj);
}
return ananysicData;
},
setUnique(arr) {
let newArr = [];
arr.forEach(item => {
return newArr.includes(item) ? '' : newArr.push(item);
});
return newArr;
},
setGroupBy(array, name) {
const groups = {}
array.forEach(function (o) {
const group = JSON.stringify(o[name])
groups[group] = groups[group] || []
groups[group].push(o)
})
return Object.keys(groups).map(function (group) {
return groups[group]
})
},
},
watch: {
'selectInput.keyname'(newVal, oldVal) {
this.resetForm(this.params)
this.params.currentPage = 1
this.params.pageSize = 10
this.parseParamsBySelectInput(newVal, this.selectInput.keyword)
},
'selectInput.keyword'(newVal, oldVal) {
if (!this.selectInput.keyname) return
this.parseParamsBySelectInput(this.selectInput.keyname, newVal)
}
// 'selectInput.keyword'(newVal, oldVal) {
// this.parseParamsBySelectInput(this.selectInput.keyname, newVal)
// }
}
}