Skip to content

Commit

Permalink
fix: 处理趋势分析表中不存在任何指标,只包含日期列头的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
wjgogogo committed Nov 29, 2023
1 parent b5ab5ac commit 7871a49
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/s2-core/src/common/constant/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const TOTAL_VALUE = '$$total$$';
export const MULTI_VALUE = '$$multi$$';
export const SERIES_NUMBER_FIELD = '$$series_number$$';
export const EMPTY_FIELD_VALUE = '$$empty_field_value$$';
export const EMPTY_EXTRA_FIELD_PLACEHOLDER = '$$empty_extra_placeholder$$';
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
@@ -1,7 +1,7 @@
import { isNumber } from 'lodash';
import { i18n } from '../../common';
import type { SpreadSheet } from '../../sheet-type';
import { filterTotal } from '../../utils/data-set-operate';
import { filterOutDetail } 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 @@ -43,7 +43,7 @@ export const buildRowTreeHierarchy = (params: TreeHeaderParams) => {
const { query, id: parentId } = parentNode;
const isDrillDownItem = spreadsheet.dataCfg.fields.rows?.length <= level;

const dimValues = filterTotal(Array.from(pivotMeta.keys()));
const dimValues = filterOutDetail(Array.from(pivotMeta.keys()));

let fieldValues: FieldValue[] = layoutArrange(
dimValues,
Expand Down
11 changes: 8 additions & 3 deletions packages/s2-core/src/utils/data-set-operate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { isArray, flattenDeep } from 'lodash';
import { TOTAL_VALUE } from '../common/constant/field';
import {
EMPTY_EXTRA_FIELD_PLACEHOLDER,
TOTAL_VALUE,
} from '../common/constant/field';
import type { Totals, TotalsStatus } from '../common/interface';

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

export const filterTotal = (values: string[] = []) => {
return values.filter((v) => v !== TOTAL_VALUE);
export const filterOutDetail = (values: string[] = []) => {
return values.filter(
(v) => v !== TOTAL_VALUE && v !== EMPTY_EXTRA_FIELD_PLACEHOLDER,
);
};

export const customFlattenDeep = (
Expand Down
29 changes: 21 additions & 8 deletions packages/s2-core/src/utils/dataset/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'lodash';
import type { PickEssential } from '../../common/interface/utils';
import {
EMPTY_EXTRA_FIELD_PLACEHOLDER,
EXTRA_FIELD,
ID_SEPARATOR,
MULTI_VALUE,
Expand Down Expand Up @@ -94,12 +95,22 @@ export function transformDimensionsValuesWithExtraFields(

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

/**
* result 为空时,就是命中了数组置于当前维度,但是当前的数据中并没有包含任何 values 配置,给一个默认维度用于占位
* 主要用于处理趋势分析表中只存在日期列头,不存在任何值的情况:
* ref: @see packages/s2-react/__tests__/unit/components/sheets/strategy-sheet/index-spec.tsx
*/
if (isEmpty(result)) {
result.push(transform(record, dimensions, EMPTY_EXTRA_FIELD_PLACEHOLDER));
}
values.forEach((value) => {
if (value in record) {
result.push(transform(record, dimensions, value));
}
});

return result;
}
Expand Down Expand Up @@ -455,9 +466,11 @@ export function getSatisfiedPivotMetaValues(params: {
function flattenMetaValue(list: PivotMetaValue[], field: string) {
const allValues = flatMap(list, (metaValue) => {
const values = [...metaValue.children.values()];
return queryType === QueryDataType.All
? values
: values.filter((v) => v.value !== TOTAL_VALUE);
return values.filter(
(v) =>
v.value !== EMPTY_EXTRA_FIELD_PLACEHOLDER &&
(queryType === QueryDataType.All ? true : v.value !== TOTAL_VALUE),
);
});
if (list.length > 1) {
// 从不同父维度中获取的子维度需要再排一次,比如province => city 按照字母倒序,那么在获取了所有 province 的 city 后需要再排一次
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import {
import React from 'react';
import ReactDOM from 'react-dom';
import { act } from 'react-dom/test-utils';
import { merge } from 'lodash';
import {
SheetComponent,
SheetComponentOptions,
} from '../../../../../src/components';
import { getContainer, sleep } from '../../../../util/helpers';
import { getContainer } from '../../../../util/helpers';
import {
StrategyOptions,
StrategySheetDataConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export class StrategyDataSet extends CustomTreePivotDataSet {

return {
...updatedDataCfg,

meta: newMeta,
};
}
Expand Down

0 comments on commit 7871a49

Please sign in to comment.