From 24437622e1b259288bf7f8f3505837088a6e1b9d Mon Sep 17 00:00:00 2001 From: NoobNot <56724970+NoobNotN@users.noreply.github.com> Date: Fri, 20 Oct 2023 10:27:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=88=86=E7=BB=84?= =?UTF-8?q?=E6=B1=87=E6=80=BB=E6=97=B6=EF=BC=8C=E6=8C=89=E6=B1=87=E6=80=BB?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E8=8E=B7=E5=8F=96=E6=8E=92=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=B8=BA=E7=A9=BA=20(#2370)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 修复指标不在最后一级配置与分组汇总同时使用时,获取数据为空 * fix: 修复指标不在最后一级配置与分组汇总同时使用时,获取数据为空 * test: 增加分组汇总时按汇总排序单测 * test: 增加分组汇总时按汇总排序单测 * chore: 优化代码可读,getMultiData 使用对象传参 * chore: 优化代码可读,getMultiData 使用对象传参 * chore: 优化代码可读,getMultiData 使用对象传参 * chore: getMultiData 使用传参格式改回 * chore: 改名为 includeTotalData 参数名以及文档 * docs: 加个空格 --------- Co-authored-by: JuZe --- .../__snapshots__/sort-action-spec.tsx.snap | 20 +++++++ .../__tests__/unit/utils/sort-action-spec.tsx | 52 +++++++++++++++++++ .../s2-core/src/data-set/base-data-set.ts | 2 + .../s2-core/src/data-set/pivot-data-set.ts | 12 ++--- packages/s2-core/src/utils/sort-action.ts | 8 ++- .../docs/api/basic-class/base-data-set.en.md | 38 +++++++------- .../docs/api/basic-class/base-data-set.zh.md | 2 +- 7 files changed, 107 insertions(+), 27 deletions(-) create mode 100644 packages/s2-core/__tests__/unit/utils/__snapshots__/sort-action-spec.tsx.snap diff --git a/packages/s2-core/__tests__/unit/utils/__snapshots__/sort-action-spec.tsx.snap b/packages/s2-core/__tests__/unit/utils/__snapshots__/sort-action-spec.tsx.snap new file mode 100644 index 0000000000..a27042ad8a --- /dev/null +++ b/packages/s2-core/__tests__/unit/utils/__snapshots__/sort-action-spec.tsx.snap @@ -0,0 +1,20 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`GetSortByMeasureValues Total Fallback Tests should sort by col total whit group 1`] = ` +Array [ + Object { + "$$extra$$": "price", + "$$value$$": "666", + "city": "杭州", + "price": "666", + "type": "笔", + }, + Object { + "$$extra$$": "price", + "$$value$$": "999", + "city": "杭州", + "price": "999", + "type": "纸张", + }, +] +`; diff --git a/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx b/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx index 5b90bdb267..e9989b8847 100644 --- a/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx +++ b/packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx @@ -714,4 +714,56 @@ describe('GetSortByMeasureValues Total Fallback Tests', () => { }, ]); }); + + test('should sort by col total whit group', () => { + const currentOptions = { + totals: { + col: { + totalsGroupDimensions: ['city'], + showGrandTotals: true, + }, + }, + } as S2Options; + + const dataConfig = { + ...sortData, + data: [ + ...sortData.data, + { + city: '杭州', + type: '纸张', + price: '999', + }, + { + city: '杭州', + type: '笔', + price: '666', + }, + ], + fields: { + rows: ['type'], + columns: ['province', 'city'], + values: ['price'], + }, + }; + sheet = new PivotSheet(getContainer(), dataConfig, currentOptions); + sheet.render(); + // 根据列(类别)的总和排序 + const sortParam: SortParam = { + sortFieldId: 'type', + sortByMeasure: TOTAL_VALUE, + sortMethod: 'desc', + query: { + [EXTRA_FIELD]: 'price', + city: '杭州', + }, + }; + + const params: SortActionParams = { + dataSet: sheet.dataSet, + sortParam, + }; + const measureValues = getSortByMeasureValues(params); + expect(measureValues).toMatchSnapshot(); + }); }); diff --git a/packages/s2-core/src/data-set/base-data-set.ts b/packages/s2-core/src/data-set/base-data-set.ts index 92ffb6ae8e..3697f895e3 100644 --- a/packages/s2-core/src/data-set/base-data-set.ts +++ b/packages/s2-core/src/data-set/base-data-set.ts @@ -206,12 +206,14 @@ export abstract class BaseDataSet { * @param isTotals * @param isRow * @param drillDownFields + * @param includeTotalData 用于标记是否包含汇总数据,例如在排序功能中需要汇总数据,在计算汇总值中只取明细数据 */ public abstract getMultiData( query: DataType, isTotals?: boolean, isRow?: boolean, drillDownFields?: string[], + includeTotalData?: boolean, ): DataType[]; public moreThanOneValue() { 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 545d9eebf8..4f4766272c 100644 --- a/packages/s2-core/src/data-set/pivot-data-set.ts +++ b/packages/s2-core/src/data-set/pivot-data-set.ts @@ -577,8 +577,8 @@ export class PivotDataSet extends BaseDataSet { getTotalGroupQueries(dimensions: string[], originQuery: DataType) { let queries = [originQuery]; let existDimensionGroupKey = null; - for (let i = dimensions.length; i > 0; i--) { - const key = dimensions[i - 1]; + for (let i = dimensions.length - 1; i >= 0; i--) { + const key = dimensions[i]; if (keys(originQuery).includes(key)) { if (key !== EXTRA_FIELD) { existDimensionGroupKey = key; @@ -587,8 +587,6 @@ export class PivotDataSet extends BaseDataSet { const allCurrentFieldDimensionValues = this.sortedDimensionValues[existDimensionGroupKey]; let res = []; - const arrayLength = - allCurrentFieldDimensionValues[0].split(ID_SEPARATOR).length; for (const query of queries) { const resKeys = []; for (const dimValue of allCurrentFieldDimensionValues) { @@ -601,7 +599,7 @@ export class PivotDataSet extends BaseDataSet { }) ) { const arrTypeValue = dimValue.split(ID_SEPARATOR); - const currentKey = arrTypeValue[arrayLength - 2]; + const currentKey = arrTypeValue[i]; if (currentKey !== 'undefined') { resKeys.push(currentKey); } @@ -662,6 +660,7 @@ export class PivotDataSet extends BaseDataSet { isTotals?: boolean, isRow?: boolean, drillDownFields?: string[], + includeTotalData?: boolean, ): DataType[] { if (isEmpty(query)) { return compact(customFlattenDeep(this.indexesData)); @@ -672,7 +671,8 @@ export class PivotDataSet extends BaseDataSet { : rows; // existDimensionGroup:当 undefined 维度后面有非 undefined,为维度分组场景,将非 undefined 维度前的维度填充为所有可能的维度值。 // 如 [undefined , '杭州市' , undefined , 'number'] - const existDimensionGroup = this.checkExistDimensionGroup(query); + const existDimensionGroup = + !includeTotalData && this.checkExistDimensionGroup(query); let result = []; if (existDimensionGroup) { result = this.getGroupTotalMultiData(totalRows, query); diff --git a/packages/s2-core/src/utils/sort-action.ts b/packages/s2-core/src/utils/sort-action.ts index 921df04fb0..79bdf3e52d 100644 --- a/packages/s2-core/src/utils/sort-action.ts +++ b/packages/s2-core/src/utils/sort-action.ts @@ -252,7 +252,13 @@ export const getSortByMeasureValues = ( const { dataSet, sortParam, originValues } = params; const { fields } = dataSet; const { sortByMeasure, query, sortFieldId } = sortParam; - const dataList = dataSet.getMultiData(query); // 按 query 查出所有数据 + const dataList = dataSet.getMultiData( + query, + undefined, + undefined, + undefined, + true, + ); // 按 query 查出所有数据 const columns = getLeafColumnsWithKey(fields.columns); /** * 按明细数据 diff --git a/s2-site/docs/api/basic-class/base-data-set.en.md b/s2-site/docs/api/basic-class/base-data-set.en.md index 2f3b37a108..a6cba2cc77 100644 --- a/s2-site/docs/api/basic-class/base-data-set.en.md +++ b/s2-site/docs/api/basic-class/base-data-set.en.md @@ -8,25 +8,25 @@ Function description: tabular data set. [details](https://github.com/antvis/S2/b s2.dataSet.xx() ``` -| parameter | illustrate | type | Version | -| ------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------- | -| fields | field information | () => [Fields](/docs/api/general/S2DataConfig#fields) | | -| meta | Field meta information, including field name, formatting, etc. | () => [Meta\[\]](/docs/api/general/S2DataConfig#meta) | | -| originData | Raw data | () => [DataType\[\]](#datatype) | | -| totalData | summary data | () => [DataType\[\]](#datatype) | | -| indexesData | multidimensional index data | () => [DataType\[\]](#datatype) | | -| sortParams | sort configuration | () => [SortParams](/docs/api/general/S2DataConfig#sortparams) | | -| spreadsheet | Form example | () => [SpreadSheet](/docs/api/basic-class/spreadsheet) | | -| getFieldMeta | Get field metadata information | (field: string, meta?: [Meta\[\]](/docs/api/general/S2DataConfig#meta) ) => [Meta](/docs/api/general/S2DataConfig#meta) | | -| getFieldName | get field name | `() => string` | | -| getFieldFormatter | Get the field formatting function | `() => (v: string) => unknown` | | -| getFieldDescription | Get field description | `() => string` | | -| setDataCfg | Set data configuration | `(dataCfg: T extends true ?` [`S2DataConfig`](/docs/api/general/S2DataConfig) `: Partial<`[`S2DataConfig`](/docs/api/general/S2DataConfig)`>, reset?: T) => void` | The `reset` parameter needs to be used in `@antv/s2-v1.34.0` version | -| getDisplayDataSet | Get the currently displayed dataset | () => [DataType\[\]](#datatype) | | -| getDimensionValues | get dimension value | (filed: string, query?: [DataType](#datatype) ) => string\[] | | -| getCellData | Get a single cell data | (params: [CellDataParams](#celldataparams) ) => [DataType\[\]](#datatype) | | -| getMultiData | Get bulk cell data | (query: [DataType](#datatype) , isTotals?: boolean, isRow?: boolean, drillDownFields?: string\[]) => [DataType\[\]](#datatype) | | -| moreThanOneValue | Is there more than 1 value | () => [ViewMeta](#viewmeta) | | +| parameter | illustrate | type | Version | +| ------------------- | -------------------------------------------------------------- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -------------------------------------------------------------------- | +| fields | field information | () => [Fields](/docs/api/general/S2DataConfig#fields) | | +| meta | Field meta information, including field name, formatting, etc. | () => [Meta\[\]](/docs/api/general/S2DataConfig#meta) | | +| originData | Raw data | () => [DataType\[\]](#datatype) | | +| totalData | summary data | () => [DataType\[\]](#datatype) | | +| indexesData | multidimensional index data | () => [DataType\[\]](#datatype) | | +| sortParams | sort configuration | () => [SortParams](/docs/api/general/S2DataConfig#sortparams) | | +| spreadsheet | Form example | () => [SpreadSheet](/docs/api/basic-class/spreadsheet) | | +| getFieldMeta | Get field metadata information | (field: string, meta?: [Meta\[\]](/docs/api/general/S2DataConfig#meta) ) => [Meta](/docs/api/general/S2DataConfig#meta) | | +| getFieldName | get field name | `() => string` | | +| getFieldFormatter | Get the field formatting function | `() => (v: string) => unknown` | | +| getFieldDescription | Get field description | `() => string` | | +| setDataCfg | Set data configuration | `(dataCfg: T extends true ?` [`S2DataConfig`](/docs/api/general/S2DataConfig) `: Partial<`[`S2DataConfig`](/docs/api/general/S2DataConfig)`>, reset?: T) => void` | The `reset` parameter needs to be used in `@antv/s2-v1.34.0` version | +| getDisplayDataSet | Get the currently displayed dataset | () => [DataType\[\]](#datatype) | | +| getDimensionValues | get dimension value | (filed: string, query?: [DataType](#datatype) ) => string\[] | | +| getCellData | Get a single cell data | (params: [CellDataParams](#celldataparams) ) => [DataType\[\]](#datatype) | | +| getMultiData | Get bulk cell data | (query: [DataType](#datatype) , isTotals?: boolean, isRow?: boolean, drillDownFields?: string\[], includeTotalData:boolean) => [DataType\[\]](#datatype) | | +| moreThanOneValue | Is there more than 1 value | () => [ViewMeta](#viewmeta) | | ### DataType diff --git a/s2-site/docs/api/basic-class/base-data-set.zh.md b/s2-site/docs/api/basic-class/base-data-set.zh.md index b259e92ced..115356e46a 100644 --- a/s2-site/docs/api/basic-class/base-data-set.zh.md +++ b/s2-site/docs/api/basic-class/base-data-set.zh.md @@ -26,7 +26,7 @@ s2.dataSet.getFieldName('type') | getDisplayDataSet | 获取当前显示的数据集 | () => [DataType[]](#datatype) | | | getDimensionValues | 获取维值 | (filed: string, query?: [DataType](#datatype) ) => string[] | | | getCellData | 获取单个的单元格数据 | (params: [CellDataParams](#celldataparams)) => [DataType[]](#datatype) | | -| getMultiData | 获取批量的单元格数据 | (query: [DataType](#datatype), isTotals?: boolean, isRow?: boolean, drillDownFields?: string[]) => [DataType[]](#datatype) | | +| getMultiData | 获取批量的单元格数据 | (query: [DataType](#datatype), isTotals?: boolean, isRow?: boolean, drillDownFields?: string[], includeTotalData:boolean) => [DataType[]](#datatype) | | | moreThanOneValue | 是否超过 1 个数值 | () => [ViewMeta](#viewmeta) | | | isEmpty | 是否为空数据集 | () => `boolean` | `@antv/s2-v1.51.1` |