Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复分组汇总时,按汇总排序获取排序数据为空 #2370

Merged
merged 11 commits into from
Oct 20, 2023
Original file line number Diff line number Diff line change
@@ -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": "纸张",
},
]
`;
52 changes: 52 additions & 0 deletions packages/s2-core/__tests__/unit/utils/sort-action-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
2 changes: 2 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 @@ -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,
lijinke666 marked this conversation as resolved.
Show resolved Hide resolved
): DataType[];

public moreThanOneValue() {
Expand Down
12 changes: 6 additions & 6 deletions packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,25 +570,23 @@
* 补足分组汇总场景的前置 undefined
* {undefined,'可乐','undefined','price'}
* => [
* {'可口公司','可乐','undefined','price'},

Check warning on line 573 in packages/s2-core/src/data-set/pivot-data-set.ts

View check run for this annotation

Codecov / codecov/patch

packages/s2-core/src/data-set/pivot-data-set.ts#L573

Added line #L573 was not covered by tests
* {'百事公司','可乐','undefined','price'},
* ]
*/
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;
}
} else if (existDimensionGroupKey) {
const allCurrentFieldDimensionValues =

Check warning on line 587 in packages/s2-core/src/data-set/pivot-data-set.ts

View check run for this annotation

Codecov / codecov/patch

packages/s2-core/src/data-set/pivot-data-set.ts#L587

Added line #L587 was not covered by tests
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) {
Expand All @@ -601,7 +599,7 @@
})
) {
const arrTypeValue = dimValue.split(ID_SEPARATOR);
const currentKey = arrTypeValue[arrayLength - 2];
const currentKey = arrTypeValue[i];
if (currentKey !== 'undefined') {
resKeys.push(currentKey);
}
Expand Down Expand Up @@ -662,6 +660,7 @@
isTotals?: boolean,
isRow?: boolean,
drillDownFields?: string[],
includeTotalData?: boolean,
): DataType[] {
if (isEmpty(query)) {
return compact(customFlattenDeep(this.indexesData));
Expand All @@ -672,7 +671,8 @@
: 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);
Expand Down
8 changes: 7 additions & 1 deletion packages/s2-core/src/utils/sort-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
/**
* 按明细数据
Expand Down
38 changes: 19 additions & 19 deletions s2-site/docs/api/basic-class/base-data-set.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | `<T extends boolean = false>(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 | `<T extends boolean = false>(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

Expand Down
2 changes: 1 addition & 1 deletion s2-site/docs/api/basic-class/base-data-set.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |

Expand Down
Loading