Skip to content

Commit

Permalink
fix: type&annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfu1 committed Nov 16, 2023
1 parent 1798094 commit 8d2202c
Show file tree
Hide file tree
Showing 57 changed files with 244 additions and 329 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ export class BidirectionalBarAxisText {
}
public transformLabelStyle(style) {
const removeLabel = {};
const reg = /^label[A-Z]/;
Object.keys(style).forEach((key) => {
if (key.startsWith('label')) {
if (reg.test(key)) {
removeLabel[key.replace('label', '').replace(/^[A-Z]/, (match) => match.toLowerCase())] = style[key];
}
});
Expand All @@ -86,10 +87,7 @@ export class BidirectionalBarAxisText {

public drawBidirectionalBarAxisText() {
const axisLayout = this.getBidirectionalBarAxisTextLayout();
const {
x: { labelFormatter, ...textStyle },
layout: viewLayout,
} = this.options;
const { layout: viewLayout, labelFormatter, ...textStyle } = this.options;

axisLayout.forEach((layout) => {
const { x, y, text } = layout;
Expand Down
7 changes: 5 additions & 2 deletions packages/plots/src/core/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const SOURCE_ATTRIBUTE_NAME = 'data-chart-source-type';

const ANNOTATION_MAP = new Map();

export abstract class Plot<O extends Options> extends EE {
type PickOptions = Pick<Options, 'autoFit' | 'width' | 'height'>;
export abstract class Plot<O extends PickOptions> extends EE {
/** plot 类型名称 */
public abstract readonly type: string;
/** plot 绘制的 dom */
Expand Down Expand Up @@ -90,7 +91,7 @@ export abstract class Plot<O extends Options> extends EE {
/**
* 获取默认的 options 配置项,每个组件都可以复写
*/
protected getDefaultOptions(): Partial<Options> | void {}
protected getDefaultOptions(): any {}

/**
* 绘制
Expand All @@ -100,6 +101,8 @@ export abstract class Plot<O extends Options> extends EE {
if (this.type !== 'base') {
this.execAdaptor();
}
console.log(this.getSpecOptions());

// options 转换
this.chart.options(this.getSpecOptions());
// 渲染
Expand Down
8 changes: 4 additions & 4 deletions packages/plots/src/core/components/coordinateLayout.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Adaptor } from '../types';
import type { Adaptor, Options } from '../types';

/**
* layout = 'horizontal' | 'vertical'
* @param params
* @returns
* @param params
* @returns
*/
export function coordinateLayout<P extends Adaptor>(params: P) {
export function coordinateLayout<P extends Adaptor<Options & { layout: 'horizontal' | 'vertical' }>>(params: P) {
const { layout = 'horizontal' } = params.options;
params.options.coordinate.transform = layout !== 'horizontal' ? undefined : [{ type: 'transpose' }];
return params;
Expand Down
4 changes: 2 additions & 2 deletions packages/plots/src/core/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const CHART_OPTIONS = [
'marginLeft',
'depth',
'title',
];
] as const;

/** 最终透传给 G2 Spec 的保留字 */
export const RESERVED_KEYS = ['data', 'type', 'children', 'direction'];
Expand Down Expand Up @@ -257,7 +257,7 @@ export const ANNOTATION_LIST = [
shape: 'ConversionTag',
},
{
key: 'axis',
key: 'axisText',
shape: 'BidirectionalBarAxisText',
},
];
9 changes: 1 addition & 8 deletions packages/plots/src/core/plots/area/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,5 @@ type Params = Adaptor<AreaOptions>;
* @param options
*/
export function adaptor(params: Params) {
/**
* 图表差异化处理
*/
const init = (params: Params) => {
return params;
};

return flow(init, transformOptions, mark)(params);
return flow(transformOptions, mark)(params);
}
4 changes: 2 additions & 2 deletions packages/plots/src/core/plots/area/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { BaseOptions, Options } from '../../types/common';
import type { Options } from '../../types/common';

export type AreaOptions = Options & BaseOptions;
export type AreaOptions = Options;
9 changes: 1 addition & 8 deletions packages/plots/src/core/plots/bar/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ type Params = Adaptor<BarOptions>;
* @param options
*/
export function adaptor(params: Params) {
/**
* 图表差异化处理
*/
const init = (params: Params) => {
return params;
};

/**
* @title 背景图
* @description 通过新增 interval 实现
Expand Down Expand Up @@ -49,5 +42,5 @@ export function adaptor(params: Params) {
return params;
};

return flow(init, background, transformOptions, mark)(params);
return flow(background, transformOptions, mark)(params);
}
17 changes: 7 additions & 10 deletions packages/plots/src/core/plots/bar/type.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { BaseOptions, Options } from '../../types/common';
import type { Options } from '../../types/common';

type CommonOptions = Options & BaseOptions;

export type BarOptions = Options &
BaseOptions & {
/**
* @title mark 背景配置
*/
markBackground?: CommonOptions;
};
export type BarOptions = Options & {
/**
* @title mark 背景配置
*/
markBackground?: Options;
};
17 changes: 10 additions & 7 deletions packages/plots/src/core/plots/bidirectional-bar/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mark } from '../../components';
import type { Adaptor } from '../../types';
import { flow, transformOptions, set, isArray, omit, get } from '../../utils';
import { flow, transformOptions, set, isString, isArray, get } from '../../utils';
import { HORIZONTAL_MARGIN, AXIS_LABEL_PADDING, VERTICAL_MARGIN } from './constants';
import type { BidirectionalBarOptions } from './type';

Expand All @@ -15,11 +15,9 @@ export function adaptor(params: Params) {
const field = (params: Params) => {
const { options } = params;
const { yField, children } = options;
if (isArray(yField)) {
children.forEach((child, index) => {
set(child, 'yField', yField[index] || yField[0]);
});
}
children.forEach((child, index) => {
set(child, 'yField', yField[index]);
});
return params;
};

Expand Down Expand Up @@ -67,8 +65,13 @@ export function adaptor(params: Params) {
coordinate: { transform },
paddingBottom = AXIS_LABEL_PADDING,
paddingLeft = AXIS_LABEL_PADDING,
axis,
} = options;
set(options, 'axis.layout', layout);
set(options, 'axisText', {
...(axis?.x || {}),
layout,
});

const [child1, child2] = children;
if (layout === 'vertical') {
set(options, 'direction', 'col');
Expand Down
1 change: 0 additions & 1 deletion packages/plots/src/core/plots/bidirectional-bar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class BidirectionalBar extends Plot<BidirectionalBarOptions> {
axis: {
y: {
title: false,
labelFormatter: (item: number) => Math.abs(item),
},
x: { title: false, label: false },
},
Expand Down
23 changes: 12 additions & 11 deletions packages/plots/src/core/plots/bidirectional-bar/type.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { BaseOptions, Options } from '../../types/common';
import type { Options } from '../../types/common';

type CommonOptions = Options & BaseOptions;

export type BidirectionalBarOptions = Options &
BaseOptions & {
/**
* @title 布局
* @default "vertical"
*/
layout: 'vertical' | 'horizontal';
};
export type BidirectionalBarOptions = Options & {
/**
* @title y轴字段
*/
readonly yField?: string[];
/**
* @title 布局
* @default "vertical"
*/
layout: 'vertical' | 'horizontal';
};
4 changes: 2 additions & 2 deletions packages/plots/src/core/plots/box/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BaseOptions, Options } from '../../types/common';
import type { Options } from '../../types/common';

export type BoxOptions = Options & BaseOptions & {
export type BoxOptions = Options & {
// box 预处理, boxplot 非预处理
boxType: 'boxplot' | 'box';
};
5 changes: 2 additions & 3 deletions packages/plots/src/core/plots/bullet/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { coordinateLayout } from '../../components';
import { flow, transformOptions, map, get, isArray, includes, isNumber, deepAssign } from '../../utils';
import { flow, transformOptions, map, set, get, isArray, includes, isNumber, deepAssign } from '../../utils';

import type { Adaptor } from '../../types';
import type { BulletOptions } from './type';
Expand Down Expand Up @@ -133,8 +133,7 @@ export function adaptor(params: Params) {
const { layout = 'horizontal' } = params.options;

if (layout !== 'horizontal') {
const target = params.options.children[2];
target.shapeField = 'hyphen';
set(params, 'options.children[2].shapeField', 'hyphen');
}

return params;
Expand Down
34 changes: 17 additions & 17 deletions packages/plots/src/core/plots/bullet/type.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { BaseOptions, Options } from '../../types/common';
import type { Options } from '../../types/common';

export type BulletOptions = Options & BaseOptions & {
export type BulletOptions = Options & {
color: {
ranges: string | string[],
measures: string | string[],
target: string | string[],
},
rangeField: string,
measureField: string,
targetField: string,
ranges: string | string[];
measures: string | string[];
target: string | string[];
};
rangeField: string;
measureField: string;
targetField: string;
mapField: {
ranges: string | string[],
measures: string | string[],
target: string | string[],
},
ranges: string | string[];
measures: string | string[];
target: string | string[];
};
// 竖直|水平
layout: 'vertical' | 'horizontal',
range: BulletOptions,
measure: BulletOptions,
target: BulletOptions,
layout: 'vertical' | 'horizontal';
range: BulletOptions;
measure: BulletOptions;
target: BulletOptions;
};
9 changes: 1 addition & 8 deletions packages/plots/src/core/plots/column/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,5 @@ type Params = Adaptor<ColumnOptions>;
* @param options
*/
export function adaptor(params: Params) {
/**
* 图表差异化处理
*/
const init = (params: Params) => {
return params;
};

return flow(init, transformOptions, mark)(params);
return flow(transformOptions, mark)(params);
}
10 changes: 0 additions & 10 deletions packages/plots/src/core/plots/column/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ export class Column extends Plot<ColumnOptions> {
/** 图表类型 */
public type = 'column';

/**
* 获取 折线图 默认配置项
* 供外部使用
*/
static getDefaultOptions(): Partial<ColumnOptions> {
return {
type: 'view',
Expand Down Expand Up @@ -41,16 +37,10 @@ export class Column extends Plot<ColumnOptions> {
};
}

/**
* 获取 折线图 默认配置
*/
protected getDefaultOptions() {
return Column.getDefaultOptions();
}

/**
* 折线图适配器
*/
protected getSchemaAdaptor(): (params: Adaptor<ColumnOptions>) => void {
return adaptor;
}
Expand Down
9 changes: 1 addition & 8 deletions packages/plots/src/core/plots/dual-axes/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,5 @@ type Params = Adaptor<DualAxesOptions>;
* @param options
*/
export function adaptor(params: Params) {
/**
* 图表差异化处理
*/
const init = (params: Params) => {
return params;
};

return flow(init, transformOptions, mark)(params);
return flow(transformOptions, mark)(params);
}
7 changes: 3 additions & 4 deletions packages/plots/src/core/plots/funnel/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@ export function adaptor(params: Params) {
*/
const init = (params: Params) => {
const { options } = params;
const { xField, colorField, labels } = options;
const { xField, colorField } = options;
if (!colorField) {
set(options, 'colorField', xField);
}
if (labels) {
set(options, 'label', false);
}
return params;
};

Expand Down Expand Up @@ -51,6 +48,7 @@ export function adaptor(params: Params) {
children.push({
type: 'interval',
data: groupedData[1],
// @ts-ignore
yField: (item) => -item[yField],
});
delete options['compareField'];
Expand All @@ -60,6 +58,7 @@ export function adaptor(params: Params) {
set(options, 'type', 'spaceFlex');
set(options, 'ratio', [1, 1]);
set(options, 'direction', isTransposed ? 'row' : 'col');
// @ts-expect-error
delete options['seriesField'];
}

Expand Down
11 changes: 5 additions & 6 deletions packages/plots/src/core/plots/funnel/type.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { BaseOptions, Options } from '../../types/common';
import type { Options } from '../../types/common';

export type FunnelOptions = Options &
BaseOptions & {
compareField?: string;
isTransposed?: boolean;
};
export type FunnelOptions = Options & {
compareField?: string;
isTransposed?: boolean;
};
4 changes: 2 additions & 2 deletions packages/plots/src/core/plots/gauge/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { BaseOptions, Options } from '../../types/common';
import type { Options } from '../../types/common';

export type GaugeOptions = Options & BaseOptions;
export type GaugeOptions = Options;
Loading

0 comments on commit 8d2202c

Please sign in to comment.