lineTotable.js
3.06 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
/**
* 组合头部数据
* @param {Array} headData
*/
function autoComplateHead(headData) {
let asisOrgName = [],
defaultHead = [];
// if (headData.length < 8) {
// asisOrgName = headData.map(item => item);
// for (let i = 0, len = 8 - headData.length; i < len; i++) {
// asisOrgName.push('--');
// }
// asisOrgName.forEach((k, index) => {
// defaultHead.push({
// prop: 'defaultOrg' + index,
// label: k
// })
// })
// } else {
headData.forEach((k, index) => {
defaultHead.push({
prop: 'defaultOrg' + index,
label: k
})
})
// }
return defaultHead;
}
/**
* 合并表头和表数据
* @param {Array} data
* @param {Array} headData
*/
function dealData(data, headData) {
let obj = {},
arr = [],
_prop = '';
data.forEach(item => {
// console.log(item.data.length < headData.length)
if (item.length < headData.length) {
for (let i = 0, len = item.length; i < len; i++) {
_prop = headData[i].prop;
obj[_prop] = (item[i] || item[i] == 0) ? item[i] : '--';
}
for (let j = item.length; j < headData.length; j++) {
_prop = headData[j].prop;
obj[_prop] = '--';
}
} else {
for (let i = 0, len = item.length; i < len; i++) {
_prop = headData[i].prop;
obj[_prop] = (item[i] || item[i] == 0) ? item[i] : '--';
}
}
arr.push(obj);
obj = {};
})
return arr;
}
/**
*
* @param {Object} data
*/
export const lineToTab = function(data) {
let columnData = [],
rowData = [],
tabData = [],
result = {};
// x轴转化为表格列
let pushLastArr = JSON.parse(JSON.stringify(data.xaxis.data));
pushLastArr[pushLastArr.length] = '00:00';
let tabFirstColumn = [];
for (let i = 0, len = pushLastArr.length; i < len - 1; i++) {
tabFirstColumn.push(pushLastArr[i] + '-' + pushLastArr[i + 1]);
}
// console.log('tabFirstColumn:', tabFirstColumn)
// 组合table表头
let tabHead = [];
tabHead = data.series.map(item => {
return item.name;
})
tabHead.unshift('时间段');
let tabHeadData = autoComplateHead(tabHead);
// console.log('tabHeadData:', tabHeadData);
// 组合报表列数据
data.series.forEach((item, index) => {
for (let i = 0; i < item.data.length; i++) {
rowData[i] = [];
}
});
data.series.forEach((item, index) => {
item.data.forEach((dataItem, dataIndex, dataArr) => {
rowData[dataIndex].push(dataItem)
})
});
// 合并第一列数据
rowData.forEach((item, index) => {
item.unshift(tabFirstColumn[index])
})
columnData = rowData;
tabData = dealData(columnData, tabHeadData);
result = {
headData: tabHeadData,
bodyData: tabData
}
return result
}