Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo authored Apr 19, 2024
2 parents ece6e2a + ab0d0e7 commit 7d9686c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
36 changes: 35 additions & 1 deletion packages/s2-core/__tests__/spreadsheet/total-group-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Total Group Dimension Test', () => {
});

afterEach(() => {
s2?.destroy();
// s2?.destroy();
});

test(`should get correct layout with row total group dimension 'type'`, () => {
Expand Down Expand Up @@ -357,4 +357,38 @@ describe('Total Group Dimension Test', () => {
[VALUE_FIELD]: 9999,
});
});

// https://github.com/antvis/S2/issues/2661
test.each([
{ totalsGroupDimensions: [] },
{ totalsGroupDimensions: ['city'] },
])(
'should render correctly group totals layout if data is empty by %o',
(config) => {
s2 = new PivotSheet(container, dataCfg, {
...config,
showGrandTotals: true,
showSubTotals: true,
reverseLayout: true,
reverseSubLayout: true,
});

s2.setDataCfg({
fields: {
rows: ['type'],
columns: ['province', 'city'],
values: ['price', 'cost'],
},
data: [],
});
s2.render();

const { colNodes, colsHierarchy } = s2.facet.layoutResult;

expect(colsHierarchy.height).toEqual(90);
expect(colNodes).toHaveLength(4);
expect(colNodes.find((node) => node.value === '总计')).toBeFalsy();
expect(colNodes.find((node) => node.value === '小计')).toBeFalsy();
},
);
});
8 changes: 5 additions & 3 deletions packages/s2-core/src/facet/layout/build-gird-hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ const buildTotalGridHierarchy = (params: GridHeaderParams) => {

let query: Record<string, unknown> = {};
const totalsConfig = spreadsheet.getTotalsConfig(currentField);
const dimensionGroup = parentNode.isGrandTotals
? totalsConfig.totalsGroupDimensions
: totalsConfig.subTotalsGroupDimensions;
const defaultDimensionGroup = parentNode.isGrandTotals
? totalsConfig.totalsGroupDimensions || []
: totalsConfig.subTotalsGroupDimensions || [];
const dimensionGroup = !dataSet.isEmpty() ? defaultDimensionGroup : [];

if (dimensionGroup?.includes(currentField)) {
query = getDimsCondition(parentNode);
const dimValues = dataSet.getDimensionValues(currentField, query);
Expand Down
11 changes: 7 additions & 4 deletions packages/s2-core/src/facet/pivot-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,10 +675,12 @@ export class PivotFacet extends FrozenFacet {
const { rows, columns } = dataSet.fields;
const fields = isRowHeader ? rows : columns;
const totalConfig = isRowHeader ? totals.row : totals.col;
const dimensionGroup = isSubTotal
const defaultDimensionGroup = isSubTotal
? totalConfig.subTotalsGroupDimensions || []
: totalConfig.totalsGroupDimensions || [];
const dimensionGroup = !dataSet.isEmpty() ? defaultDimensionGroup : [];
const multipleMap: number[] = Array.from({ length: maxLevel + 1 }, () => 1);

for (let level = maxLevel; level > 0; level--) {
const currentField = fields[level] as string;
// 若不符合【分组维度包含此维度】或【者指标维度下非单指标维度】,此表头单元格为空,将宽高合并到上级单元格
Expand Down Expand Up @@ -716,9 +718,10 @@ export class PivotFacet extends FrozenFacet {
}
let res = 0;
for (let i = 0; i < multiple; i++) {
res += hierarchy.sampleNodesForAllLevels.find(
(sampleNode) => sampleNode.level === node.level + i,
)[key];
res +=
hierarchy.sampleNodesForAllLevels.find(
(sampleNode) => sampleNode.level === node.level + i,
)?.[key] || 0;
}
node[key] = res;
});
Expand Down

0 comments on commit 7d9686c

Please sign in to comment.