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: 修复在明细表中绘制 G2 图表, 点击单元格报错 close #2843 #2864

Merged
merged 1 commit into from
Aug 23, 2024
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
37 changes: 37 additions & 0 deletions packages/s2-core/__tests__/unit/utils/tooltip-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,43 @@ describe('Tooltip Utils Tests', () => {
`);
});
});

// https://github.com/antvis/S2/issues/2843
test('should get empty cell name for custom chart shape', () => {
s2 = createFakeSpreadSheet();

s2.dataSet = {
getFieldFormatter: () => {},
getCustomFieldDescription: () => {},
getFieldName: () => {
return {
values: {
test: '1',
},
};
},
};

s2.facet = {
getRowLeafNodes: () => [],
getColLeafNodes: () => [],
} as unknown as BaseFacet;

const cell = createMockCellInfo('test-a');

const tooltipData = getTooltipData({
cellInfos: [],
options: {
enableFormat: true,
hideSummary: true,
onlyShowCellText: true,
},
targetCell: cell.mockCell,
spreadsheet: s2,
});

expect(tooltipData.name).toEqual('');
});
});

test('should set container style', () => {
Expand Down
9 changes: 5 additions & 4 deletions packages/s2-core/src/utils/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
isFunction,
isNumber,
isObject,
isString,
last,
map,
mapKeys,
Expand Down Expand Up @@ -563,15 +564,14 @@ export const getTooltipData = (params: TooltipDataParam): TooltipData => {
const firstCellInfo = (cellInfos[0] || {}) as unknown as ViewMetaData;

if (!options?.hideSummary) {
// 计算多项的sum(默认为sum,可自定义)
// 计算多项的 sum(默认为 sum,可自定义)
summaries = getSummaries({
spreadsheet,
options,
targetCell,
});
} else if (options.onlyShowCellText) {
// 行列头hover & 明细表所有hover

// 行列头 hover & 明细表所有 hover
const value = CellData.getFieldValue(firstCellInfo, 'value') as string;
const valueField = CellData.getFieldValue(
firstCellInfo,
Expand All @@ -584,7 +584,8 @@ export const getTooltipData = (params: TooltipDataParam): TooltipData => {
? spreadsheet.dataSet.getFieldName(value) || formattedValue
Copy link
Member Author

Choose a reason for hiding this comment

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

该场景会兜底到 formattedValue, value 是 G2 的 Spec, 这种情况应该展示为空字符串

: spreadsheet.dataSet.getFieldName(valueField);

name = cellText || '';
// https://github.com/antvis/S2/issues/2843
name = isString(cellText) ? cellText : '';
} else {
headInfo = getHeadInfo(spreadsheet, firstCellInfo, options);
details = getTooltipDetailList(
Expand Down
17 changes: 15 additions & 2 deletions s2-site/docs/manual/advanced/chart-in-cell.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,20 @@ import { renderToMountedElement } from '@antv/g2';

#### 2.3 在 `@antv/s2` 中使用

##### 1. 自定义 `DataCell`, 如果是图表数据,则不渲染默认的文本
##### 1. 自定义 `DataCell/TableDataCell`, 如果是图表数据,则不渲染默认的文本

:::info{title="提示"}
如果是**明细表**, 需要继承 `TableDataCell`

```diff
- import { DataCell } from '@antv/s2';
+ import { TableDataCell } from '@antv/s2';

- class ChartSheetDataCell extends DataCell {}
+ class ChartSheetDataCell extends TableDataCell {}
```

:::

```ts
import { PivotSheet, DataCell } from '@antv/s2';
Expand All @@ -357,7 +370,7 @@ await s2.render();

##### 2. 监听数值单元格渲染完成后,使用 `G2` 提供的 `renderToMountedElement` 将图表挂载在 `S2` 单元格实例上

:::warning{title="提示"}
:::info{title="提示"}
由于 `G2` 按需加载的特性,请根据你渲染的图表,自行选择适合的 [`library`](https://g2.antv.antgroup.com/manual/extra-topics/bundle#g2stdlib)
:::

Expand Down
Loading