Skip to content

Commit

Permalink
feat: 增加自定义行列头对总计小计的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo committed Oct 28, 2024
1 parent 99bbd46 commit 81ffb0d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
35 changes: 0 additions & 35 deletions packages/s2-core/src/data-set/custom-tree-pivot-data-set.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,9 @@
import { get, type PropertyPath } from 'lodash';
import { EXTRA_FIELD } from '../common/constant';
import { i18n } from '../common/i18n';
import type { Meta, S2DataConfig } from '../common/interface';
import {
getDataPath,
getDataPathPrefix,
getIndexFields,
transformDimensionsValues,
} from '../utils/dataset/pivot-data-set';
import { CellData } from './cell-data';
import type { GetCellDataParams } from './interface';
import { PivotDataSet } from './pivot-data-set';

export class CustomTreePivotDataSet extends PivotDataSet {
getCellData(params: GetCellDataParams) {
const { query = {} } = params || {};
const { columns, rows } = this.fields;

const indexRows = getIndexFields(rows);
const indexColumns = getIndexFields(columns);

const rowDimensionValues = transformDimensionsValues(query, indexRows);
const colDimensionValues = transformDimensionsValues(query, indexColumns);
const path = getDataPath({
rowDimensionValues,
colDimensionValues,
rowPivotMeta: this.rowPivotMeta,
colPivotMeta: this.colPivotMeta,
rowFields: indexRows,
colFields: indexColumns,
prefix: getDataPathPrefix(indexRows, indexColumns),
});

const rawData = get(this.indexesData, path as PropertyPath);

if (rawData) {
return CellData.getCellData(rawData, query[EXTRA_FIELD]);
}
}

processDataCfg(dataCfg: S2DataConfig): S2DataConfig {
/**
* 自定义行头有如下几个特点
Expand Down
4 changes: 2 additions & 2 deletions packages/s2-core/src/sheet-type/pivot-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ export class PivotSheet extends SpreadSheet {
const { rows, columns } = this.dataCfg.fields;
const { hideValue } = this.options.style!.colCell!;
const sortField = this.isValueInCols() ? last(rows) : last(columns);
const { query, value } = meta;
const { query, field, value, extra } = meta;
const sortQuery = clone(query);

let sortValue = value;
let sortValue = extra?.isCustomNode ? field : value;

// 数值置于列头且隐藏了指标列头的情况, 会默认取第一个指标做组内排序, 需要还原指标列的 query, 所以多指标时请不要这么用……
if (hideValue && this.isValueInCols()) {
Expand Down
58 changes: 57 additions & 1 deletion packages/s2-react/playground/components/CustomGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/* eslint-disable no-console */
import type { S2DataConfig, SpreadSheet, ThemeCfg } from '@antv/s2';
import {
Aggregation,
type S2DataConfig,
type SpreadSheet,
type ThemeCfg,
} from '@antv/s2';
import {
customColGridFields,
customRowGridFields,
Expand Down Expand Up @@ -154,6 +159,57 @@ export const CustomGrid = React.forwardRef<SpreadSheet, CustomGridProps>(
setSheetType(checked ? 'pivot' : 'table');
}}
/>

<Switch
checkedChildren="显示行小计/总计"
unCheckedChildren="隐藏行小计/总计"
disabled={customType === CustomType.Row}
defaultChecked={options.totals?.row?.showSubTotals as boolean}
onChange={(checked) => {
setOptions({
totals: {
row: {
showGrandTotals: checked,
showSubTotals: checked,
reverseGrandTotalsLayout: true,
reverseSubTotalsLayout: true,
subTotalsDimensions: ['province'],
calcGrandTotals: {
aggregation: Aggregation.SUM,
},
calcSubTotals: {
aggregation: Aggregation.SUM,
},
},
},
});
}}
/>
<Switch
checkedChildren="显示列小计/总计"
unCheckedChildren="隐藏列小计/总计"
disabled={customType === CustomType.Col}
defaultChecked={options.totals?.col?.showSubTotals as boolean}
onChange={(checked) => {
setOptions({
totals: {
col: {
showGrandTotals: checked,
showSubTotals: checked,
reverseGrandTotalsLayout: true,
reverseSubTotalsLayout: true,
subTotalsDimensions: ['type'],
calcGrandTotals: {
aggregation: Aggregation.SUM,
},
calcSubTotals: {
aggregation: Aggregation.SUM,
},
},
},
});
}}
/>
</Space>
<Space style={{ marginBottom: 20, display: 'flex' }}>
<ResizeConfig
Expand Down

0 comments on commit 81ffb0d

Please sign in to comment.