Skip to content

Commit

Permalink
fix: 自定义计算汇总 calcFunc 回调函数增加 s2 实例, 修复无法获取汇总数据的问题 close #2795 (#2805)
Browse files Browse the repository at this point in the history
* fix: 自定义计算汇总 calcFunc 回调函数增加 s2 实例, 修复无法获取汇总数据的问题 close #2795

* docs: 更新文档主题版本

* docs: 更新文档主题版本
  • Loading branch information
lijinke666 committed Jul 5, 2024
1 parent c8ebc98 commit 2c28e84
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { get, keys } from 'lodash';
import * as multiDataCfg from 'tests/data/simple-data.json';
import { assembleDataCfg, TOTALS_OPTIONS } from '../../util';
import { CalcTotals, SpreadSheet } from '../../../src';
import {
EXTRA_FIELD,
QueryDataType,
Expand Down Expand Up @@ -402,18 +403,31 @@ describe('Pivot Dataset Total Test', () => {
});

describe('getCellData function when totals calculated by calcFunc', () => {
let s2: SpreadSheet | undefined;

beforeEach(() => {
MockPivotSheet.mockClear();
const mockSheet = new MockPivotSheet();
mockSheet.store = new Store();
mockSheet.isValueInCols = () => true;
const calcFunc1 = (query, data) => {

const calcFunc1: CalcTotals['calcFunc'] = (
query,
data,
spreadsheet,
) => {
s2 = spreadsheet;
const sum = data.reduce((pre, next) => {
return pre + next[next[EXTRA_FIELD]];
}, 0);
return sum * 2;
};
const calcFunc2 = (query, data) => {
const calcFunc2: CalcTotals['calcFunc'] = (
query,
data,
spreadsheet,
) => {
s2 = spreadsheet;
const sum = data.reduce((pre, next) => {
return pre + next[next[EXTRA_FIELD]];
}, 0);
Expand Down Expand Up @@ -451,6 +465,10 @@ describe('Pivot Dataset Total Test', () => {
dataSet.setDataCfg(dataCfg);
});

afterEach(() => {
s2 = undefined;
});

test('should get correct total cell data when totals calculated by calcFunc and Existential dimension grouping', () => {
const totalStatus = {
isRowTotal: true,
Expand Down Expand Up @@ -504,6 +522,8 @@ describe('Pivot Dataset Total Test', () => {
totalStatus,
}),
).toContainEntries([[VALUE_FIELD, 32418]]);

expect(s2).toBeDefined();
});

test('should get correct total cell data when totals calculated by calcFunc', () => {
Expand Down Expand Up @@ -571,6 +591,8 @@ describe('Pivot Dataset Total Test', () => {
isTotals: true,
}),
).toContainEntries([[VALUE_FIELD, 78868]]);

expect(s2).toBeDefined();
});
});

Expand Down
6 changes: 5 additions & 1 deletion packages/s2-core/src/common/interface/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ export enum Aggregation {

export interface CalcTotals {
aggregation?: Aggregation; // 聚合方式
calcFunc?: (query: Query, arr: DataType[]) => number;
calcFunc?: (
query: Query,
data: DataType[],
spreadsheet: SpreadSheet,
) => number;
}

export interface Total {
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ export class PivotDataSet extends BaseDataSet {
});
let totalValue: number;
if (calcFunc) {
totalValue = calcFunc(query, data);
totalValue = calcFunc(query, data, this.spreadsheet);
} else if (calcAction) {
totalValue = calcAction(data, VALUE_FIELD);
}
Expand Down
10 changes: 4 additions & 6 deletions packages/s2-core/src/utils/data-set-operate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
EMPTY_EXTRA_FIELD_PLACEHOLDER,
TOTAL_VALUE,
} from '../common/constant/field';
import type { Totals, TotalsStatus } from '../common/interface';
import type { CalcTotals, Totals, TotalsStatus } from '../common/interface';

export const getListBySorted = (
list: string[],
Expand Down Expand Up @@ -68,11 +68,9 @@ export function getAggregationAndCalcFuncByQuery(
calcTotals: colCalcTotals = {},
calcSubTotals: colCalcSubTotals = {},
} = col || {};
const getCalcTotals = (dimensionTotals, totalType) => {
if (
(dimensionTotals.aggregation || dimensionTotals.calcFunc) &&
totalType
) {

const getCalcTotals = (dimensionTotals: CalcTotals, isTotal: boolean) => {
if ((dimensionTotals.aggregation || dimensionTotals.calcFunc) && isTotal) {
return {
aggregation: dimensionTotals.aggregation,
calcFunc: dimensionTotals.calcFunc,
Expand Down
59 changes: 15 additions & 44 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 23 additions & 9 deletions s2-site/.dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,25 @@ export default defineConfig({
showChartResize: true, // 是否在 demo 页展示图表视图切换
showAPIDoc: true, // 是否在 demo 页展示API文档
es5: false, // 案例代码是否编译到 es5
versions: {
// 历史版本以及切换下拉菜单
'1.x': 'https://s2-v1.antv.antgroup.com',
'2.x': 'https://s2.antv.antgroup.com',
},
docsearchOptions: {
// 头部搜索框配置
versionV3: true,
apiKey: '74b99a09199729fd4ac472746ada8456',
indexName: 's2-antv-antgroup',
appId: 'LWCKDMVZ87',
},
internalSite: {
url: 'https://s2.antv.antgroup.com',
name: {
zh: '国内镜像',
en: 'China Mirror',
},
},
// internalSite: {
// url: 'https://s2.antv.antgroup.com',
// name: {
// zh: '国内镜像',
// en: 'China Mirror',
// },
// },
navs: [
{
slug: 'docs/manual',
Expand Down Expand Up @@ -270,8 +275,17 @@ export default defineConfig({
</html>`,
},
announcement: {
zh: '',
en: '',
title: {
zh: '🎉 S2 2.0 版本开始内测啦! v1 版本维护截止日期为 2024 年年底。',
en: '🎉 S2 Next version 2.0 is in beta! The maintenance deadline for the original 1.x version is the end of 2024.',
},
link: {
text: {
zh: '查看升级指南',
en: 'Upgrade Guide',
},
url: 'https://s2.antv.antgroup.com/manual/migration-v2',
},
},
/** 首页技术栈介绍 */
detail: {
Expand Down
2 changes: 1 addition & 1 deletion s2-site/docs/common/totals.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ object **可选**,_default:null_ 功能描述: 计算小计总计配置
| 参数 | 说明 | 类型 | 必选 | 默认值 |
| ----------- | ---------- | -------------------------------------------------------------------- | --- | ------ |
| aggregation | 聚合方式 | `Aggregation.SUM` \| `Aggregation.MIN` \| `Aggregation.MAX` \| `Aggregation.AVG` | | |
| calcFunc | 自定义方法 | `(query: Record<string, any>, arr: Record<string, any>[]) => number` | | |
| calcFunc | 自定义计算 | `(query: Record<string, any>, data: Record<string, any>[], spreadsheet: SpreadSheet) => number` | | |
22 changes: 19 additions & 3 deletions s2-site/docs/manual/basic/totals.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,27 @@ const s2DataConfig = {

<br/>

##### 2.2. 配置自定义方法
##### 2.2. 配置自定义计算方法

通过配置 `calcFunc: (query: Record<string, any>, arr: Record<string, any>[]) => number` 来实现。[查看示例](https://s2.antv.antgroup.com/zh/examples/analysis/totals/#custom)
通过配置 `calcFunc: (query: Record<string, any>, data: Record<string, any>[], spreadsheet: SpreadSheet) => number` 来实现。[查看示例](https://s2.antv.antgroup.com/zh/examples/analysis/totals/#custom)

<Playground path='analysis/totals/demo/custom.ts' rid='pivot-total-group' height='400'></playground>
注意:`data` 为明细数据,如需获取包含汇总的数据

```ts
import { QueryDataType } from '@antv/s2';

const calcFunc = (query, data, spreadsheet) => {
const allData = spreadsheet.dataSet.getMultiData(query, {
queryType: QueryDataType.All,
});

console.log('data(明细数据):', data);
console.log('data(全部数据,含汇总):', allData);
};

```

<Playground path='analysis/totals/demo/custom.ts' rid='pivot-total-custom' height='400'></playground>

<br/>

Expand Down
11 changes: 9 additions & 2 deletions s2-site/examples/analysis/totals/demo/custom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PivotSheet, EXTRA_FIELD } from '@antv/s2';
import { PivotSheet, EXTRA_FIELD, QueryDataType } from '@antv/s2';

fetch('https://render.alipay.com/p/yuyan/180020010001215413/s2/basic.json')
.then((res) => res.json())
Expand Down Expand Up @@ -31,7 +31,14 @@ fetch('https://render.alipay.com/p/yuyan/180020010001215413/s2/basic.json')
data,
};

const calcFunc = (query, data) => {
const calcFunc = (query, data, spreadsheet) => {
const allData = spreadsheet.dataSet.getMultiData(query, {
queryType: QueryDataType.All,
});

console.log('data (明细数据):', data);
console.log('data (全部数据, 含汇总):', allData);

const sum = data.reduce((pre, next) => {
return pre + next[next[EXTRA_FIELD]];
}, 0);
Expand Down
6 changes: 3 additions & 3 deletions s2-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
},
"dependencies": {
"@ant-design/icons": "^4.8.1",
"@antv/dumi-theme-antv": "^0.5.0",
"@antv/dumi-theme-antv": "^0.5.2",
"@antv/g-canvas": "^0.5.14",
"@antv/s2": "^1.54.3",
"@antv/s2-react": "^1.46.1",
"@antv/s2": "^1.55.6",
"@antv/s2-react": "^1.47.0",
"antd": "^4.24.15",
"copy-to-clipboard": "^3.3.3",
"dumi": "^2.2.17",
Expand Down

0 comments on commit 2c28e84

Please sign in to comment.