Skip to content

Commit

Permalink
feat: 优化 ViewMeta 类型定义和相关文档 (#2935)
Browse files Browse the repository at this point in the history
* feat: 优化 ViewMeta 类型定义和相关文档

* docs: 补充 facet api 迁移文档
  • Loading branch information
lijinke666 authored Oct 25, 2024
1 parent a426a46 commit 55b6f55
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 83 deletions.
2 changes: 1 addition & 1 deletion packages/s2-core/src/common/interface/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ export interface ViewMeta {
height: number;

/** 单元格数据 */
data: ViewMetaData | SimpleData | undefined;
data: ViewMetaData | undefined;

/** 行索引 */
rowIndex: number;
Expand Down
3 changes: 2 additions & 1 deletion packages/s2-core/src/data-set/pivot-data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import type {
S2DataConfig,
SimpleData,
ViewMeta,
ViewMetaData,
} from '../common/interface';
import { Node } from '../facet/layout/node';
import { resolveNillString } from '../utils';
Expand Down Expand Up @@ -439,7 +440,7 @@ export class PivotDataSet extends BaseDataSet {
}
}

public getCellData(params: GetCellDataParams) {
public getCellData(params: GetCellDataParams): ViewMetaData | undefined {
const { query = {}, rowNode, isTotals = false, totalStatus } = params || {};

const { rows: originRows, columns } = this.fields;
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/pivot-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export class PivotFacet extends FrozenFacet {
rowQuery!,
colQuery!,
);
const data = dataSet.getCellData({
const data = (dataSet as PivotDataSet).getCellData({
query: dataQuery,
rowNode: row,
isTotals,
Expand Down
5 changes: 3 additions & 2 deletions packages/s2-core/src/facet/table-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { DebuggerUtil } from '../common/debug';
import type {
CellCallbackParams,
Data,
DataItem,
FilterParam,
LayoutResult,
Expand Down Expand Up @@ -376,7 +377,7 @@ export class TableFacet extends FrozenFacet {
if (options.seriesNumber?.enable && colNode.field === SERIES_NUMBER_FIELD) {
data = rowIndex + 1;
} else {
data = dataSet.getCellData({
data = (dataSet as TableDataSet).getCellData({
query: {
field: colNode.field,
rowIndex,
Expand All @@ -397,7 +398,7 @@ export class TableFacet extends FrozenFacet {
height: cellHeight,
data: {
[colNode.field]: data,
} as unknown as SimpleData,
} as unknown as Data,
rowIndex,
colIndex,
isTotals: false,
Expand Down
14 changes: 7 additions & 7 deletions s2-site/docs/api/basic-class/base-data-set.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ s2.dataSet.getFieldName('type')
### Formatter

```ts
type Formatter = (
v: unknown,
data?: SimpleData | ViewMetaData | ViewMetaData[],
meta?: Node | ViewMeta,
) => string;
type Formatter = (
value: DataItem,
data?: DataItem | ViewMetaData | ViewMetaData[],
meta?: Node | ViewMeta | null | undefined,
) => SimpleData;
```

### FormatResult

```ts
interface FormatResult {
formattedValue: string;
interface FormatResult {
formattedValue: SimpleData;
value: DataItem;
}
```
Expand Down
70 changes: 2 additions & 68 deletions s2-site/docs/api/general/S2Options.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const s2Options = {

<embed src="@/docs/common/style.zh.md"></embed>

<embed src="@/docs/common/view-meta.zh.md"></embed>

## DataCellCallback

```js | pure
Expand All @@ -78,8 +80,6 @@ DataCellCallback = (viewMeta: ViewMeta, s2: Spreadsheet) => G.Group;

功能描述:自定义数值单元格。[查看示例](/examples/custom/custom-cell#data-cell)

<embed src="@/docs/common/view-meta.zh.md"></embed>

## CellCallback

```js | pure
Expand All @@ -102,8 +102,6 @@ DataCellCallback = (s2: Spreadsheet, cells: S2CellType[], viewMeta: ViewMeta) =>

功能描述:自定义合并单元格。[查看示例](/examples/custom/custom-cell/#custom-merged-cell)

<embed src="@/docs/common/view-meta.zh.md"></embed>

## CornerHeaderCallback

```js | pure
Expand Down Expand Up @@ -142,70 +140,6 @@ CornerHeaderCallback = (parent: S2CellType, spreadsheet: SpreadSheet, ...restOpt

<embed src="@/docs/common/custom/customSvgIcons.zh.md"></embed>

## DataItem

功能描述:基本数据格式

```ts
export enum MiniChartType {
Line = 'line',
Bar = 'bar',
Bullet = 'bullet',
}

export interface MultiData {
label?: string;
values: SimpleData[][];
originalValues?: SimpleData[][]
}

export type SimpleData = string | number;

export interface BaseChartData {
type: MiniChartType;
data: RawData[];
encode?: {
x: keyof RawData;
y: keyof RawData;
};
[key: string]: unknown;
}

export interface BulletValue {
type: MiniChartType.Bullet;
measure: number | string;
target: number | string;
[key: string]: unknown;
}

export type MiniChartData = BaseChartData | BulletValue;

export interface MultiData<T = SimpleData[][] | MiniChartData> {
values: T;
originalValues?: T;
label?: string;
[key: string]: unknown;
}

export type SimpleData = string | number | null;

export type DataItem =
| SimpleData
| MultiData
| Record<string, unknown>
| undefined
| null;

export type RawData = Record<string, DataItem>;

export type ExtraData = {
[EXTRA_FIELD]: string;
[VALUE_FIELD]: string | DataItem;
};

export type Data = RawData & ExtraData;
```

## LayoutResult

功能描述:基本数据格式。[查看文档](/manual/advanced/get-cell-data#%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E5%8C%BA%E5%9F%9F%E5%8D%95%E5%85%83%E6%A0%BC%E8%8A%82%E7%82%B9)
Expand Down
2 changes: 1 addition & 1 deletion s2-site/docs/common/view-meta.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ order: 6
| y | `number` | | | 单元格 y 坐标 |
| width | `number` | | | 单元格宽度 |
| height | `number` | | | 单元格高度 |
| data | [ViewMetaData](#viewmetadata) \| [SimpleData](#simpledata) | | | 单元格原始数据度量 |
| data | [ViewMetaData](#viewmetadata) | | | 单元格数据 |
| rowIndex | `number` | | | 单元格在行叶子节点中的索引 |
| colIndex | `number` | | | 单元格在列叶子节点中的索引 |
| valueField | `string` | | | 度量 id |
Expand Down
18 changes: 16 additions & 2 deletions s2-site/docs/manual/migration-v2.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,15 +584,29 @@ export interface LayoutResult {
}
```

5.`s2.getContentHeight()` 废弃,移动到 `s2.facet.getContentHeight()`
5.`s2.getContentHeight()` 废弃,移动到 `s2.facet.getContentHeight()`

```diff
- s2.getContentHeight()
+ s2.facet.getContentHeight()
+ s2.facet.getContentWidth()
```

具体请查看 [获取单元格数据](/manual/advanced/get-cell-data) 相关文档。
6. 获取布局节点相关 API,移动至 `s2.facet` 命名空间下。并新增丰富的 [语法糖](/api/basic-class/base-facet).

```diff
- s2.getRowNodes()
- s2.getRowLeafNodes()
- s2.getColumnLeafNodes()
- s2.getColumnNodes()

+ s2.facet.getRowNodes()
+ s2.facet.getRowLeafNodes()
+ s2.facet.getColLeafNodes()
+ s2.facet.getColNodes()
```

具体请查看 [获取单元格数据](/manual/advanced/get-cell-data)[BaseFacet](/api/basic-class/base-facet) 相关文档。

#### 渲染参数变更

Expand Down

0 comments on commit 55b6f55

Please sign in to comment.