Skip to content

Commit

Permalink
feat: 支持渲染 G2 图表
Browse files Browse the repository at this point in the history
  • Loading branch information
lijinke666 committed Nov 29, 2023
1 parent 7c4da43 commit 99b0f5e
Show file tree
Hide file tree
Showing 38 changed files with 1,512 additions and 167 deletions.
2 changes: 1 addition & 1 deletion packages/s2-core/src/cell/base-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export abstract class BaseCell<T extends SimpleBBox> extends Group {
return resize?.[type];
}

public getBBoxByType(type = CellClipBox.BORDER_BOX) {
public getBBoxByType(type = CellClipBox.BORDER_BOX): SimpleBBox {
const bbox: SimpleBBox = {
x: this.meta.x,
y: this.meta.y,
Expand Down
34 changes: 33 additions & 1 deletion packages/s2-core/src/cell/data-cell.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import type { PointLike } from '@antv/g';
import { find, findLast, first, get, isEmpty, isEqual, merge } from 'lodash';
import {
find,
findLast,
first,
get,
isEmpty,
isEqual,
isObject,
merge,
} from 'lodash';
import { BaseCell } from '../cell/base-cell';
import {
CellType,
Expand All @@ -22,6 +31,8 @@ import type {
TextTheme,
ViewMeta,
ViewMetaIndexType,
MultiData,
BaseChartData,
} from '../common/interface';
import {
getHorizontalTextIconPosition,
Expand Down Expand Up @@ -61,6 +72,27 @@ export class DataCell extends BaseCell<ViewMeta> {
return CellType.DATA_CELL;
}

public isMultiData() {
return isObject(this.getMeta().fieldValue);
}

public getRenderChartData(): BaseChartData {
const { fieldValue } = this.meta;

return (fieldValue as MultiData)?.values as BaseChartData;
}

public getRenderChartOptions() {
const chartData = this.getRenderChartData();
const cellArea = this.getBBoxByType(CellClipBox.CONTENT_BOX);

return {
...cellArea,
...chartData,
autoFit: true,
};
}

protected getBorderPositions(): CellBorderPosition[] {
return [CellBorderPosition.BOTTOM, CellBorderPosition.RIGHT];
}
Expand Down
1 change: 1 addition & 0 deletions packages/s2-core/src/common/constant/events/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export enum S2Event {
DATA_CELL_MOUSE_MOVE = 'data-cell:mouse-move',
DATA_CELL_BRUSH_SELECTION = 'data-cell:brush-selection',
DATA_CELL_SELECT_MOVE = 'data-cell:select-move',
DATA_CELL_RENDER = 'data-cell:render',

/** ================ Corner Cell ================ */
CORNER_CELL_HOVER = 'corner-cell:hover',
Expand Down
13 changes: 3 additions & 10 deletions packages/s2-core/src/common/interface/basic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FederatedPointerEvent as Event, Group } from '@antv/g';
import type { MergedCell } from '../../cell';
import type { FederatedPointerEvent as Event } from '@antv/g';
import type { DataCell, MergedCell } from '../../cell';
import type {
CustomTreeNode,
Data,
Expand Down Expand Up @@ -376,7 +376,7 @@ export type CellCallback<T extends BaseHeaderConfig> = (
headerConfig: T,
) => S2CellType;

export type DataCellCallback = (viewMeta: ViewMeta) => S2CellType;
export type DataCellCallback = (viewMeta: ViewMeta) => DataCell;

export type MergedCellCallback = (
spreadsheet: SpreadSheet,
Expand Down Expand Up @@ -502,13 +502,6 @@ export interface GridInfo {
rows: number[];
}

export type RenderHandler = (
options: Record<string, unknown>,
context: {
group: Group;
},
) => void;

export interface Point {
x: number;
y: number;
Expand Down
1 change: 1 addition & 0 deletions packages/s2-core/src/common/interface/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export interface EmitterType {
[S2Event.DATA_CELL_CONTEXT_MENU]: CanvasEventHandler;
[S2Event.DATA_CELL_BRUSH_SELECTION]: (cells: (DataCell | CellMeta)[]) => void;
[S2Event.DATA_CELL_SELECT_MOVE]: (metas: CellMeta[]) => void;
[S2Event.DATA_CELL_RENDER]: (cell: DataCell) => void;

/** ================ Row Cell ================ */
[S2Event.ROW_CELL_MOUSE_DOWN]: CanvasEventHandler;
Expand Down
6 changes: 6 additions & 0 deletions packages/s2-core/src/common/interface/s2DataConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ export interface BaseChartData {
* 类型
*/
type: MiniChartTypes;

/**
* 数据
*/
data: RawData[];

/**
* 坐标轴数据
*/
encode?: {
x: keyof RawData;
y: keyof RawData;
};

[key: string]: unknown;
}

/* 子弹图数据结构 */
Expand All @@ -29,10 +33,12 @@ export interface BulletValue {
* 类型
*/
type: MiniChartTypes.Bullet;

/**
* 当前值
*/
measure: number | string;

/**
* 目标值
*/
Expand Down
38 changes: 22 additions & 16 deletions packages/s2-core/src/facet/base-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1275,54 +1275,60 @@ export abstract class BaseFacet {
this.columnHeader.onColScroll(scrollX, KEY_GROUP_COL_RESIZE_AREA);
}

addCell = (cell: S2CellType<ViewMeta>) => {
addDataCell = (cell: DataCell) => {
this.panelScrollGroup?.appendChild(cell);
this.spreadsheet.emit(S2Event.DATA_CELL_RENDER, cell);
this.spreadsheet.emit(S2Event.LAYOUT_CELL_MOUNTED, cell);
};

realCellRender = (scrollX: number, scrollY: number) => {
realDataCellRender = (scrollX: number, scrollY: number) => {
const indexes = this.calculateXYIndexes(scrollX, scrollY);

DebuggerUtil.getInstance().logger(
'realCellRender:',
'realDataCellRender:',
this.preCellIndexes,
indexes,
);
const { add, remove } = diffPanelIndexes(this.preCellIndexes!, indexes);
const { add: willAddDataCells, remove: willRemoveDataCells } =
diffPanelIndexes(this.preCellIndexes!, indexes);

DebuggerUtil.getInstance().debugCallback(DEBUG_VIEW_RENDER, () => {
// add new cell in panelCell
each(add, ([i, j]) => {
each(willAddDataCells, ([i, j]) => {
const viewMeta = this.getCellMeta(j, i);

if (viewMeta) {
const cell = this.spreadsheet.options.dataCell?.(viewMeta)!;

// mark cell for removing
cell.name = `${i}-${j}`;
this.addCell(cell);
this.addDataCell(cell);
}
});
const allCells = getAllChildCells(
this.panelGroup.children,
const allDataCells = getAllChildCells<DataCell>(
this.panelGroup.children as DataCell[],
DataCell,
) as DataCell[];
);

// remove cell from panelCell
each(remove, ([i, j]) => {
const findOne = find(allCells, (cell) => cell.name === `${i}-${j}`);
each(willRemoveDataCells, ([i, j]) => {
const mountedDataCell = find(
allDataCells,
(cell) => cell.name === `${i}-${j}`,
);

findOne?.remove();
mountedDataCell?.remove();
});

DebuggerUtil.getInstance().logger(
`Render Cell Panel: ${allCells?.length}, Add: ${add?.length}, Remove: ${remove?.length}`,
`Render Cell Panel: ${allDataCells?.length}, Add: ${willAddDataCells?.length}, Remove: ${willRemoveDataCells?.length}`,
);
});

this.preCellIndexes = indexes;
this.spreadsheet.emit(S2Event.LAYOUT_AFTER_REAL_DATA_CELL_RENDER, {
add,
remove,
add: willAddDataCells,
remove: willRemoveDataCells,
spreadsheet: this.spreadsheet,
});
};
Expand Down Expand Up @@ -1549,7 +1555,7 @@ export abstract class BaseFacet {
this.spreadsheet.hideTooltip();
this.spreadsheet.interaction.clearHoverTimer();

this.realCellRender(scrollX, scrollY);
this.realDataCellRender(scrollX, scrollY);
this.updatePanelScrollGroup();
this.translateRelatedGroups(scrollX, scrollY, rowHeaderScrollX);
if (!skipScrollEvent) {
Expand Down
4 changes: 2 additions & 2 deletions packages/s2-core/src/facet/pivot-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
import { EXTRA_FIELD, LayoutWidthTypes, VALUE_FIELD } from '../common/constant';
import { CellType } from '../common/constant/interaction';
import { DebuggerUtil } from '../common/debug';
import type { LayoutResult } from '../common/interface';
import type { LayoutResult, SimpleData } from '../common/interface';
import type { PivotDataSet } from '../data-set/pivot-data-set';
import type { SpreadSheet } from '../sheet-type';
import { safeJsonParse } from '../utils';
Expand Down Expand Up @@ -763,7 +763,7 @@ export class PivotFacet extends BaseFacet {
for (let j = 0; j < cellDataKeys.length; j++) {
const dataValue: MultiData = get(cellData, cellDataKeys[j]);

const valueSize = size(get(dataValue?.values, '0'));
const valueSize = size(get(dataValue?.values as SimpleData[][], '0'));

if (valueSize > maxLength) {
// greater length
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/facet/table-facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ export class TableFacet extends BaseFacet {
};
};

addCell = (cell: S2CellType<ViewMeta>) => {
addDataCell = (cell: S2CellType<ViewMeta>) => {
const {
rowCount: frozenRowCount = 0,
colCount: frozenColCount = 0,
Expand Down
2 changes: 1 addition & 1 deletion packages/s2-core/src/utils/cell/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export const getCellBoxByType = (
borderPositions: CellBorderPosition[],
cellStyle: CellTheme,
boxType: CellClipBox,
) => {
): SimpleBBox => {
if (boxType === CellClipBox.BORDER_BOX) {
return bbox;
}
Expand Down
30 changes: 0 additions & 30 deletions packages/s2-core/src/utils/custom-render.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/s2-core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export * from './export/index';
export * from './export/copy';
export * from './interaction';
export * from './g-renders';
export * from './custom-render';
export * from './g-mini-charts';
export * from './merge';
export * from './is-mobile';
Expand Down
Loading

0 comments on commit 99b0f5e

Please sign in to comment.