Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复维度缺失部分维值时, 行维值以及对应的数值展示错误 #2436

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 246 additions & 0 deletions packages/s2-core/__tests__/spreadsheet/miss-dimension-values-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import { getContainer } from 'tests/util/helpers';
import { EMPTY_FIELD_VALUE, type S2DataConfig, type S2Options } from '@/common';
import { PivotSheet, SpreadSheet } from '@/sheet-type';

const s2Options: S2Options = {
debug: true,
width: 600,
height: 400,
hierarchyType: 'grid',
totals: {
row: {
showGrandTotals: false,
showSubTotals: {
always: false,
},
reverseLayout: false,
reverseSubLayout: false,
subTotalsDimensions: [
'2d7feabd-76a2-4c11-8f24-79764af936b4',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec',
],
},
col: {
showGrandTotals: false,
showSubTotals: false,
reverseLayout: false,
reverseSubLayout: false,
subTotalsDimensions: [],
},
},
style: {
layoutWidthType: 'adaptive',
cellCfg: {
height: 30,
},
},
showDefaultHeaderActionIcon: false,
};

const testDataCfg: S2DataConfig = {
meta: [
{
field: '2d7feabd-76a2-4c11-8f24-79764af936b4',
name: '一级维度',
},
{
field: '30b4b32d-d69a-4772-b7f9-84cd54cf0cec',
name: '二级维度',
},
{
field: 'c5ce4e54-795a-42b3-9cc8-e8b685da44ee',
name: '数值',
},
],
fields: {
rows: [
'2d7feabd-76a2-4c11-8f24-79764af936b4',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec',
],
columns: [],
values: ['c5ce4e54-795a-42b3-9cc8-e8b685da44ee'],
valueInCols: true,
},
data: [
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '总计',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 1732771,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '维值-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '维值-2',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 172245,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '维值-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '维值-2',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 12222,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '维值-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '维值-3',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 11111,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '维值-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '维值-3',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 11111,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '维值-1',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 456,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-2',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 12,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-2',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 4444567,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-3',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 111233,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-3',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 785222,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-4',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 6455644,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-4',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 289898,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-5',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 2222,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-1',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-5',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 1111,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-1',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 125555,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-6',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-x',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 409090,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-6',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-x',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 111111,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-6',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-7',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 5555,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-6',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-7',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 67878,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-6',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-8',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 53445.464,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-6',
'30b4b32d-d69a-4772-b7f9-84cd54cf0cec': '测试-8',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 456.464,
},
{
'2d7feabd-76a2-4c11-8f24-79764af936b4': '测试-6',
'c5ce4e54-795a-42b3-9cc8-e8b685da44ee': 123.416,
},
],
};

describe('Miss Dimension Values Tests', () => {
let s2: SpreadSheet;

beforeEach(() => {
s2 = new PivotSheet(getContainer(), testDataCfg, s2Options);
s2.render();
});

test('should get correctly empty dimension values', () => {
const emptyDimensionValueNode = s2.getRowNodes()[0].children[0];

expect(emptyDimensionValueNode.value).toEqual(EMPTY_FIELD_VALUE);
expect(emptyDimensionValueNode.id).toEqual(
`root[&]总计[&]${EMPTY_FIELD_VALUE}`,
);
expect(emptyDimensionValueNode.belongsCell.getActualText()).toEqual('-');
});

test('should get correctly empty dimension values and use custom placeholder text', () => {
const placeholder = '*';

s2.setOptions({
placeholder,
});
s2.render(false);

const emptyDimensionValueNode = s2.getRowNodes()[0].children[0];
expect(emptyDimensionValueNode.belongsCell.getActualText()).toEqual(
placeholder,
);
});

test('should get correctly dimension data and ignore empty dimension value', () => {
const emptyDimensionValueNode = s2.getRowNodes()[0].children[0];

const data = s2.dataSet.getMultiData(emptyDimensionValueNode.query);
const dimensionValues = s2.dataSet.getDimensionValues(
emptyDimensionValueNode.field,
);
const emptyDimensionDataCell =
s2.interaction.getPanelGroupAllDataCells()[0];

expect(emptyDimensionValueNode.query).toEqual(
emptyDimensionValueNode.parent.query,
);
expect(emptyDimensionDataCell.getMeta().fieldValue).toEqual(1732771);
expect(data).toMatchInlineSnapshot(`
Array [
Array [
Object {
"$$extra$$": "c5ce4e54-795a-42b3-9cc8-e8b685da44ee",
"$$value$$": 1732771,
"2d7feabd-76a2-4c11-8f24-79764af936b4": "总计",
"c5ce4e54-795a-42b3-9cc8-e8b685da44ee": 1732771,
},
],
]
`);
expect(dimensionValues).toMatchInlineSnapshot(`
Array [
"维值-2",
"维值-3",
"测试-2",
"测试-3",
"测试-4",
"测试-5",
"测试-x",
"测试-7",
"测试-8",
]
`);
});
});
8 changes: 7 additions & 1 deletion packages/s2-core/src/cell/base-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from 'lodash';
import {
CellTypes,
EMPTY_FIELD_VALUE,
InteractionStateName,
SHAPE_ATTRS_MAP,
SHAPE_STYLE_MAP,
Expand Down Expand Up @@ -135,7 +136,12 @@ export abstract class BaseCell<T extends SimpleBBox> extends Group {
}

public getFieldValue() {
return this.getFormattedFieldValue().formattedValue;
const { formattedValue } = this.getFormattedFieldValue();

if (formattedValue === EMPTY_FIELD_VALUE) {
return this.getActualText();
}
return formattedValue;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/s2-core/src/cell/header-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
this.hasDefaultHiddenIcon = false;
}

public getTreeIcon(): GuiIcon {
return this.treeIcon;
}

protected getInteractiveBorderShapeStyle(border: number) {
const { x, y, height, width } = this.getCellArea();
return {
Expand Down Expand Up @@ -101,7 +105,7 @@
} else if (this.spreadsheet.isHierarchyTreeType()) {
shouldFormat = !(isGrandTotals && isTotalRoot);
} else {
shouldFormat = !isTotalRoot;

Check warning on line 108 in packages/s2-core/src/cell/header-cell.ts

View check run for this annotation

Codecov / codecov/patch

packages/s2-core/src/cell/header-cell.ts#L108

Added line #L108 was not covered by tests
}

const formattedValue =
Expand Down
3 changes: 2 additions & 1 deletion packages/s2-core/src/cell/row-cell.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Point } from '@antv/g-canvas';
import { GM } from '@antv/g-gesture';
import { find, get } from 'lodash';
import { find, get, isEmpty } from 'lodash';
import type { SimpleBBox } from '@antv/g-canvas';
import {
CellTypes,
Expand Down Expand Up @@ -130,6 +130,7 @@
) {
return;
}

Check warning on line 133 in packages/s2-core/src/cell/row-cell.ts

View check run for this annotation

Codecov / codecov/patch

packages/s2-core/src/cell/row-cell.ts#L133

Added line #L133 was not covered by tests
return get(this.meta, 'parent.belongsCell.treeIcon.cfg');
}

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 @@ -4,6 +4,7 @@ export const EXTRA_FIELD = '$$extra$$';
export const EXTRA_COLUMN_FIELD = '$$extra_column$$';
export const TOTAL_VALUE = '$$total$$';
export const SERIES_NUMBER_FIELD = '$$series_number$$';
export const EMPTY_FIELD_VALUE = '$$empty_field_value$$';

export const BACK_GROUND_GROUP_CONTAINER_Z_INDEX = 0;

Expand Down
18 changes: 8 additions & 10 deletions packages/s2-core/src/facet/layout/build-gird-hierarchy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isEmpty, isUndefined } from 'lodash';
import { EXTRA_FIELD } from '../../common/constant';
import { EMPTY_FIELD_VALUE, EXTRA_FIELD } from '../../common/constant';
import { addTotals } from '../../utils/layout/add-totals';
import { generateHeaderNodes } from '../../utils/layout/generate-header-nodes';
import { getDimsCondition } from '../../utils/layout/get-dims-condition-by-node';
Expand All @@ -23,16 +23,15 @@

const { dataSet, values, spreadsheet } = facetCfg;
const fieldValues: FieldValue[] = [];
const fieldName = dataSet.getFieldName(currentField);

let query = {};
let query: Record<string, unknown> = {};
const totalsConfig = spreadsheet.getTotalsConfig(currentField);
const dimensionGroup = parentNode.isGrandTotals
? totalsConfig.totalsGroupDimensions
: totalsConfig.subTotalsGroupDimensions;
if (dimensionGroup?.includes(currentField)) {
query = getDimsCondition(parentNode);
const dimValues = dataSet.getTotalDimensionValues(currentField, query);

Check warning on line 34 in packages/s2-core/src/facet/layout/build-gird-hierarchy.ts

View check run for this annotation

Codecov / codecov/patch

packages/s2-core/src/facet/layout/build-gird-hierarchy.ts#L34

Added line #L34 was not covered by tests
fieldValues.push(
...(dimValues || []).map(
(value) =>
Expand All @@ -44,15 +43,15 @@
}),
),
);
if (isEmpty(fieldValues)) {
fieldValues.push(fieldName);
if (isEmpty(fieldValues) && currentField) {
fieldValues.push(EMPTY_FIELD_VALUE);
}
} else if (addTotalMeasureInTotal && currentField === EXTRA_FIELD) {
// add total measures
query = getDimsCondition(parentNode);
fieldValues.push(...values.map((v) => new TotalMeasure(v)));
} else if (whetherLeafByLevel({ facetCfg, level: index, fields })) {
// 如果最后一级没有分组维度,则将上一个结点设为叶子结点
// 如果最后一级没有分组维度,则将上一个结点设为叶子节点
parentNode.isLeaf = true;
hierarchy.pushIndexNode(parentNode);
parentNode.rowIndex = hierarchy.getIndexNodes().length - 1;
Expand Down Expand Up @@ -80,9 +79,8 @@

const { dataSet, spreadsheet } = facetCfg;
const fieldValues: FieldValue[] = [];
const fieldName = dataSet.getFieldName(currentField);

let query = {};
let query: Record<string, unknown> = {};

// field(dimension)'s all values
query = getDimsCondition(parentNode, true);
Expand All @@ -99,11 +97,11 @@

// add skeleton for empty data

if (isEmpty(fieldValues)) {
if (isEmpty(fieldValues) && currentField) {
if (currentField === EXTRA_FIELD) {
fieldValues.push(...dataSet.fields?.values);
} else {
fieldValues.push(fieldName);
fieldValues.push(EMPTY_FIELD_VALUE);
lijinke666 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading
Loading