From 2c28e841752ab94829b3c38f41becc536a5e8328 Mon Sep 17 00:00:00 2001 From: Jinke Li Date: Fri, 5 Jul 2024 15:59:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=87=AA=E5=AE=9A=E4=B9=89=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E6=B1=87=E6=80=BB=20calcFunc=20=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=A2=9E=E5=8A=A0=20s2=20=E5=AE=9E=E4=BE=8B,?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=B1=87=E6=80=BB=E6=95=B0=E6=8D=AE=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20close=20#2795=20(#2805)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 自定义计算汇总 calcFunc 回调函数增加 s2 实例, 修复无法获取汇总数据的问题 close #2795 * docs: 更新文档主题版本 * docs: 更新文档主题版本 --- .../data-set/pivot-data-set-total-spec.ts | 26 +++++++- .../s2-core/src/common/interface/basic.ts | 6 +- .../s2-core/src/data-set/pivot-data-set.ts | 2 +- .../s2-core/src/utils/data-set-operate.ts | 10 ++-- pnpm-lock.yaml | 59 +++++-------------- s2-site/.dumirc.ts | 32 +++++++--- s2-site/docs/common/totals.zh.md | 2 +- s2-site/docs/manual/basic/totals.zh.md | 22 ++++++- .../examples/analysis/totals/demo/custom.ts | 11 +++- s2-site/package.json | 6 +- 10 files changed, 104 insertions(+), 72 deletions(-) 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 029dd0cb47..1e3ba4be96 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 @@ -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, @@ -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); @@ -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, @@ -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', () => { @@ -571,6 +591,8 @@ describe('Pivot Dataset Total Test', () => { isTotals: true, }), ).toContainEntries([[VALUE_FIELD, 78868]]); + + expect(s2).toBeDefined(); }); }); diff --git a/packages/s2-core/src/common/interface/basic.ts b/packages/s2-core/src/common/interface/basic.ts index 3314d1e74b..e06f7f6ca4 100644 --- a/packages/s2-core/src/common/interface/basic.ts +++ b/packages/s2-core/src/common/interface/basic.ts @@ -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 { 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 3e4fc11792..ac69462197 100644 --- a/packages/s2-core/src/data-set/pivot-data-set.ts +++ b/packages/s2-core/src/data-set/pivot-data-set.ts @@ -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); } diff --git a/packages/s2-core/src/utils/data-set-operate.ts b/packages/s2-core/src/utils/data-set-operate.ts index 4075c49228..bf1cd5123b 100644 --- a/packages/s2-core/src/utils/data-set-operate.ts +++ b/packages/s2-core/src/utils/data-set-operate.ts @@ -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[], @@ -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, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 20b2327e99..5ed264abc8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -426,17 +426,17 @@ importers: specifier: ^4.8.1 version: 4.8.1(react-dom@18.2.0)(react@18.2.0) '@antv/dumi-theme-antv': - specifier: ^0.5.0 - version: 0.5.0(@babel/core@7.23.7)(dumi@2.2.17) + specifier: ^0.5.2 + version: 0.5.2(@babel/core@7.23.7)(dumi@2.2.17) '@antv/g-canvas': specifier: ^0.5.14 version: 0.5.14 '@antv/s2': - specifier: ^1.54.3 - version: 1.54.3 + specifier: ^1.55.6 + version: link:../packages/s2-core '@antv/s2-react': - specifier: ^1.46.1 - version: 1.46.1(@ant-design/icons@4.8.1)(@antv/s2@1.54.3)(antd@4.24.15) + specifier: ^1.47.0 + version: link:../packages/s2-react antd: specifier: ^4.24.15 version: 4.24.15 @@ -732,8 +732,8 @@ packages: resolution: {integrity: sha512-gFPqTG7otEJ8uP6wrhDv6mqwGWYZKNvAcCq6u9hOj0c+IKCEsY4L1oC9trPq2SaWIzAfHvqfBDxF591JkMf+kg==} dev: false - /@antv/dumi-theme-antv@0.5.0(@babel/core@7.23.7)(dumi@2.2.17): - resolution: {integrity: sha512-IL17dW9x9sJbRbdrDxTw9/gUrVWMt7rAS+pBeN6YVSsNh0/nqMDEmGtrLyrxEBCcnmo4azSocKr/uKMW9Jt+RQ==} + /@antv/dumi-theme-antv@0.5.2(@babel/core@7.23.7)(dumi@2.2.17): + resolution: {integrity: sha512-jLsWW9ZaKZHEOmlZ+XAaznuFopBPujvEzGl7cSoPwtS5fXOBwqY94eyv5M33maWDPaq5MzelybVQ5In4uTT0Jw==} peerDependencies: dumi: ^2.0.0 dependencies: @@ -857,40 +857,6 @@ packages: '@antv/util': 2.0.17 tslib: 2.6.2 - /@antv/s2-react@1.46.1(@ant-design/icons@4.8.1)(@antv/s2@1.54.3)(antd@4.24.15): - resolution: {integrity: sha512-nE4EzJrtTvtU2dOWR5nybISBbr1xvQv0DRm83jJ7Gm1JZh2mr1R5YkI6bWKBgRYXxInBgcn1n+pKU6AuVg9wXA==} - peerDependencies: - '@ant-design/icons': '>=4.7.0' - '@antv/s2': '>=1.0.0' - antd: '>=4.16.13' - react: '>=16.8.0' - react-dom: '>=16.8.0' - dependencies: - '@ant-design/icons': 4.8.1(react-dom@18.2.0)(react@18.2.0) - '@antv/s2': 1.54.3 - ahooks: 3.7.8(react@17.0.2) - antd: 4.24.15 - classnames: 2.5.1 - lodash: 4.17.21 - react-beautiful-dnd: 13.1.1(react-dom@17.0.2)(react@17.0.2) - transitivePeerDependencies: - - react-native - dev: false - - /@antv/s2@1.54.3: - resolution: {integrity: sha512-HEzK+KHd/uhyJAp6Ijk2z8T91CrknTltcTahi1B0GmKi/v4FAtSEhB0QT5bldS0pALeRgjij0AfdPC6C4qYJ+g==} - dependencies: - '@antv/event-emitter': 0.1.3 - '@antv/g-canvas': 0.5.14 - '@antv/g-gesture': 1.0.1 - d3-interpolate: 1.4.0 - d3-timer: 1.0.10 - decimal.js: 10.4.3 - lodash: 4.17.21 - transitivePeerDependencies: - - '@antv/g-base' - dev: false - /@antv/util@2.0.17: resolution: {integrity: sha512-o6I9hi5CIUvLGDhth0RxNSFDRwXeywmt6ExR4+RmVAzIi48ps6HUy+svxOCayvrPBN37uE6TAc2KDofRo0nK9Q==} dependencies: @@ -4356,7 +4322,7 @@ packages: /@types/hast@3.0.3: resolution: {integrity: sha512-2fYGlaDy/qyLlhidX42wAH0KBi2TCjKMH8CHmBXgRlJ3Y+OXTiqsPQ6IWarZKwF1JoUcAJdPogv1d4b0COTpmQ==} dependencies: - '@types/unist': 2.0.10 + '@types/unist': 3.0.2 dev: false /@types/hoist-non-react-statics@3.3.5: @@ -4446,7 +4412,7 @@ packages: /@types/mdast@4.0.3: resolution: {integrity: sha512-LsjtqsyF+d2/yFOYaN22dHZI1Cpwkrj+g06G8+qtUKlhovPW89YhqSnfKtMbkgmEtYpH2gydRNULd6y8mciAFg==} dependencies: - '@types/unist': 2.0.10 + '@types/unist': 3.0.2 dev: false /@types/mdurl@1.0.5: @@ -7611,6 +7577,7 @@ packages: /copy-concurrently@1.0.5: resolution: {integrity: sha512-f2domd9fsVDFtaFcbaRZuYXwtdmnzqbADSwhSWYxYB/Q8zsdUUFMXVRwXGDMWmbEzAn1kdRrtI1T/KTFOL4X2A==} + deprecated: This package is no longer supported. dependencies: aproba: 1.2.0 fs-write-stream-atomic: 1.0.10 @@ -10553,6 +10520,7 @@ packages: /fs-write-stream-atomic@1.0.10: resolution: {integrity: sha512-gehEzmPn2nAwr39eay+x3X34Ra+M2QlVUTLhkXPjWdeO8RF9kszk116avgBJM3ZyNHgHXBNx+VmPaFC36k0PzA==} + deprecated: This package is no longer supported. dependencies: graceful-fs: 4.2.11 iferr: 0.1.5 @@ -15820,6 +15788,7 @@ packages: /move-concurrently@1.0.1: resolution: {integrity: sha512-hdrFxZOycD/g6A6SoI2bB5NA/5NEqD0569+S47WZhPvm46sD50ZHdYaFmnua5lndde9rCHGjmfK7Z8BuCt/PcQ==} + deprecated: This package is no longer supported. dependencies: aproba: 1.2.0 copy-concurrently: 1.0.5 @@ -16544,6 +16513,7 @@ packages: /osenv@0.1.5: resolution: {integrity: sha512-0CWcCECdMVc2Rw3U5w9ZjqX6ga6ubk1xDVKxtBQPK7wis/0F2r9T6k4ydGYhecl7YUBxBVxhL5oisPsNxAPe2g==} + deprecated: This package is no longer supported. dependencies: os-homedir: 1.0.2 os-tmpdir: 1.0.2 @@ -20597,6 +20567,7 @@ packages: /rimraf@2.7.1: resolution: {integrity: sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==} + deprecated: Rimraf versions prior to v4 are no longer supported hasBin: true dependencies: glob: 7.2.3 diff --git a/s2-site/.dumirc.ts b/s2-site/.dumirc.ts index 1cd49e83e3..8e5164b0c0 100644 --- a/s2-site/.dumirc.ts +++ b/s2-site/.dumirc.ts @@ -32,6 +32,11 @@ 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, @@ -39,13 +44,13 @@ export default defineConfig({ 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', @@ -270,8 +275,17 @@ export default defineConfig({ `, }, 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: { diff --git a/s2-site/docs/common/totals.zh.md b/s2-site/docs/common/totals.zh.md index 6edacd116a..9e123ec67e 100644 --- a/s2-site/docs/common/totals.zh.md +++ b/s2-site/docs/common/totals.zh.md @@ -37,4 +37,4 @@ object **可选**,_default:null_ 功能描述: 计算小计总计配置 | 参数 | 说明 | 类型 | 必选 | 默认值 | | ----------- | ---------- | -------------------------------------------------------------------- | --- | ------ | | aggregation | 聚合方式 | `Aggregation.SUM` \| `Aggregation.MIN` \| `Aggregation.MAX` \| `Aggregation.AVG` | | | -| calcFunc | 自定义方法 | `(query: Record, arr: Record[]) => number` | | | +| calcFunc | 自定义计算 | `(query: Record, data: Record[], spreadsheet: SpreadSheet) => number` | | | diff --git a/s2-site/docs/manual/basic/totals.zh.md b/s2-site/docs/manual/basic/totals.zh.md index 4d07c9910f..164597e8a9 100644 --- a/s2-site/docs/manual/basic/totals.zh.md +++ b/s2-site/docs/manual/basic/totals.zh.md @@ -222,11 +222,27 @@ const s2DataConfig = {
-##### 2.2. 配置自定义方法 +##### 2.2. 配置自定义计算方法 -通过配置 `calcFunc: (query: Record, arr: Record[]) => number` 来实现。[查看示例](https://s2.antv.antgroup.com/zh/examples/analysis/totals/#custom) +通过配置 `calcFunc: (query: Record, data: Record[], spreadsheet: SpreadSheet) => number` 来实现。[查看示例](https://s2.antv.antgroup.com/zh/examples/analysis/totals/#custom) - +注意:`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); +}; + +``` + +
diff --git a/s2-site/examples/analysis/totals/demo/custom.ts b/s2-site/examples/analysis/totals/demo/custom.ts index eeb012699e..1ae997ae94 100644 --- a/s2-site/examples/analysis/totals/demo/custom.ts +++ b/s2-site/examples/analysis/totals/demo/custom.ts @@ -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()) @@ -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); diff --git a/s2-site/package.json b/s2-site/package.json index 456300a085..695163a05f 100644 --- a/s2-site/package.json +++ b/s2-site/package.json @@ -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",