diff --git a/packages/s2-core/__tests__/data/data-issue-292.json b/packages/s2-core/__tests__/data/data-issue-292.json index 8a387e8391..98ccfbf083 100644 --- a/packages/s2-core/__tests__/data/data-issue-292.json +++ b/packages/s2-core/__tests__/data/data-issue-292.json @@ -10,12 +10,7 @@ "province": "浙江", "city": "义乌", "type": "笔", - "cost": "2" - }, - { - "province": "浙江", - "city": "义乌", - "type": "笔", + "cost": "2", "price": "8" }, { diff --git a/packages/s2-core/__tests__/unit/data-process/pivot-spec.tsx b/packages/s2-core/__tests__/unit/data-process/pivot-spec.tsx index 0af2b4a6a0..601b997755 100644 --- a/packages/s2-core/__tests__/unit/data-process/pivot-spec.tsx +++ b/packages/s2-core/__tests__/unit/data-process/pivot-spec.tsx @@ -48,35 +48,35 @@ describe('Pivot Table Core Data Process', () => { data.length, ); - expect(get(indexesData, [prefix, 1, 1, 1, 1])).toEqual({ + expect(get(indexesData, [prefix, 1, 1, 1, 1, 1])).toEqual({ province: '浙江省', city: '杭州市', type: '家具', sub_type: '桌子', number: 7789, }); // 左上角 - expect(get(indexesData, [prefix, 1, 1, 2, 2])).toEqual({ + expect(get(indexesData, [prefix, 1, 1, 2, 2, 1])).toEqual({ province: '浙江省', city: '杭州市', type: '办公用品', sub_type: '纸张', number: 1343, }); // 右上角 - expect(get(indexesData, [prefix, 2, 4, 1, 1])).toEqual({ + expect(get(indexesData, [prefix, 2, 4, 1, 1, 1])).toEqual({ province: '四川省', city: '乐山市', type: '家具', sub_type: '桌子', number: 2330, }); // 左下角 - expect(get(indexesData, [prefix, 2, 4, 2, 2])).toEqual({ + expect(get(indexesData, [prefix, 2, 4, 2, 2, 1])).toEqual({ province: '四川省', city: '乐山市', type: '办公用品', sub_type: '纸张', number: 352, }); // 右下角 - expect(get(indexesData, [prefix, 1, 4, 2, 1])).toEqual({ + expect(get(indexesData, [prefix, 1, 4, 2, 1, 1])).toEqual({ province: '浙江省', city: '舟山市', type: '办公用品', diff --git a/packages/s2-core/__tests__/unit/data-set/custom-tree-data-set-spec.ts b/packages/s2-core/__tests__/unit/data-set/custom-tree-data-set-spec.ts index 99e6b7e8da..11faa4c7a3 100644 --- a/packages/s2-core/__tests__/unit/data-set/custom-tree-data-set-spec.ts +++ b/packages/s2-core/__tests__/unit/data-set/custom-tree-data-set-spec.ts @@ -51,7 +51,7 @@ describe('Custom Tree Dataset Test', () => { test('should get empty row pivot meta', () => { const rowPivotMeta = dataSet.rowPivotMeta; - expect([...rowPivotMeta.keys()]).toEqual([]); + expect([...rowPivotMeta.keys()]).toEqual(values); }); test('should get correct col pivot meta', () => { @@ -68,7 +68,7 @@ describe('Custom Tree Dataset Test', () => { test('should get correct indexesData', () => { const prefix = 'type[&]sub_type'; const indexesData = dataSet.indexesData; - expect(get(indexesData, [prefix, 1, 1])).toEqual({ + expect(get(indexesData, [prefix, 1, 1, 1])).toEqual({ type: '家具', sub_type: '桌子', 'measure-a': 1, @@ -78,7 +78,7 @@ describe('Custom Tree Dataset Test', () => { 'measure-e': 5, 'measure-f': 6, }); - expect(get(indexesData, [prefix, 1, 2])).toEqual({ + expect(get(indexesData, [prefix, 1, 1, 2])).toEqual({ type: '家具', sub_type: '椅子', 'measure-a': 11, diff --git a/packages/s2-core/__tests__/unit/data-set/pivot-data-set-row-value-spec.ts b/packages/s2-core/__tests__/unit/data-set/pivot-data-set-row-value-spec.ts index 43e9c12308..82fbaf2871 100644 --- a/packages/s2-core/__tests__/unit/data-set/pivot-data-set-row-value-spec.ts +++ b/packages/s2-core/__tests__/unit/data-set/pivot-data-set-row-value-spec.ts @@ -93,21 +93,21 @@ describe('Pivot Mode Test When Value In Row', () => { test('should get correct indexesData', () => { const prefix = 'province[&]city[&]type[&]sub_type'; const indexesData = dataSet.indexesData; - expect(get(indexesData, [prefix, 1, 1, 1, 1])).toEqual({ + expect(get(indexesData, [prefix, 1, 1, 1, 1, 1])).toEqual({ province: '浙江省', city: '杭州市', type: '家具', sub_type: '桌子', number: 7789, }); - expect(get(indexesData, [prefix, 1, 2, 1, 2])).toEqual({ + expect(get(indexesData, [prefix, 1, 2, 1, 1, 2])).toEqual({ province: '浙江省', city: '绍兴市', type: '家具', sub_type: '沙发', number: 632, }); - expect(get(indexesData, [prefix, 2, 1, 1, 2])).toEqual({ + expect(get(indexesData, [prefix, 2, 1, 1, 1, 2])).toEqual({ province: '四川省', city: '成都市', type: '家具', @@ -121,6 +121,7 @@ describe('Pivot Mode Test When Value In Row', () => { expect([...keys(sortedDimensionValues)]).toEqual([ 'province', 'city', + EXTRA_FIELD, 'type', 'sub_type', ]); diff --git a/packages/s2-core/__tests__/unit/data-set/pivot-data-set-spec.ts b/packages/s2-core/__tests__/unit/data-set/pivot-data-set-spec.ts index b11c74f165..00d54a0dda 100644 --- a/packages/s2-core/__tests__/unit/data-set/pivot-data-set-spec.ts +++ b/packages/s2-core/__tests__/unit/data-set/pivot-data-set-spec.ts @@ -97,21 +97,21 @@ describe('Pivot Dataset Test', () => { test('should get correct indexesData', () => { const indexesData = dataSet.indexesData; const prefix = 'province[&]city[&]type[&]sub_type'; - expect(get(indexesData, [prefix, 1, 1, 1, 1])).toEqual({ + expect(get(indexesData, [prefix, 1, 1, 1, 1, 1])).toEqual({ province: '浙江省', city: '杭州市', type: '家具', sub_type: '桌子', number: 7789, }); - expect(get(indexesData, [prefix, 1, 2, 2, 1])).toEqual({ + expect(get(indexesData, [prefix, 1, 2, 2, 1, 1])).toEqual({ province: '浙江省', city: '绍兴市', type: '办公用品', sub_type: '笔', number: 1304, }); - expect(get(indexesData, [prefix, 2, 1, 1, 2])).toEqual({ + expect(get(indexesData, [prefix, 2, 1, 1, 2, 1])).toEqual({ province: '四川省', city: '成都市', type: '家具', @@ -127,6 +127,7 @@ describe('Pivot Dataset Test', () => { 'city', 'type', 'sub_type', + EXTRA_FIELD, ]); expect( getDimensionsWithoutPathPre(sortedDimensionValues.province), diff --git a/packages/s2-core/__tests__/unit/data-set/pivot-data-set-total-spec.ts b/packages/s2-core/__tests__/unit/data-set/pivot-data-set-total-spec.ts index 86c198be73..029dd0cb47 100644 --- a/packages/s2-core/__tests__/unit/data-set/pivot-data-set-total-spec.ts +++ b/packages/s2-core/__tests__/unit/data-set/pivot-data-set-total-spec.ts @@ -84,7 +84,7 @@ describe('Pivot Dataset Total Test', () => { test('should get correct indexesData', () => { const indexesData = dataSet.indexesData; expect( - get(indexesData, ['province[&]city[&]type[&]sub_type', 1, 1, 0, 0]), + get(indexesData, ['province[&]city[&]type[&]sub_type', 1, 1, 0, 0, 1]), ).toEqual({ province: '浙江省', city: '杭州市', @@ -92,7 +92,7 @@ describe('Pivot Dataset Total Test', () => { }); expect( - get(indexesData, ['province[&]city[&]type[&]sub_type', 1, 1, 2, 0]), + get(indexesData, ['province[&]city[&]type[&]sub_type', 1, 1, 2, 0, 1]), ).toEqual({ province: '浙江省', city: '杭州市', @@ -100,14 +100,14 @@ describe('Pivot Dataset Total Test', () => { number: 2288, }); expect( - get(indexesData, ['province[&]city[&]type[&]sub_type', 2, 0, 2, 0]), + get(indexesData, ['province[&]city[&]type[&]sub_type', 2, 0, 2, 0, 1]), ).toEqual({ province: '四川省', type: '办公用品', number: 18479, }); expect( - get(indexesData, ['province[&]city[&]type[&]sub_type', 0, 0, 0, 0]), + get(indexesData, ['province[&]city[&]type[&]sub_type', 0, 0, 0, 0, 1]), ).toEqual({ number: 78868, }); @@ -120,6 +120,7 @@ describe('Pivot Dataset Total Test', () => { 'city', 'type', 'sub_type', + EXTRA_FIELD, ]); expect( getDimensionsWithoutPathPre(sortedDimensionValues.province), @@ -153,6 +154,17 @@ describe('Pivot Dataset Total Test', () => { TOTAL_VALUE, TOTAL_VALUE, ]); + expect( + getDimensionsWithoutPathPre(sortedDimensionValues[EXTRA_FIELD]), + ).toEqual([ + 'number', + 'number', + 'number', + 'number', + 'number', + 'number', + 'number', + ]); }); }); diff --git a/packages/s2-core/__tests__/unit/facet/util.ts b/packages/s2-core/__tests__/unit/facet/util.ts index 1a1713235b..49b4ae4878 100644 --- a/packages/s2-core/__tests__/unit/facet/util.ts +++ b/packages/s2-core/__tests__/unit/facet/util.ts @@ -19,5 +19,6 @@ export function getMockPivotMeta() { sortedDimensionValues, rowPivotMeta: rawRowPivotMeta, colPivotMeta: rawColPivotMeta, + valueInCols: true, }); } diff --git a/packages/s2-core/__tests__/unit/utils/dataset/pivot-dataset-spec.ts b/packages/s2-core/__tests__/unit/utils/dataset/pivot-dataset-spec.ts index e6802ce53a..4d441b98c7 100644 --- a/packages/s2-core/__tests__/unit/utils/dataset/pivot-dataset-spec.ts +++ b/packages/s2-core/__tests__/unit/utils/dataset/pivot-dataset-spec.ts @@ -48,6 +48,7 @@ describe('PivotDataSet util test', () => { sortedDimensionValues, rowPivotMeta, colPivotMeta, + valueInCols: true, }); expect(result.paths).toHaveLength(32); expect(get(result.indexesData, result.paths[0])).toEqual({ @@ -107,6 +108,7 @@ describe('PivotDataSet util test', () => { isFirstCreate: true, rowFields: rows, colFields: columns, + prefix, }); expect(result).toEqual([prefix, 1, 1, 1, 1]); }); diff --git a/packages/s2-core/src/data-set/pivot-data-set.ts b/packages/s2-core/src/data-set/pivot-data-set.ts index 36fff05c68..5415828708 100644 --- a/packages/s2-core/src/data-set/pivot-data-set.ts +++ b/packages/s2-core/src/data-set/pivot-data-set.ts @@ -584,9 +584,10 @@ export class PivotDataSet extends BaseDataSet { const extraFields = this.getQueryExtraFields(query); - return flatMap(all.filter(Boolean), (item) => - DataHandler.createProxyDataList(item, extraFields), - ); + // 多个 extra field 有时对应的同一个对象,需要进行去重 + return flatMap(uniq(all), (item) => { + return item ? DataHandler.createProxyDataList(item, extraFields) : []; + }); } public getFieldFormatter(field: string, cellMeta?: ViewMeta): Formatter {