Skip to content

Commit

Permalink
feat: 交叉表行头叶子节点支持斑马纹风格 (#2332)
Browse files Browse the repository at this point in the history
* feat: 行头叶子节点支持crossBackgroundColor

* feat: 行头叶子节点支持crossBackgroundColor

* refactor: 函数命名getCrossBackgroundColor、测试用例使用createPivotSheet

---------

Co-authored-by: huiyu.zjt <[email protected]>
  • Loading branch information
Alexzjt and huiyu.zjt authored Sep 8, 2023
1 parent e0d3de8 commit 5320fbf
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 10 deletions.
33 changes: 33 additions & 0 deletions packages/s2-core/__tests__/unit/cell/row-cell-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,37 @@ describe('Row Cell Tests', () => {
expect(get(rowCell, 'backgroundShape.attrs.fill')).toEqual('#F7B46F');
});
});

describe('Cross Background Color Tests', () => {
const s2 = createPivotSheet({
width: 800,
height: 600,
});
const crossColor = '#FFFFFF';
const defaultColor = '#F5F8FF';
const cellColorConfig = {
crossBackgroundColor: crossColor,
backgroundColor: defaultColor,
};
s2.setTheme({
rowCell: {
cell: cellColorConfig,
},
dataCell: {
cell: cellColorConfig,
},
});
s2.render();
test('should draw right condition background shape', () => {
const rowCell0 = s2.facet.rowHeader.getChildByIndex(0);
const rowCell1 = s2.facet.rowHeader.getChildByIndex(1);
const rowCell2 = s2.facet.rowHeader.getChildByIndex(2);
expect(get(rowCell0, 'actualText')).toEqual('浙江');
expect(get(rowCell0, 'backgroundShape.attrs.fill')).toEqual(defaultColor);
expect(get(rowCell1, 'actualText')).toEqual('义乌');
expect(get(rowCell1, 'backgroundShape.attrs.fill')).toEqual(crossColor);
expect(get(rowCell2, 'actualText')).toEqual('杭州');
expect(get(rowCell2, 'backgroundShape.attrs.fill')).toEqual(defaultColor);
});
});
});
16 changes: 16 additions & 0 deletions packages/s2-core/src/cell/base-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,20 @@ export abstract class BaseCell<T extends SimpleBBox> extends Group {
}
return fillResult;
}

protected getCrossBackgroundColor(rowIndex: number) {
const { crossBackgroundColor, backgroundColorOpacity } =
this.getStyle().cell;

if (crossBackgroundColor && rowIndex % 2 === 0) {
// 隔行颜色的配置
// 偶数行展示灰色背景,因为index是从0开始的
return { backgroundColorOpacity, backgroundColor: crossBackgroundColor };
}

return {
backgroundColorOpacity,
backgroundColor: this.getStyle().cell.backgroundColor,
};
}
}
16 changes: 6 additions & 10 deletions packages/s2-core/src/cell/data-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,12 @@ export class DataCell extends BaseCell<ViewMeta> {
}

public getBackgroundColor() {
const { crossBackgroundColor, backgroundColorOpacity } =
this.getStyle().cell;

let backgroundColor = this.getStyle().cell.backgroundColor;

if (crossBackgroundColor && this.meta.rowIndex % 2 === 0) {
// 隔行颜色的配置
// 偶数行展示灰色背景,因为index是从0开始的
backgroundColor = crossBackgroundColor;
}
const backgroundColorByCross = this.getCrossBackgroundColor(
this.meta.rowIndex,
);
let backgroundColor = backgroundColorByCross.backgroundColor;
const backgroundColorOpacity =
backgroundColorByCross.backgroundColorOpacity;

if (this.shouldHideRowSubtotalData()) {
return { backgroundColor, backgroundColorOpacity };
Expand Down
10 changes: 10 additions & 0 deletions packages/s2-core/src/cell/header-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,16 @@ export abstract class HeaderCell extends BaseCell<Node> {
public getBackgroundColor() {
const { backgroundColor, backgroundColorOpacity } =
this.getStyle()?.cell || {};
return this.getBackgroundColorByCondition(
backgroundColor,
backgroundColorOpacity,
);
}

protected getBackgroundColorByCondition(
backgroundColor: string,
backgroundColorOpacity: number,
) {
let fill = backgroundColor;
// get background condition fill color
const bgCondition = this.findFieldCondition(this.conditions?.background);
Expand Down
9 changes: 9 additions & 0 deletions packages/s2-core/src/cell/row-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ export class RowCell extends HeaderCell {
this.update();
}

public getBackgroundColor() {
const { backgroundColor, backgroundColorOpacity } =
this.getCrossBackgroundColor(this.meta.rowIndex);
return this.getBackgroundColorByCondition(
backgroundColor,
backgroundColorOpacity,
);
}

/**
* 绘制hover悬停,刷选的外框
*/
Expand Down

1 comment on commit 5320fbf

@vercel
Copy link

@vercel vercel bot commented on 5320fbf Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

antvis-s2 – ./

antvis-s2.vercel.app
antvis-s2-git-master-antv-s2.vercel.app
antvis-s2-antv-s2.vercel.app

Please sign in to comment.