From 0a6fe65d9feb4077d3962515975009560152ae01 Mon Sep 17 00:00:00 2001 From: lijinke666 Date: Fri, 16 Aug 2024 13:56:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9C=A8=E6=98=8E?= =?UTF-8?q?=E7=BB=86=E8=A1=A8=E4=B8=AD=E7=BB=98=E5=88=B6=20G2=20=E5=9B=BE?= =?UTF-8?q?=E8=A1=A8,=20=E7=82=B9=E5=87=BB=E5=8D=95=E5=85=83=E6=A0=BC?= =?UTF-8?q?=E6=8A=A5=E9=94=99=20close=20#2843?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/unit/utils/tooltip-spec.ts | 37 +++++++++++++++++++ packages/s2-core/src/utils/tooltip.ts | 9 +++-- .../docs/manual/advanced/chart-in-cell.zh.md | 17 ++++++++- 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/packages/s2-core/__tests__/unit/utils/tooltip-spec.ts b/packages/s2-core/__tests__/unit/utils/tooltip-spec.ts index 18f0c8f1a8..2503d8ebc6 100644 --- a/packages/s2-core/__tests__/unit/utils/tooltip-spec.ts +++ b/packages/s2-core/__tests__/unit/utils/tooltip-spec.ts @@ -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', () => { diff --git a/packages/s2-core/src/utils/tooltip.ts b/packages/s2-core/src/utils/tooltip.ts index 2763aea0da..3232d595fa 100644 --- a/packages/s2-core/src/utils/tooltip.ts +++ b/packages/s2-core/src/utils/tooltip.ts @@ -19,6 +19,7 @@ import { isFunction, isNumber, isObject, + isString, last, map, mapKeys, @@ -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, @@ -584,7 +584,8 @@ export const getTooltipData = (params: TooltipDataParam): TooltipData => { ? spreadsheet.dataSet.getFieldName(value) || formattedValue : 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( diff --git a/s2-site/docs/manual/advanced/chart-in-cell.zh.md b/s2-site/docs/manual/advanced/chart-in-cell.zh.md index 5bdc8baa68..a2fd05ee13 100644 --- a/s2-site/docs/manual/advanced/chart-in-cell.zh.md +++ b/s2-site/docs/manual/advanced/chart-in-cell.zh.md @@ -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'; @@ -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) :::