Skip to content

Commit

Permalink
fix: 修复透视表数据为空时,行列交叉单元格缺失的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo committed Dec 6, 2023
1 parent 8a67d4b commit 4a4cd59
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
64 changes: 64 additions & 0 deletions packages/s2-core/__tests__/spreadsheet/empty-dataset-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { getContainer } from 'tests/util/helpers';
import { PivotSheet, TableSheet } from '@/sheet-type';
import type { S2Options } from '@/common/interface/s2Options';

const s2Options: S2Options = {
width: 400,
height: 400,
hierarchyType: 'grid',
};

describe('Empty Dataset Structure Tests', () => {
test('should generate placeholder for pivot mode with single dimension', () => {
const container = getContainer();

const s2DataCfg = {
fields: {
rows: ['province', 'city'],
columns: ['type'],
values: ['price'],
valueInCols: true,
},
data: [],
};
const s2 = new PivotSheet(container, s2DataCfg, s2Options);
s2.render();

// @ts-ignore
expect(s2.facet.panelScrollGroupIndexes).toEqual([0, 0, 0, 0]);
});

test('should generate placeholder for pivot mode with two dimensions', () => {
const container = getContainer();

const s2DataCfg = {
fields: {
rows: ['province', 'city'],
columns: ['type'],
values: ['price', 'cost'],
valueInCols: true,
},
data: [],
};
const s2 = new PivotSheet(container, s2DataCfg, s2Options);
s2.render();
// @ts-ignore
expect(s2.facet.panelScrollGroupIndexes).toEqual([0, 1, 0, 0]);
});

test(`shouldn't generate placeholder for table mode`, () => {
const container = getContainer();

const s2DataCfg = {
fields: {
columns: ['province', 'city', 'type', 'price', 'cost'],
},
data: [],
};
const s2 = new TableSheet(container, s2DataCfg, s2Options);
s2.render();
// @ts-ignore
expect(s2.facet.panelScrollGroupIndexes).toEqual([]);
});
});
6 changes: 6 additions & 0 deletions packages/s2-core/src/data-set/base-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from '../utils/condition/state-controller';
import type { CellMeta, RowData } from '../common';
import { generateExtraFieldMeta } from '../utils/dataset/pivot-data-set';
import type { Indexes } from '../utils/indexes';
import type { CellDataParams, DataType } from './index';

export abstract class BaseDataSet {
Expand Down Expand Up @@ -122,6 +123,11 @@ export abstract class BaseDataSet {
return isEmpty(this.getDisplayDataSet());
}

// https://github.com/antvis/S2/issues/2255
public getEmptyViewIndexes(): Indexes {
return [];
}

public getValueRangeByField(field: string): ValueRange {
const cacheRange = getValueRangeState(this.spreadsheet, field);
if (cacheRange) {
Expand Down
22 changes: 11 additions & 11 deletions packages/s2-core/src/facet/frozen-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ export abstract class FrozenFacet extends BaseFacet {
}
}

// https://github.com/antvis/S2/issues/2255
const indexes = this.spreadsheet.dataSet?.isEmpty?.()
? ([] as unknown as Indexes)
: calculateInViewIndexes(
scrollX,
scrollY,
this.viewCellWidths,
this.viewCellHeights,
finalViewport,
this.getRealScrollX(this.cornerBBox.width),
);
const indexes =
this.spreadsheet.isTableMode() && this.spreadsheet.dataSet?.isEmpty?.()
? this.spreadsheet.dataSet.getEmptyViewIndexes()
: calculateInViewIndexes(
scrollX,
scrollY,
this.viewCellWidths,
this.viewCellHeights,
finalViewport,
this.getRealScrollX(this.cornerBBox.width),
);

this.panelScrollGroupIndexes = indexes;

Expand Down

0 comments on commit 4a4cd59

Please sign in to comment.