Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/TotalsDimGroup' into TotalsDimGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
JuZe committed Sep 5, 2023
2 parents 405a73f + 8edb616 commit 57db05a
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 169 deletions.
252 changes: 252 additions & 0 deletions packages/s2-core/__tests__/unit/data-process/new-total-spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/**
* 透视表核心数据流程(保证基本数据正确)
* */
// eslint-disable-next-line import/no-extraneous-dependencies
import { Aggregation } from '@antv/s2';
import { assembleDataCfg, assembleOptions } from '../../util';
import { getContainer } from '../../util/helpers';
import { PivotSheet } from '@/sheet-type';

const myData = [
{
cost: 99099,
number: 99099,
city: '杭州市',
type: '家具',
sub_type: '桌子',
},
{
cost: 99099,
number: 99099,
province: '浙江省',
city: '杭州市',
sub_type: '桌子',
},
{
cost: 99099,
number: 99099,
province: '浙江省',
city: '杭州市',
type: '家具',
},
{
cost: 99099,
number: 99099,
province: '浙江省',
city: '杭州市',
type: '家具',
sub_type: '桌子',
},
{
cost: 356,
number: 3877,
province: '浙江省',
city: '宁波市',
type: '家具',
sub_type: '桌子',
},
{
cost: 5734,
number: 5343,
province: '浙江省',
city: '杭州市',
type: '家具',
sub_type: '沙发',
},
{
cost: 957,
number: 7234,
province: '浙江省',
city: '宁波市',
type: '家具',
sub_type: '沙发',
},
{
cost: 486,
number: 945,
province: '浙江省',
city: '杭州市',
type: '办公用品',
sub_type: '笔',
},
{
cost: 357,
number: 1145,
province: '浙江省',
city: '宁波市',
type: '办公用品',
sub_type: '笔',
},
{
cost: 513,
number: 1343,
province: '浙江省',
city: '杭州市',
type: '办公用品',
sub_type: '纸张',
},
{
cost: 234,
number: 1523,
province: '浙江省',
city: '宁波市',
type: '办公用品',
sub_type: '纸张',
},
{
cost: 456,
number: 1822,
province: '四川省',
city: '绵阳市',
type: '家具',
sub_type: '桌子',
},
{
cost: 2654,
number: 1943,
province: '四川省',
city: '南充市',
type: '家具',
sub_type: '桌子',
},
{
cost: 578,
number: 2244,
province: '四川省',
city: '绵阳市',
type: '家具',
sub_type: '沙发',
},
{
cost: 687,
number: 2333,
province: '四川省',
city: '南充市',
type: '家具',
sub_type: '沙发',
},
{
cost: 345,
number: 245,
province: '四川省',
city: '绵阳市',
type: '办公用品',
sub_type: '笔',
},
{
cost: 756,
number: 2457,
province: '四川省',
city: '南充市',
type: '办公用品',
sub_type: '笔',
},
{
cost: 243,
number: 3077,
province: '四川省',
city: '绵阳市',
type: '办公用品',
sub_type: '纸张',
},
{
cost: 123,
number: 3551,
province: '四川省',
city: '南充市',
type: '办公用品',
sub_type: '纸张',
},
];
const meta = [
{
field: 'number',
name: '数量',
description: '数量说明。。',
},
{
field: 'province',
name: '省份',
description: '省份说明。。',
},
{
field: 'city',
name: '城市',
description: '城市说明。。',
},
{
field: 'type',
name: '类别',
description: '类别说明。。',
},
{
field: 'sub_type',
name: '子类别',
description: '子类别说明。。',
},
{
field: 'cost',
name: '价格',
description: 'Value2',
},
];
describe('Pivot Table Core Data Process', () => {
const s2 = new PivotSheet(
getContainer(),
assembleDataCfg({
data: myData,
meta,
fields: {
rows: ['province', 'city', 'type'],
columns: ['sub_type'],
values: ['number', 'cost'],
valueInCols: false,
// customValueOrder: 2,
},
totalData: [],
}),
assembleOptions({
// hierarchyType: 'tree',
debug: false,
width: 1024,
height: 2600,
interaction: {
brushSelection: { row: true },
enableCopy: true,
},
totals: {
row: {
totalsDimensionsGroup: ['city', 'type'],
subTotalsDimensionsGroup: ['sub_type'],
calcTotals: {
aggregation: Aggregation.SUM,
},
calcSubTotals: {
aggregation: Aggregation.SUM,
},
showGrandTotals: true,
showSubTotals: true,
reverseLayout: true,
reverseSubLayout: true,
subTotalsDimensions: ['province'],
},
// col: {
// subTotalsDimensionsGroup: ['city'],
// totalsDimensionsGroup: ['sub_type'],
// calcTotals: {
// aggregation: Aggregation.SUM,
// },
// calcSubTotals: {
// aggregation: Aggregation.SUM,
// },
// showGrandTotals: true,
// showSubTotals: true,
// reverseLayout: true,
// reverseSubLayout: true,
// subTotalsDimensions: ['province'],
// },
},
}),
);
s2.render();
});
2 changes: 1 addition & 1 deletion packages/s2-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": false,
"name": "@antv/s2",
"version": "1.50.0",
"version": "1.49.2",
"main": "lib/index.js",
"unpkg": "dist/index.min.js",
"module": "esm/index.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO
84 changes: 47 additions & 37 deletions packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ export class PivotDataSet extends BaseDataSet {
field,
),
);
return uniq(
getDimensionsWithoutPathPre([...allCurrentFieldDimensionValues]),
).filter((v) => v !== 'undefined');
return filterUndefined(
uniq(getDimensionsWithoutPathPre([...allCurrentFieldDimensionValues])),
);
}

public getDimensionValues(field: string, query?: DataType): string[] {
Expand Down Expand Up @@ -622,6 +622,39 @@ export class PivotDataSet extends BaseDataSet {
return queryArray;
}

private getGroupTotalMultiData(totalRows, columns, query): DataType[] {
let result = [];
const rowTotalGroupQueries = this.getTotalGroupQueries(totalRows, query);
let totalGroupQueries = [];
for (const queryItem of rowTotalGroupQueries) {
totalGroupQueries = concat(
totalGroupQueries,
this.getTotalGroupQueries(columns as string[], queryItem),
);
}

for (const queryItem of totalGroupQueries) {
const rowDimensionValues = getQueryDimValues(totalRows, queryItem);
const colDimensionValues = getQueryDimValues(
columns as string[],
queryItem,
);
const path = getDataPath({
rowDimensionValues,
colDimensionValues,
careUndefined: true,
isFirstCreate: true,
rowFields: rowDimensionValues,
colFields: colDimensionValues,
rowPivotMeta: this.rowPivotMeta,
colPivotMeta: this.colPivotMeta,
});
const currentData = this.getCustomData(path);
result = concat(result, compact(customFlatten(currentData)));
}
return result;
}

public getMultiData(
query: DataType,
isTotals?: boolean,
Expand All @@ -639,45 +672,22 @@ export class PivotDataSet extends BaseDataSet {
// 如 [undefined , '杭州市' , undefined , 'number']
const existDimensionGroup = this.checkExistDimensionGroup(query);
let result = [];
const getDataPathByRowCol = (row: string[], col: string[]) =>
getDataPath({
rowDimensionValues: row,
colDimensionValues: col,
careUndefined: true,
isFirstCreate: true,
rowFields: rows,
colFields: columns as string[],
rowPivotMeta: this.rowPivotMeta,
colPivotMeta: this.colPivotMeta,
});
// 有中间维度汇总的分组场景,将有中间汇总值的 query 处理为一组合法 query 后查询数据
if (existDimensionGroup) {
const rowTotalGroupQueries = this.getTotalGroupQueries(totalRows, query);
let totalGroupQueries = [];
for (const queryItem of rowTotalGroupQueries) {
totalGroupQueries = concat(
totalGroupQueries,
this.getTotalGroupQueries(columns as string[], queryItem),
);
}

for (const queryItem of totalGroupQueries) {
const rowDimensionValues = getQueryDimValues(totalRows, queryItem);
const colDimensionValues = getQueryDimValues(
columns as string[],
queryItem,
);
const path = getDataPathByRowCol(
rowDimensionValues,
colDimensionValues,
);
const currentData = this.getCustomData(path);
result = concat(result, compact(customFlatten(currentData)));
}
result = this.getGroupTotalMultiData(totalRows, columns, query);
} else {
const rowDimensionValues = getQueryDimValues(totalRows, query);
const colDimensionValues = getQueryDimValues(columns as string[], query);
const path = getDataPathByRowCol(rowDimensionValues, colDimensionValues);
const path = getDataPath({
rowDimensionValues,
colDimensionValues,
careUndefined: true,
isFirstCreate: true,
rowFields: rowDimensionValues,
colFields: colDimensionValues,
rowPivotMeta: this.rowPivotMeta,
colPivotMeta: this.colPivotMeta,
});
const currentData = this.getCustomData(path);
result = compact(customFlatten(currentData));
if (isTotals) {
Expand Down
Loading

0 comments on commit 57db05a

Please sign in to comment.