Skip to content

Commit

Permalink
fix: solve the cr problem
Browse files Browse the repository at this point in the history
  • Loading branch information
interstellarmt committed Sep 24, 2024
1 parent 18fd393 commit 2b0a93f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
28 changes: 7 additions & 21 deletions src/runtime/option-preprocess/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { get } from '@antv/util';
import { G2View, G2ViewTree } from '../types/options';
import { convertScale } from './scale';
import { G2ViewTree } from '../types/options';
import { flow } from '../../utils/flow';
import { columnWidthRatio } from './style';

export function optionPreprocess<T extends G2ViewTree = G2ViewTree>(
options: T,
): T {
export function optionPreprocess(options: G2ViewTree): G2ViewTree {
const convertedOptions = preprocess(options);
// If there are children, recursively convert each child node.
if (convertedOptions.children && Array.isArray(convertedOptions.children)) {
Expand All @@ -17,19 +15,7 @@ export function optionPreprocess<T extends G2ViewTree = G2ViewTree>(
}

// Entry point for all syntactic sugar functions.
export function preprocess<T extends G2ViewTree = G2ViewTree>(options: T): T {
const { style, scale, type } = options;
const scaleOption: G2View = {};
// style: { columnWidthRatio: 0.2 } => scale: { x: { padding: 0.8 } }
const columnWidthRatio = get(style, 'columnWidthRatio');
if (columnWidthRatio && type === 'interval') {
scaleOption.x = {
...scale?.x,
...convertScale(columnWidthRatio),
};
}
return {
...options,
scale: { ...scale, ...scaleOption },
};
export function preprocess(options: G2ViewTree): G2ViewTree {
//@todo define a type for params of flow
return flow(columnWidthRatio)(options);
}
5 changes: 0 additions & 5 deletions src/runtime/option-preprocess/scale.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/runtime/option-preprocess/style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { get } from '@antv/util';
import { G2View, G2ViewTree } from '../types/options';

// style: { columnWidthRatio: 0.2 } => scale: { x: { padding: 0.8 } }
export function columnWidthRatio(option: G2ViewTree) {
const { style, scale, type } = option;
const scaleOption: G2View = {};
const columnWidthRatio = get(style, 'columnWidthRatio');
if (columnWidthRatio && type === 'interval') {
scaleOption.x = {
...scale?.x,
padding: 1 - columnWidthRatio,
};
}
return {
...option,
scale: { ...scale, ...scaleOption },
};
}
4 changes: 2 additions & 2 deletions src/runtime/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export function render<T extends G2ViewTree = G2ViewTree>(
// Initialize the context if it is not provided.
const { width = 640, height = 480, depth = 0 } = options;
// Preprocessing here, such as syntactic sugar.
const optionsPre = optionPreprocess(options);
const keyed = inferKeys(optionsPre);
const preprocessOptions = optionPreprocess(options);
const keyed = inferKeys(preprocessOptions);
const {
canvas = Canvas(width, height),
emitter = new EventEmitter(),
Expand Down
13 changes: 13 additions & 0 deletions src/utils/flow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type FlowFunction<P> = (param: P) => P;

/**
* 类似 lodash.flow 的方法
* @param flows
*/
export function flow<P>(...flows: FlowFunction<P>[]): FlowFunction<P> {
return (param: P) => {
return flows.reduce((result: P, f: FlowFunction<P>) => {
return f(result);
}, param);
};
}

0 comments on commit 2b0a93f

Please sign in to comment.