Skip to content

Commit

Permalink
refactor: 处理总计小计在 indexData 中的存储位置
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo committed Oct 23, 2023
1 parent 8bf7c2d commit ea38a38
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 91 deletions.
1 change: 1 addition & 0 deletions packages/s2-core/__tests__/unit/facet/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function getMockPivotMeta() {
return transformIndexesData({
rows: fields.rows,
columns: fields.columns,
values: fields.values,
originData: data,
indexesData: rawIndexesData,
totalData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ describe('PivotDataSet util test', () => {
});

test('for transformIndexesData function', () => {
const { rows, columns } = dataCfg.fields;
const { rows, columns, values } = dataCfg.fields;
const sortedDimensionValues = {};
const rowPivotMeta = new Map();
const colPivotMeta = new Map();
const result = transformIndexesData({
rows,
columns: columns as string[],
values,
originData: dataCfg.data,
totalData: [],
indexesData: [],
Expand Down Expand Up @@ -105,7 +106,6 @@ describe('PivotDataSet util test', () => {
rowPivotMeta,
colPivotMeta,
isFirstCreate: true,
careUndefined: false,
rowFields: rows,
colFields: columns,
});
Expand All @@ -124,7 +124,6 @@ describe('PivotDataSet util test', () => {
rowPivotMeta,
colPivotMeta,
isFirstCreate: false,
careUndefined: false,
});
expect(rowPivotMeta.size).toEqual(0);
expect(colPivotMeta.size).toEqual(0);
Expand All @@ -142,7 +141,6 @@ describe('PivotDataSet util test', () => {
rowPivotMeta,
colPivotMeta,
isFirstCreate: true,
careUndefined: false,
});
expect(rowPivotMeta.get(rowDimensionValues[0]).childField).toBeUndefined();
expect(colPivotMeta.get(colDimensionValues[0]).childField).toBeUndefined();
Expand All @@ -162,7 +160,6 @@ describe('PivotDataSet util test', () => {
rowPivotMeta,
colPivotMeta,
isFirstCreate: true,
careUndefined: false,
rowFields: rows,
colFields: columns,
});
Expand Down
1 change: 1 addition & 0 deletions packages/s2-core/src/common/constant/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const VALUE_FIELD = '$$value$$';
export const EXTRA_FIELD = '$$extra$$';
export const EXTRA_COLUMN_FIELD = '$$extra_column$$';
export const TOTAL_VALUE = '$$total$$';
export const MULTI_VALUE = '$$multi$$';
export const SERIES_NUMBER_FIELD = '$$series_number$$';

export const BACK_GROUND_GROUP_CONTAINER_Z_INDEX = 0;
Expand Down
6 changes: 1 addition & 5 deletions packages/s2-core/src/data-set/custom-tree-pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { PivotDataSet } from './pivot-data-set';
export class CustomTreePivotDataSet extends PivotDataSet {
getCellData(params: CellDataParams): DataType {
const { query } = params;
const { columns, rows } = this.fields;
const { rows, columns } = this.fields;
const rowDimensionValues = transformDimensionsValues(query, rows);
// 透视表下columns只支持简单结构
const colDimensionValues = transformDimensionsValues(
Expand All @@ -25,10 +25,6 @@ export class CustomTreePivotDataSet extends PivotDataSet {
colDimensionValues,
rowPivotMeta: this.rowPivotMeta,
colPivotMeta: this.colPivotMeta,
isFirstCreate: true,
careUndefined: true,
rowFields: rows,
colFields: columns as string[],
});
const data = get(this.indexesData, path);
if (data) {
Expand Down
4 changes: 0 additions & 4 deletions packages/s2-core/src/data-set/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@ export type DataPathParams = {
isFirstCreate?: boolean;
// callback when pivot map create node
onFirstCreate?: (params: {
// 是否是行头字段
isRow: boolean;
// 维度 id,如 city
dimension: string;
// 维度数组 ['四川省', '成都市']
dimensionPath: string[];
}) => void;
// use for multiple data queries(path contains undefined)
careUndefined?: boolean;
// use in row tree mode to append fields information
rowFields?: string[];
colFields?: string[];
Expand Down
28 changes: 11 additions & 17 deletions packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ import type {
} from '../common/interface';
import { Node } from '../facet/layout/node';
import {
filterUndefined,
filterTotal,
flatten as customFlatten,
flattenDeep as customFlattenDeep,
getAggregationAndCalcFuncByQuery,
getFieldKeysByDimensionValues,
getListBySorted,
isEveryUndefined,
isTotalData,
splitTotal,
} from '../utils/data-set-operate';
import {
Expand Down Expand Up @@ -165,7 +164,6 @@ export class PivotDataSet extends BaseDataSet {
originData,
totalData,
indexesData: this.indexesData,

sortedDimensionValues: this.sortedDimensionValues,
rowPivotMeta: this.rowPivotMeta,
colPivotMeta: this.colPivotMeta,
Expand Down Expand Up @@ -329,7 +327,7 @@ export class PivotDataSet extends BaseDataSet {
field,
}),
);
return filterUndefined(
return filterTotal(
uniq(getDimensionsWithoutPathPre([...allCurrentFieldDimensionValues])),
);
}
Expand Down Expand Up @@ -371,16 +369,16 @@ export class PivotDataSet extends BaseDataSet {
if (isEmpty(sortedMeta)) {
return [];
}
return filterUndefined(getListBySorted([...meta.keys()], sortedMeta));
return filterTotal(getListBySorted([...meta.keys()], sortedMeta));
}

if (this.sortedDimensionValues[field]) {
return filterUndefined(
return filterTotal(
getDimensionsWithoutPathPre([...this.sortedDimensionValues[field]]),
);
}

return filterUndefined([...meta.keys()]);
return filterTotal([...meta.keys()]);
}

getTotalValue(query: Query, totalStatus?: TotalStatus) {
Expand Down Expand Up @@ -429,6 +427,7 @@ export class PivotDataSet extends BaseDataSet {
if (!isTotals || isDrillDown) {
rows = Node.getFieldPath(rowNode, isDrillDown) ?? originRows;
}

const rowDimensionValues = transformDimensionsValues(query, rows);
const colDimensionValues = transformDimensionsValues(
query,
Expand All @@ -437,8 +436,6 @@ export class PivotDataSet extends BaseDataSet {
const path = getDataPath({
rowDimensionValues,
colDimensionValues,
careUndefined:
isTotals || isTotalData([].concat(originRows).concat(columns), query),
rowPivotMeta: this.rowPivotMeta,
colPivotMeta: this.colPivotMeta,
});
Expand All @@ -447,7 +444,10 @@ export class PivotDataSet extends BaseDataSet {
// 如果已经有数据则取已有数据
return DataHandler.createProxyData(data, query[EXTRA_FIELD]);
}
return isTotals ? this.getTotalValue(query, totalStatus) : data;

if (isTotals) {
return this.getTotalValue(query, totalStatus);
}
}

getCustomData = (path: number[]) => {
Expand Down Expand Up @@ -630,9 +630,6 @@ export class PivotDataSet extends BaseDataSet {
const path = getDataPath({
rowDimensionValues,
colDimensionValues,
careUndefined: true,
rowFields: rows,
colFields: columns as string[],
rowPivotMeta: this.rowPivotMeta,
colPivotMeta: this.colPivotMeta,
});
Expand All @@ -646,7 +643,7 @@ export class PivotDataSet extends BaseDataSet {
query: Query,
isTotals?: boolean,
isRow?: boolean,
drillDownFields?: string[],
drillDownFields: string[] = [],
includeTotalData?: boolean,
): DataType[] {
if (isEmpty(query)) {
Expand All @@ -672,9 +669,6 @@ export class PivotDataSet extends BaseDataSet {
const path = getDataPath({
rowDimensionValues,
colDimensionValues,
careUndefined: true,
rowFields: rows,
colFields: columns as string[],
rowPivotMeta: this.rowPivotMeta,
colPivotMeta: this.colPivotMeta,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/data-set/table-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class TableDataSet extends BaseDataSet {
return rowData[query.col];
}

public getMultiData(query: Query, isTotals?: boolean): DataType[] {
public getMultiData(): DataType[] {
return this.displayData;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/s2-core/src/facet/layout/build-row-tree-hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isNumber } from 'lodash';
import { i18n, ID_SEPARATOR, ROOT_ID } from '../../common';
import type { PivotDataSet } from '../../data-set';
import type { SpreadSheet } from '../../sheet-type';
import { filterUndefined, getListBySorted } from '../../utils/data-set-operate';
import { filterTotal, getListBySorted } from '../../utils/data-set-operate';
import { generateId } from '../../utils/layout/generate-id';
import type { FieldValue, TreeHeaderParams } from '../layout/interface';
import { layoutArrange, layoutHierarchy } from '../layout/layout-hooks';
Expand Down Expand Up @@ -53,7 +53,7 @@ export const buildRowTreeHierarchy = (params: TreeHeaderParams) => {
const sortedDimensionValues =
(dataSet as PivotDataSet)?.sortedDimensionValues?.[currentField] || [];

const unsortedDimValues = filterUndefined(Array.from(pivotMeta.keys()));
const unsortedDimValues = filterTotal(Array.from(pivotMeta.keys()));
const dimValues = getListBySorted(
unsortedDimValues,
sortedDimensionValues,
Expand Down
9 changes: 5 additions & 4 deletions packages/s2-core/src/utils/data-set-operate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { every, filter, get, isUndefined, keys, reduce } from 'lodash';
import { every, get, isUndefined, keys, reduce } from 'lodash';
import { TOTAL_VALUE } from '../common/constant/basic';
import type { Data, Fields, Totals, TotalsStatus } from '../common/interface';

export const getListBySorted = (
Expand Down Expand Up @@ -27,8 +28,8 @@ export const getListBySorted = (
});
};

export const filterUndefined = (values: string[]) => {
return filter(values, (t) => !isUndefined(t) && t !== 'undefined');
export const filterTotal = (values: string[] = []) => {
return values.filter((v) => v !== TOTAL_VALUE);
};

export const flattenDeep = (data: Record<any, any>[] | Record<any, any>) =>
Expand Down Expand Up @@ -108,7 +109,7 @@ export const sortByItems = (arr1: string[], arr2: string[]) => {
* @returns
*/
export const isTotalData = (ids: string[], data: Data): boolean => {
return !every(ids, (id) => data[id]);
return !every(ids, (id) => id in data);
};

/**
Expand Down
Loading

0 comments on commit ea38a38

Please sign in to comment.