原始數據
let data = {
fields: ['name', 'subject', 'score'],
data: [
['張三', '語文', 86],
['李四', '語文', 92],
['王五', '語文', 56],
['張三', '數學', 100],
['李四', '數學', 88],
['王五', '數學', 98],
['張三', '英語', 62],
['李四', '英語', 83]
]
}
1. 簡樸去重排列
Report.format(data, [{ converge: 'v' }, 2, 1])
輸出
‘score’ | ‘subject’ |
---|---|
86 | ‘語文’ |
92 | ‘語文’ |
56 | ‘語文’ |
100 | ‘數學’ |
88 | ‘數學’ |
98 | ‘數學’ |
62 | ‘英語’ |
83 | ‘英語’ |
2. 聚合(公式)
Report.format(data, [{
converge: 'v',
formula: 'sum'
}, 0, 1, 2], {
name: '姓名',
subject: '科目',
score: '結果'
})
輸出:
‘姓名’ | ‘科目’ | ‘結果’ |
---|---|---|
‘張三’ | ‘語文’ | 86 |
‘李四’ | ‘語文’ | 92 |
‘王五’ | ‘語文’ | 56 |
‘張三’ | ‘數學’ | 100 |
‘李四’ | ‘數學’ | 88 |
‘王五’ | ‘數學’ | 98 |
‘張三’ | ‘英語’ | 62 |
‘李四’ | ‘英語’ | 83 |
” | ” | 665 |
3. 交織聚合(公式)
Report.format(data, [{
field: 0,
converge: 'v',
formula: {
formula: 'sum',
label: '總分'
}
}, {
field: 1,
converge: 'v',
formula: {
formula: 'sum',
label: '總結果'
}
}, 2])
輸出:
‘name’ | ‘subject’ | ‘score’ |
---|---|---|
‘張三’ | ‘語文’ | 86 |
” | ‘數學’ | 100 |
” | ‘英語’ | 62 |
” | ‘總結果’ | 248 |
‘李四’ | ‘語文’ | 92 |
” | ‘數學’ | 88 |
” | ‘英語’ | 83 |
” | ‘總結果’ | 263 |
‘王五’ | ‘語文’ | 56 |
” | ‘數學’ | 98 |
” | ‘總結果’ | 154 |
‘總分’ | ” | 665 |
4. 交織聚合
Report.format(data, [{
field: 0,
converge: 'v',
formula: 'avg'
}, {
field: 1,
converge: 'h',
formula: 'sum'
}, 2])
輸出:
‘name’ | ‘語文’ | ‘數學’ | ‘英語’ | ‘sum’ |
---|---|---|---|---|
‘張三’ | 86 | 100 | 62 | 248 |
‘李四’ | 92 | 88 | 83 | 263 |
‘王五’ | 56 | 98 | 0 | 154 |
‘avg’ | 78.00 | 95.33 | 48.33 | 221.67 |
5. 交織聚合(疏忽細緻)
Report.format(data, [{
field: 0,
converge: 'v',
formula: 'avg'
}, {
field: 1,
converge: 'h',
formula: {
detail: false,
formula: 'sum'
}
}, 2])
輸出:
‘name’ | ‘sum’ |
---|---|
‘張三’ | 248 |
‘李四’ | 263 |
‘王五’ | 154 |
‘avg’ | 221.67 |
6. 平級聚合
Report.format(data, [{
converge: 'v'
}, {
field: 1,
converge: 'v',
formula: {
formula: 'sum',
field: 2
}
}, 2])
輸出:
‘subject’ | ‘sum’ | ‘score’ |
---|---|---|
‘語文’ | 234 | 86 |
” | ” | 92 |
” | ” | 56 |
‘數學’ | 286 | 100 |
” | ” | 88 |
” | ” | 98 |
‘英語’ | 145 | 62 |
” | ” | 83 |
7. 多級交織聚合
Report.format({
fields: ['province', 'city', 'count', 'industry', 'type'],
data: [
["湖北", "武漢", 18000, "房地產", "收入"],
["湖北", "武漢", 10000, "房地產", "收入"],
["湖北", "武漢", 2000, "衛生", "付出"],
["湖北", "武漢", 1200, "衛生", "付出"],
["湖北", "武漢", 8000, "衛生", "收入"],
["湖北", "武漢", 12000, "房地產", "付出"],
["湖北", "黃石", 1500, "衛生", "付出"],
["湖北", "黃石", 3500, "衛生", "收入"],
["湖北", "襄陽", 15800, "房地產", "收入"],
["湖北", "襄陽", 5800, "衛生", "收入"],
["湖北", "襄陽", 11800, "房地產", "付出"],
["湖北", "襄陽", 1800, "衛生", "付出"],
["湖南", "長沙", 16000, "房地產", "收入"],
["湖南", "長沙", 6000, "衛生", "收入"],
["湖南", "長沙", 11500, "房地產", "付出"],
["湖南", "長沙", 1500, "衛生", "付出"],
["湖南", "岳陽", 13600, "房地產", "收入"],
["湖南", "岳陽", 3600, "衛生", "收入"],
["湖南", "岳陽", 11000, "房地產", "付出"],
["湖南", "岳陽", 1000, "衛生", "付出"],
["江西", "南昌", 15400, "房地產", "收入"],
["江西", "南昌", 5400, "衛生", "收入"],
["江西", "南昌", 11800, "房地產", "付出"],
["江西", "南昌", 1800, "衛生", "付出"],
["江西", "九江", 14800, "房地產", "收入"],
["江西", "九江", 4800, "衛生", "收入"],
["江西", "九江", 11600, "房地產", "付出"],
["江西", "九江", 1600, "衛生", "付出"]
]
}, [{
field: 0,
formula: [{ formula: 'sum', label: '全國算計' }],
converge: 'v'
}, {
field: 1,
formula: [{ formula: 'sum', label: '省算計' }],
converge: 'v'
}, {
field: 3,
formula: [{
formula: 'fmt',
label: '總利潤',
format: function(data) {
let fmt = 0;
for (let i = 0, len = data.length; i < len; i += 2) {
fmt += (data[i] - data[i + 1]);
}
return fmt;
}
}],
converge: 'h'
}, {
field: 4,
formula: [{
formula: 'fmt',
label: '利潤',
format: function(data) {
return data[0] - data[1];
}
}],
converge: 'h'
}, {
field: 2
}], { province: '省', city: '市' })
輸出:
省 | 市 | 房地產 | ” | ” | 衛生 | ” | ” | 總利潤 |
---|---|---|---|---|---|---|---|---|
” | ” | 收入 | 付出 | 利潤 | 收入 | 付出 | 利潤 | ” |
湖北 | 武漢 | 28000 | 12000 | 16000 | 8000 | 3200 | 4800 | 20800 |
” | 黃石 | 0 | 0 | 0 | 3500 | 1500 | 2000 | 2000 |
” | 襄陽 | 15800 | 11800 | 4000 | 5800 | 1800 | 4000 | 8000 |
” | 省算計 | 43800 | 23800 | 20000 | 17300 | 6500 | 10800 | 30800 |
湖南 | 岳陽 | 13600 | 11000 | 2600 | 3600 | 1000 | 2600 | 5200 |
” | 長沙 | 16000 | 11500 | 4500 | 6000 | 1500 | 4500 | 9000 |
” | 省算計 | 29600 | 22500 | 7100 | 9600 | 2500 | 7100 | 14200 |
江西 | 九江 | 14800 | 11600 | 3200 | 4800 | 1600 | 3200 | 6400 |
” | 南昌 | 15400 | 11800 | 3600 | 5400 | 1800 | 3600 | 7200 |
” | 省算計 | 30200 | 23400 | 6800 | 10200 | 3400 | 6800 | 13600 |
全國算計 | ” | 103600 | 69700 | 33900 | 37100 | 12400 | 24700 | 58600 |
- 詳細實驗,請移步 https://github.com/shixia226/…
- 迎接提出更多整改意見