diff --git a/packages/s2-core/__tests__/bugs/issue-860-spec.ts b/packages/s2-core/__tests__/bugs/issue-860-spec.ts new file mode 100644 index 0000000000..1a3fd6d9b5 --- /dev/null +++ b/packages/s2-core/__tests__/bugs/issue-860-spec.ts @@ -0,0 +1,30 @@ +/** + * @description spec for issue #860 + * https://github.com/antvis/S2/issues/860 + * Column should not be formatted + * + */ +import { getContainer } from 'tests/util/helpers'; +import dataCfg from '../data/data-issue-860.json'; +import { PivotSheet, SpreadSheet } from '@/sheet-type'; +import { S2Options } from '@/common/interface'; + +const s2options: S2Options = { + width: 600, + height: 400, + frozenRowHeader: false, +}; + +describe('Column Formatter Tests', () => { + let s2: SpreadSheet; + + beforeEach(() => { + s2 = new PivotSheet(getContainer(), dataCfg, s2options); + s2.render(); + }); + test('should get correct row hierarchy with empty row node', () => { + const layoutResult = s2.facet.layoutResult; + expect(layoutResult.rowNodes).toHaveLength(8); + expect(layoutResult.rowLeafNodes).toHaveLength(5); + }); +}); diff --git a/packages/s2-core/__tests__/data/data-issue-860.json b/packages/s2-core/__tests__/data/data-issue-860.json new file mode 100644 index 0000000000..29bbc3f4d7 --- /dev/null +++ b/packages/s2-core/__tests__/data/data-issue-860.json @@ -0,0 +1,74 @@ +{ + "fields": { + "rows": ["date", "city", "type"], + "columns": [], + "values": ["a_sum", "b_sum"], + "valueInCols": true + }, + "meta": [ + { + "field": "date", + "name": "日期" + }, + { + "field": "city", + "name": "城市" + }, + { + "field": "type", + "name": "类型" + }, + { + "field": "a_sum", + "name": "A类求和" + }, + { + "field": "b_sum", + "name": "B类求和" + } + ], + "data": [ + { + "date": "2021-12-01", + "city": "A市", + "type": "电子", + "a_sum": 6 + }, + { + "date": "2021-12-01", + "city": "A市", + "type": "电子", + "b_sum": 6 + }, + { + "date": "2021-12-01", + "city": "A市", + "type": "", + "b_sum": 6 + }, + { + "date": "2021-12-01", + "city": "A市", + "type": "undefined", + "b_sum": 6 + }, + { + "date": "2021-12-01", + "city": "A市", + "type": null, + "b_sum": 6 + }, + { + "date": "2021-12-01", + "city": "B市", + "type": "日用", + "a_sum": 1 + }, + { + "date": "2021-12-01", + "city": "B市", + "type": "", + "b_sum": 1 + } + ] +} diff --git a/packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts b/packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts index c8899ffa2c..5049933e11 100644 --- a/packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts +++ b/packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts @@ -706,6 +706,27 @@ describe('PivotSheet Tests', () => { expect(sheet.dataCfg.fields.valueInCols).toBeTruthy(); }); + it('should render row nodes if rows fields contain empty string value', () => { + const layoutDataCfg: S2DataConfig = { + fields: { + rows: ['row'], + }, + data: [ + { + row: 'a', + }, + { + row: '', + }, + ], + } as S2DataConfig; + const sheet = new PivotSheet(getContainer(), layoutDataCfg, s2Options); + sheet.render(); + + const { layoutResult } = sheet.facet; + expect(layoutResult.rowNodes).toHaveLength(2); + }); + it('should only render value nodes in column if rows & columns fields is empty', () => { const layoutDataCfg: S2DataConfig = customMerge(dataCfg, { fields: { diff --git a/packages/s2-core/src/facet/layout/build-gird-hierarchy.ts b/packages/s2-core/src/facet/layout/build-gird-hierarchy.ts index 2ea8038db4..1450016cb2 100644 --- a/packages/s2-core/src/facet/layout/build-gird-hierarchy.ts +++ b/packages/s2-core/src/facet/layout/build-gird-hierarchy.ts @@ -1,4 +1,4 @@ -import { compact, isEmpty } from 'lodash'; +import { filter, isEmpty, isUndefined } from 'lodash'; import { FieldValue, GridHeaderParams } from '@/facet/layout/interface'; import { TotalMeasure } from '@/facet/layout/total-measure'; import { layoutArrange } from '@/facet/layout/layout-hooks'; @@ -39,6 +39,7 @@ export const buildGridHierarchy = (params: GridHeaderParams) => { } = params; const index = fields.indexOf(currentField); + const { dataSet, values, spreadsheet } = facetCfg; const fieldValues: FieldValue[] = []; @@ -72,7 +73,7 @@ export const buildGridHierarchy = (params: GridHeaderParams) => { if (currentField === EXTRA_FIELD) { fieldValues.push(...dataSet.fields?.values); } else { - fieldValues.push(fieldName || currentField); + fieldValues.push(fieldName); } } // hide measure in columns @@ -86,10 +87,16 @@ export const buildGridHierarchy = (params: GridHeaderParams) => { spreadsheet, }); } + + const omitUndefinedFieldValues = filter( + fieldValues, + (value) => !isUndefined(value), + ); + generateHeaderNodes({ currentField, fields, - fieldValues: compact(fieldValues), + fieldValues: omitUndefinedFieldValues, facetCfg, hierarchy, parentNode,