Skip to content

Commit

Permalink
fix: 修复分组汇总时,按汇总排序获取排序数据为空 (#2370)
Browse files Browse the repository at this point in the history
* fix: 修复指标不在最后一级配置与分组汇总同时使用时,获取数据为空

* fix: 修复指标不在最后一级配置与分组汇总同时使用时,获取数据为空

* test: 增加分组汇总时按汇总排序单测

* test: 增加分组汇总时按汇总排序单测

* chore: 优化代码可读,getMultiData 使用对象传参

* chore: 优化代码可读,getMultiData 使用对象传参

* chore: 优化代码可读,getMultiData 使用对象传参

* chore: getMultiData 使用传参格式改回

* chore: 改名为 includeTotalData 参数名以及文档

* docs: 加个空格

---------

Co-authored-by: JuZe <[email protected]>
  • Loading branch information
NoobNotN and JuZe authored Oct 20, 2023
1 parent bd09e3d commit 2443762
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 27 deletions.
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,
): 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 @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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));
Expand All @@ -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);
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

1 comment on commit 2443762

@vercel
Copy link

@vercel vercel bot commented on 2443762 Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

antvis-s2 – ./

antvis-s2-git-master-antv-s2.vercel.app
antvis-s2.vercel.app
antvis-s2-antv-s2.vercel.app

Please sign in to comment.