Skip to content

Commit

Permalink
走查 & 重构 (#1489)
Browse files Browse the repository at this point in the history
* chore: remove unuse console.log

* chore(export): export adaptor and params

* refactor(scale): combile meta & axis into common adaptor scale
  • Loading branch information
hustcc authored Aug 25, 2020
1 parent f696e66 commit eacf58f
Show file tree
Hide file tree
Showing 17 changed files with 155 additions and 217 deletions.
32 changes: 28 additions & 4 deletions src/adaptor/common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* @file 通用的一些 adaptor
*/
import { Geometry } from '@antv/g2';
import { each } from '@antv/util';
import { each, deepMix } from '@antv/util';
import { Params } from '../core/adaptor';
import { Options } from '../types';
import { Interaction } from '../types/interaction';
import { Axis } from '../types/axis';
import { AXIS_META_CONFIG_KEYS } from '../constant';
import { pick } from '../utils';

/**
* 通用 legend 配置, 适用于带 colorField 的图表
Expand Down Expand Up @@ -114,3 +114,27 @@ export function slider(params: Params<Options>): Params<Options> {

return params;
}

/**
* scale 的 adaptor
* @param axes
*/
export function scale(axes: Record<string, Axis>) {
return function <O extends Pick<Options, 'meta'>>(params: Params<O>): Params<O> {
const { chart, options } = params;
const { meta } = options;

// 1. 轴配置中的 scale 信息
let scales: Record<string, any> = {};
each(axes, (axis: Axis, field: string) => {
scales[field] = pick(axis, AXIS_META_CONFIG_KEYS);
});

// 2. meta 直接是 scale 的信息
scales = deepMix({}, meta, scales);

chart.scale(scales);

return params;
};
}
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export const version = '2.0.0-beta.2';

// G2 自定义能力透出
import * as G2 from '@antv/g2';
export { G2 };
export * as G2 from '@antv/g2';

/** G2Plot 的 Plot 基类 */
export { Plot } from './core/plot';

/** Adaptor 及其参数的类型定义 */
export { Adaptor, Params } from './core/adaptor';

// 类型定义导出
export * from './types';

Expand All @@ -18,12 +21,18 @@ export { Area, AreaOptions } from './plots/area';
// 柱形图及类型定义
export { Column, ColumnOptions } from './plots/column';

// 条形图及类型定义
export { Bar, BarOptions } from './plots/bar';

// 饼图及类型定义
export { Pie, PieOptions } from './plots/pie';

// 散点图及类型定义
export { Scatter, ScatterOptions } from './plots/scatter';

// 雷达图及类型定义
export { Radar, RadarOptions } from './plots/radar';

// 混合图形
export { DualAxes, DualAxesOption } from './plots/dual-axes';

Expand All @@ -45,11 +54,5 @@ export { Progress, ProgressOptions } from './plots/progress';
// 环形进度图及类型定义
export { RingProgress, RingProgressOptions } from './plots/ring-progress';

// 条形图及类型定义
export { Bar, BarOptions } from './plots/bar';

// 雷达图及类型定义
export { Radar, RadarOptions } from './plots/radar';

// 热力图及类型定义
export { Heatmap, HeatmapOptions } from './plots/heatmap';
7 changes: 3 additions & 4 deletions src/plots/area/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ function geometry(params: Params<AreaOptions>): Params<AreaOptions> {
const { data, areaStyle, smooth } = options;

chart.data(data);
// area geometry 处理
flow(area)(deepMix({}, params, { options: { area: { smooth, style: areaStyle } } }));

return params;
// area geometry 处理
return flow(area)(deepMix({}, params, { options: { area: { smooth, style: areaStyle } } }));
}

/**
Expand Down Expand Up @@ -68,7 +67,7 @@ function adjust(params: Params<AreaOptions>): Params<AreaOptions> {
*/
export function adaptor(params: Params<AreaOptions>) {
// flow 的方式处理所有的配置到 G2 API
flow(
return flow(
geometry,
meta,
// line 面积线的配置处理
Expand Down
27 changes: 11 additions & 16 deletions src/plots/column/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { deepMix } from '@antv/util';
import { Params } from '../../core/adaptor';
import { findGeometry } from '../../utils';
import { tooltip, slider, interaction, animation, theme } from '../../adaptor/common';
import { tooltip, slider, interaction, animation, theme, scale } from '../../adaptor/common';
import { interval } from '../../adaptor/geometries';
import { flow, pick } from '../../utils';
import { flow } from '../../utils';
import { ColumnOptions } from './types';
import { AXIS_META_CONFIG_KEYS } from '../../constant';

/**
* 字段
Expand All @@ -17,7 +16,7 @@ function geometry(params: Params<ColumnOptions>): Params<ColumnOptions> {

chart.data(data);

flow(interval)(
return flow(interval)(
deepMix({}, params, {
options: {
interval: {
Expand All @@ -28,26 +27,22 @@ function geometry(params: Params<ColumnOptions>): Params<ColumnOptions> {
},
})
);

return params;
}

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

const scales = deepMix({}, meta, {
[xField]: pick(xAxis, AXIS_META_CONFIG_KEYS),
[yField]: pick(yAxis, AXIS_META_CONFIG_KEYS),
});

chart.scale(scales);
const { options } = params;
const { xAxis, yAxis, xField, yField } = options;

return params;
return flow(
scale({
[xField]: xAxis,
[yField]: yAxis,
})
)(params);
}

/**
Expand Down
36 changes: 14 additions & 22 deletions src/plots/dual-axes/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { deepMix } from '@antv/util';
import { theme, tooltip, interaction, animation } from '../../adaptor/common';
import { theme, tooltip, interaction, animation, scale } from '../../adaptor/common';
import { Params } from '../../core/adaptor';
import { flow, pick } from '../../utils';
import { flow } from '../../utils';
import { getOption } from './util/option';
import { drawSingleGeometry } from './util/geometry';
import { DualAxesOption } from './types';
import { AXIS_META_CONFIG_KEYS } from '../../constant';

/**
* 获取默认参数设置
Expand Down Expand Up @@ -71,23 +70,16 @@ function geometry(params: Params<DualAxesOption>): Params<DualAxesOption> {
* @param params
*/
export function meta(params: Params<DualAxesOption>): Params<DualAxesOption> {
const { chart, options } = params;
const { meta = {}, xAxis, yAxis, xField, yField } = options;

const xFieldScales = deepMix({}, meta[xField] || {}, pick(xAxis, AXIS_META_CONFIG_KEYS));
const leftYFieldScales = deepMix({}, meta[yField[0]] || {}, pick(yAxis[0], AXIS_META_CONFIG_KEYS));
const rightYFieldScales = deepMix({}, meta[yField[1]] || {}, pick(yAxis[1], AXIS_META_CONFIG_KEYS));

chart.views[0].scale({
[xField]: xFieldScales,
[yField[0]]: leftYFieldScales,
});

chart.views[1].scale({
[xField]: xFieldScales,
[yField[1]]: rightYFieldScales,
});
return params;
const { options } = params;
const { xAxis, yAxis, xField, yField } = options;

return flow(
scale({
[xField]: xAxis,
[yField[0]]: yAxis[0],
[yField[1]]: yAxis[1],
})
)(params);
}

/**
Expand Down Expand Up @@ -150,7 +142,7 @@ export function legend(params: Params<DualAxesOption>): Params<DualAxesOption> {
* @param chart
* @param options
*/
export function adaptor(params: Params<DualAxesOption>) {
export function adaptor(params: Params<DualAxesOption>): Params<DualAxesOption> {
// flow 的方式处理所有的配置到 G2 API
flow(transformOptions, geometry, meta, axis, legend, tooltip, theme, interaction, animation)(params);
return flow(transformOptions, geometry, meta, axis, legend, tooltip, theme, interaction, animation)(params);
}
29 changes: 13 additions & 16 deletions src/plots/heatmap/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { deepMix, isFunction, isObject } from '@antv/util';
import { isFunction, isObject } from '@antv/util';
import { Params } from '../../core/adaptor';
import { findGeometry } from '../../utils';
import { flow, pick } from '../../utils';
import { AXIS_META_CONFIG_KEYS, DEFAULT_COLORS } from '../../constant';
import { tooltip, interaction, animation, theme } from '../../adaptor/common';
import { flow } from '../../utils';
import { DEFAULT_COLORS } from '../../constant';
import { tooltip, interaction, animation, theme, scale } from '../../adaptor/common';
import { HeatmapOptions, ShapeType, SHAPE_TYPES } from './types';

/**
Expand Down Expand Up @@ -90,18 +90,15 @@ function field(params: Params<HeatmapOptions>): Params<HeatmapOptions> {
* @param params
*/
function meta(params: Params<HeatmapOptions>): Params<HeatmapOptions> {
const { chart, options } = params;
const { meta, xAxis, yAxis, xField, yField } = options;

// meta 直接是 scale 的信息
const scales = deepMix({}, meta, {
[xField]: pick(xAxis, AXIS_META_CONFIG_KEYS),
[yField]: pick(yAxis, AXIS_META_CONFIG_KEYS),
});

chart.scale(scales);
const { options } = params;
const { xAxis, yAxis, xField, yField } = options;

return params;
return flow(
scale({
[xField]: xAxis,
[yField]: yAxis,
})
)(params);
}

/**
Expand Down Expand Up @@ -227,5 +224,5 @@ function label(params: Params<HeatmapOptions>): Params<HeatmapOptions> {
*/
export function adaptor(params: Params<HeatmapOptions>) {
// flow 的方式处理所有的配置到 G2 API
flow(field, meta, theme, axis, legend, tooltip, style, label, interaction, animation)(params);
return flow(field, meta, theme, axis, legend, tooltip, style, label, interaction, animation)(params);
}
28 changes: 12 additions & 16 deletions src/plots/histogram/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import DataSet from '@antv/data-set';
import { deepMix, isFunction } from '@antv/util';
import { isFunction } from '@antv/util';
import { Params } from '../../core/adaptor';
import { tooltip, interaction, animation, theme } from '../../adaptor/common';
import { tooltip, interaction, animation, theme, scale } from '../../adaptor/common';
import { findGeometry } from '../../utils';
import { flow, pick } from '../../utils';
import { AXIS_META_CONFIG_KEYS } from '../../constant';
import { flow } from '../../utils';
import { HistogramOptions } from './types';

/**
Expand Down Expand Up @@ -48,18 +47,15 @@ function field(params: Params<HistogramOptions>): Params<HistogramOptions> {
* @param params
*/
function meta(params: Params<HistogramOptions>): Params<HistogramOptions> {
const { chart, options } = params;
const { meta, xAxis, yAxis } = options;

// 上面默认 x 轴 为 range 字段, y 轴字段为 count 字段
const scales = deepMix({}, meta, {
range: pick(xAxis, AXIS_META_CONFIG_KEYS),
count: pick(yAxis, AXIS_META_CONFIG_KEYS),
});

chart.scale(scales);
const { options } = params;
const { xAxis, yAxis } = options;

return params;
return flow(
scale({
range: xAxis,
count: yAxis,
})
)(params);
}

/**
Expand Down Expand Up @@ -151,5 +147,5 @@ function style(params: Params<HistogramOptions>): Params<HistogramOptions> {
*/
export function adaptor(params: Params<HistogramOptions>) {
// flow 的方式处理所有的配置到 G2 API
flow(field, meta, axis, legend, theme, label, style, tooltip, interaction, animation)(params);
return flow(field, meta, axis, legend, theme, label, style, tooltip, interaction, animation)(params);
}
31 changes: 13 additions & 18 deletions src/plots/line/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { deepMix } from '@antv/util';
import { Params } from '../../core/adaptor';
import { tooltip, slider, interaction, animation, theme } from '../../adaptor/common';
import { tooltip, slider, interaction, animation, theme, scale } from '../../adaptor/common';
import { findGeometry } from '../../utils';
import { AXIS_META_CONFIG_KEYS } from '../../constant';
import { point, line } from '../../adaptor/geometries';
import { flow, pick } from '../../utils';
import { flow } from '../../utils';
import { LineOptions } from './types';

/**
Expand All @@ -16,29 +15,25 @@ function geometry(params: Params<LineOptions>): Params<LineOptions> {
const { data, color, lineStyle, connectNulls, smooth } = options;

chart.data(data);
// line geometry 处理
flow(line)(deepMix({}, params, { options: { line: { connectNulls, smooth, color, style: lineStyle } } }));

return params;
// line geometry 处理
return flow(line)(deepMix({}, params, { options: { line: { connectNulls, smooth, color, style: lineStyle } } }));
}

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

// meta 直接是 scale 的信息
const scales = deepMix({}, meta, {
[xField]: pick(xAxis, AXIS_META_CONFIG_KEYS),
[yField]: pick(yAxis, AXIS_META_CONFIG_KEYS),
});

chart.scale(scales);
const { options } = params;
const { xAxis, yAxis, xField, yField } = options;

return params;
return flow(
scale({
[xField]: xAxis,
[yField]: yAxis,
})
)(params);
}

/**
Expand Down Expand Up @@ -112,5 +107,5 @@ function label(params: Params<LineOptions>): Params<LineOptions> {
*/
export function adaptor(params: Params<LineOptions>) {
// flow 的方式处理所有的配置到 G2 API
flow(geometry, meta, point, theme, axis, legend, tooltip, label, slider, interaction, animation)(params);
return flow(geometry, meta, point, theme, axis, legend, tooltip, label, slider, interaction, animation)(params);
}
Loading

0 comments on commit eacf58f

Please sign in to comment.