Skip to content

Commit

Permalink
chore(template): add plot template (#1497)
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc authored Aug 28, 2020
1 parent 263caf5 commit 0ca2651
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
51 changes: 51 additions & 0 deletions src/plots/_template/adaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { interaction, animation, theme, scale } from '../../adaptor/common';
import { Params } from '../../core/adaptor';
import { flow } from '../../utils';
import { TemplateOptions } from './types';

/**
* geometry 处理
* @param params
*/
function geometry(params: Params<TemplateOptions>): Params<TemplateOptions> {
const { chart, options } = params;
const { data, xField, yField } = options;

chart.data(data);

chart.interval().position(`${xField}*${yField}`);

return params;
}

/**
* meta 配置
* @param params
*/
export function meta(params: Params<TemplateOptions>): Params<TemplateOptions> {
const { options } = params;
const { xAxis, yAxis, xField, yField } = options;

return flow(
scale({
[xField]: xAxis,
[yField]: yAxis,
})
)(params);
}
/**
* 折线图适配器
* @param chart
* @param options
*/
export function adaptor(params: Params<TemplateOptions>) {
// flow 的方式处理所有的配置到 G2 API
return flow(
geometry,
meta,
interaction,
animation,
theme
// ... 其他的 adaptor flow
)(params);
}
21 changes: 21 additions & 0 deletions src/plots/_template/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Plot } from '../../core/plot';
import { TemplateOptions } from './types';
import { adaptor } from './adaptor';
import { Adaptor } from '../../core/adaptor';

export { TemplateOptions };

/**
* 这个是一个图表开发的 模板代码!
*/
export class Template extends Plot<TemplateOptions> {
/** 图表类型 */
public type: string = 'template';

/**
* 获取适配器
*/
protected getSchemaAdaptor(): Adaptor<TemplateOptions> {
return adaptor;
}
}
9 changes: 9 additions & 0 deletions src/plots/_template/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Options } from '../../types';

/** 配置类型定义 */
export interface TemplateOptions extends Options {
/** x 轴字段 */
readonly xField?: string;
/** y 轴字段 */
readonly yField?: string;
}
8 changes: 4 additions & 4 deletions src/plots/dual-axes/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ function geometry(params: Params<DualAxesOption>): Params<DualAxesOption> {
const PADDING = [20, 40];

// 绘制左轴对应数据
chart
const leftView = chart
.createView({
padding: PADDING,
})
.data(data[0]);

// 绘制右轴对应数据
chart
const rightView = chart
.createView({
padding: PADDING,
})
.data(data[1]);

// 左轴图形
drawSingleGeometry({
chart: chart.views[0],
chart: leftView,
options: {
xField,
yField: yField[0],
Expand All @@ -55,7 +55,7 @@ function geometry(params: Params<DualAxesOption>): Params<DualAxesOption> {

// 右轴图形
drawSingleGeometry({
chart: chart.views[1],
chart: rightView,
options: {
xField,
yField: yField[1],
Expand Down
2 changes: 1 addition & 1 deletion src/plots/dual-axes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { DualAxesOption };

export class DualAxes extends Plot<DualAxesOption> {
/** 图表类型: 双轴图 */
public type: string = 'dualAxes';
public type: string = 'dual-axes';

/**
* 获取双轴图的适配器
Expand Down

0 comments on commit 0ca2651

Please sign in to comment.