Skip to content

Commit

Permalink
fix: 兼容 customValueOrder 的场景
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo committed Nov 24, 2023
1 parent 2bcde16 commit 05808f1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 51 deletions.
5 changes: 2 additions & 3 deletions packages/s2-core/src/data-set/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { QueryDataType } from '../common/constant/query';
import type { SortParam } from '../common/interface';
import type { Data, SortParam } from '../common/interface';
import type { Node } from '../facet/layout/node';
import type { BaseDataSet } from './base-data-set';
// TODO add object data value
Expand Down Expand Up @@ -28,12 +28,11 @@ export type DataPathParams = {
colPivotMeta: PivotMeta;
rowFields: string[];
colFields: string[];
valueFields?: string[];
// first create data path
isFirstCreate?: boolean;
// callback when pivot map create node
onFirstCreate?: (params: {
careRepeated: boolean;
careRepeated?: boolean;
// 维度 id,如 city
dimension: string;
// 完整维度信息:'四川省[&]成都市'
Expand Down
6 changes: 4 additions & 2 deletions packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ export class PivotDataSet extends BaseDataSet {
this.rowPivotMeta = new Map();
this.colPivotMeta = new Map();
DebuggerUtil.getInstance().debugCallback(DEBUG_TRANSFORM_DATA, () => {
const { rows, columns, values } = this.fields;
const { rows, columns, values, valueInCols } = this.fields;
const { indexesData } = transformIndexesData({
rows,
columns: columns as string[],
values,
valueInCols,
data: this.originData.concat(this.totalData),
indexesData: this.indexesData,
sortedDimensionValues: this.sortedDimensionValues,
Expand All @@ -115,7 +116,7 @@ export class PivotDataSet extends BaseDataSet {
drillDownData: DataType[],
rowNode: Node,
) {
const { columns, values } = this.fields;
const { columns, values, valueInCols } = this.fields;
const currentRowFields = Node.getFieldPath(rowNode, true);
const nextRowFields = [...currentRowFields, extraRowField];
const store = this.spreadsheet.store;
Expand All @@ -142,6 +143,7 @@ export class PivotDataSet extends BaseDataSet {
rows: nextRowFields,
columns: columns as string[],
values,
valueInCols,
data: drillDownData,
indexesData: this.indexesData,
sortedDimensionValues: this.sortedDimensionValues,
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/frozen-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export abstract class FrozenFacet extends BaseFacet {

// 1.1. spread-sheet defined group and added panelGroup

// 1.2. prepare froznGroup info and init frozenGroup position
// 1.2. prepare frozenGroup info and init frozenGroup position
public calculateFrozenGroupInfo() {
const {
frozenColCount,
Expand Down
121 changes: 76 additions & 45 deletions packages/s2-core/src/utils/dataset/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import type {
TotalStatus,
} from '../../data-set/interface';
import type { Node } from '../../facet/layout/node';
import { generateId } from '../layout';

export function isMultiValue(pathValue: string | number) {
return pathValue === MULTI_VALUE;
Expand Down Expand Up @@ -60,10 +59,6 @@ export function transformDimensionsValues(
placeholder: string = TOTAL_VALUE,
): string[] {
return dimensions.reduce((res: string[], dimension: string) => {
if (dimension === EXTRA_FIELD) {
return res;
}
// push undefined when not exist
const value = record[dimension];
if (!(dimension in record)) {
res.push(placeholder);
Expand All @@ -74,6 +69,41 @@ export function transformDimensionsValues(
}, []);
}

export function transformDimensionsValuesWithExtraFields(
record: DataType = {},
dimensions: string[] = [],
values: string[] = [],
) {
const result = [];

function transform(data: DataType, fields: string[], valueField?: string) {
return fields.reduce((res: string[], dimension: string) => {
const value = data[dimension];
if (!(dimension in data)) {
if (dimension === EXTRA_FIELD && valueField) {
res.push(valueField);
} else {
res.push(TOTAL_VALUE);
}
} else {
res.push(String(value));
}
return res;
}, []);
}

if (isEmpty(values)) {
result.push(transform(record, dimensions));
}
values.forEach((value) => {
if (value in record) {
result.push(transform(record, dimensions, value));
}
});

return result;
}

/**
* Get dimensions without path pre
* dimensions: ['辽宁省[&]芜湖市[&]家具[&]椅子']
Expand Down Expand Up @@ -148,7 +178,6 @@ export function getDataPath(params: DataPathParams) {
onFirstCreate,
rowFields,
colFields,
valueFields,
rowPivotMeta,
colPivotMeta,
} = params;
Expand All @@ -160,19 +189,6 @@ export function getDataPath(params: DataPathParams) {
.join(ID_SEPARATOR);
};

const appendValues = (parent: string) => {
const map = new Map();
valueFields?.forEach((value, level) => {
map.set(value, {
id: generateId(parent, value),
value,
level,
children: new Map(),
});
});
return map;
};

// 根据行、列维度值生成对应的 path 路径,始终将总计小计置于第 0 位,明细数据从第 1 位开始,有两个情况:
// 如果是汇总格子: path = [0, 0, 0, 0] path 中会存在 0 的值
// 如果是明细格子: path = [1, 1, 1] 数字均不为 0
Expand All @@ -186,7 +202,13 @@ export function getDataPath(params: DataPathParams) {
const path = [];
for (let i = 0; i < dimensionValues.length; i++) {
const value = dimensionValues[i];

if (isFirstCreate && currentMeta && !currentMeta?.has(value)) {
const id = dimensionValues
.slice(0, i + 1)
.map((it) => String(it))
.join(ID_SEPARATOR);

const isTotal = value === TOTAL_VALUE;

let level;
Expand All @@ -198,34 +220,29 @@ export function getDataPath(params: DataPathParams) {
level = currentMeta.size + 1;
}

const id = dimensionValues
.slice(0, i + 1)
.map((it) => String(it))
.join(ID_SEPARATOR);

currentMeta.set(value, {
id,
value,
level,
children:
dimensions[i + 1] === EXTRA_FIELD ? appendValues(id) : new Map(),
children: new Map(),
});

onFirstCreate?.({
dimension: dimensions?.[i],
dimensionPath: id,
careRepeated,
});
}

const meta = currentMeta?.get(value);

path.push(isMultiValue(value) ? value : meta?.level);

if (meta) {
if (isFirstCreate && meta.childField !== dimensions?.[i + 1]) {
const childDimension = dimensions?.[i + 1];
if (isFirstCreate && meta.childField !== childDimension) {
// mark the child field
// NOTE: should take more care when reset meta.childField to undefined, the meta info is shared with brother nodes.
meta.childField = dimensions?.[i + 1];
meta.childField = childDimension;
}
currentMeta = meta?.children;
}
Expand All @@ -242,6 +259,7 @@ interface Param {
rows: string[];
columns: string[];
values: string[];
valueInCols: boolean;
data: DataType[];
indexesData: Record<string, DataType[][] | DataType[]>;
sortedDimensionValues: SortedDimensionValues;
Expand All @@ -257,6 +275,7 @@ export function transformIndexesData(params: Param) {
rows,
columns,
values,
valueInCols,
data = [],
indexesData = {},
sortedDimensionValues,
Expand All @@ -273,7 +292,7 @@ export function transformIndexesData(params: Param) {
/**
* 在 PivotMap 创建新节点时,填充 sortedDimensionValues 维度数据
*/
const onFirstCreate = ({ dimension, dimensionPath, careRepeated }) => {
const onFirstCreate = ({ dimension, dimensionPath, careRepeated = true }) => {
if (careRepeated && repeatedDimensionSet.has(dimension)) {
// 当行、列都配置了同一维度字段时,因为 getDataPath 先处理行、再处理列
// 所有重复字段的维度值无需再加入到 sortedDimensionValues
Expand All @@ -292,21 +311,33 @@ export function transformIndexesData(params: Param) {
return;
}

const rowDimensionValues = transformDimensionsValues(item, rows);
const colDimensionValues = transformDimensionsValues(item, columns);
const path = getDataPath({
rowDimensionValues,
colDimensionValues,
rowPivotMeta,
colPivotMeta,
rowFields: rows,
colFields: columns,
valueFields: values,
isFirstCreate: true,
onFirstCreate,
});
paths.push(path);
set(indexesData, path, item);
const multiRowDimensionValues = transformDimensionsValuesWithExtraFields(
item,
rows,
valueInCols ? undefined : values,
);
const multiColDimensionValues = transformDimensionsValuesWithExtraFields(
item,
columns,
valueInCols ? values : undefined,
);

for (const rowDimensionValues of multiRowDimensionValues) {
for (const colDimensionValues of multiColDimensionValues) {
const path = getDataPath({
rowDimensionValues,
colDimensionValues,
rowPivotMeta,
colPivotMeta,
rowFields: rows,
colFields: columns,
isFirstCreate: true,
onFirstCreate,
});
paths.push(path);
set(indexesData, path, item);
}
}
});

return {
Expand Down

0 comments on commit 05808f1

Please sign in to comment.