Skip to content

Commit 26a9f05

Browse files
authored
feat(v2/column): support columnWidthRatio and marginRatio (#1354)
1 parent 3d760b6 commit 26a9f05

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

__tests__/unit/plots/column/index-spec.ts

+45
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,49 @@ describe('column', () => {
112112
yField: 'sales',
113113
});
114114
});
115+
116+
it('grouped column columnWidthRatio/marginRatio', () => {
117+
const column = new Column(createDiv('grouped column columnWidthRatio'), {
118+
width: 400,
119+
height: 300,
120+
data: subSalesByArea,
121+
xField: 'area',
122+
yField: 'sales',
123+
colorField: 'series',
124+
columnWidthRatio: 0.7,
125+
marginRatio: 0.1,
126+
});
127+
128+
column.render();
129+
130+
const geometry = column.chart.geometries[0];
131+
expect(geometry.getAdjust('dodge')).toMatchObject({
132+
xField: 'area',
133+
yField: 'sales',
134+
marginRatio: 0.1,
135+
dodgeRatio: 0.7,
136+
});
137+
});
138+
139+
it('stacked column columnWidthRatio/marginRatio', () => {
140+
const column = new Column(createDiv('stacked column columnWidthRatio'), {
141+
width: 400,
142+
height: 300,
143+
data: subSalesByArea,
144+
xField: 'area',
145+
yField: 'sales',
146+
colorField: 'series',
147+
isStack: true,
148+
columnWidthRatio: 0.7,
149+
});
150+
151+
column.render();
152+
153+
const geometry = column.chart.geometries[0];
154+
expect(geometry.getAdjust('stack')).toMatchObject({
155+
xField: 'area',
156+
yField: 'sales',
157+
});
158+
expect(geometry.theme.columnWidthRatio).toBe(0.7);
159+
});
115160
});

src/plots/column/adaptor.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Geometry, Chart } from '@antv/g2';
2-
import { deepMix, isFunction } from '@antv/util';
2+
import { deepMix, isFunction, isNil } from '@antv/util';
33
import { Params } from '../../core/adaptor';
44
import { findGeometry } from '../../common/helper';
55
import { tooltip, interaction, animation, theme } from '../../common/adaptor';
@@ -13,7 +13,7 @@ import { AXIS_META_CONFIG_KEYS } from '../../constant';
1313
*/
1414
function field(params: Params<ColumnOptions>): Params<ColumnOptions> {
1515
const { chart, options } = params;
16-
const { data, xField, yField, colorField, color, isStack } = options;
16+
const { data, xField, yField, colorField, color, isStack, marginRatio } = options;
1717

1818
chart.data(data);
1919
const geometry = chart.interval().position(`${xField}*${yField}`);
@@ -23,7 +23,10 @@ function field(params: Params<ColumnOptions>): Params<ColumnOptions> {
2323
}
2424

2525
if (colorField && ![xField, yField].includes(colorField)) {
26-
geometry.adjust(isStack ? 'stack' : 'dodge');
26+
geometry.adjust({
27+
type: isStack ? 'stack' : 'dodge',
28+
marginRatio,
29+
});
2730
}
2831

2932
return params;
@@ -129,10 +132,27 @@ function label(params: Params<ColumnOptions>): Params<ColumnOptions> {
129132
return params;
130133
}
131134

135+
/**
136+
* 柱形图额外的主题设置
137+
* @param params
138+
*/
139+
function columnTheme(params: Params<ColumnOptions>): Params<ColumnOptions> {
140+
const { chart, options } = params;
141+
const { columnWidthRatio } = options;
142+
143+
if (!isNil(columnWidthRatio)) {
144+
chart.theme({
145+
columnWidthRatio,
146+
});
147+
}
148+
149+
return params;
150+
}
151+
132152
/**
133153
* 柱形图适配器
134154
* @param params
135155
*/
136156
export function adaptor(params: Params<ColumnOptions>) {
137-
return flow(field, meta, axis, legend, tooltip, theme, style, label, interaction, animation)(params);
157+
return flow(field, meta, axis, legend, tooltip, theme, columnTheme, style, label, interaction, animation)(params);
138158
}

src/plots/column/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export interface ColumnOptions extends Options {
1010
readonly colorField?: string;
1111
/** 是否 堆积柱状图, 默认 分组柱状图 */
1212
readonly isStack?: boolean;
13-
/** 柱子宽度占比 [0-1] */
13+
/** 柱状图宽度占比 [0-1] */
14+
readonly columnWidthRatio?: number;
15+
/** 分组中柱子之间的间距 [0-1],仅对分组柱状图适用 */
1416
readonly marginRatio?: number;
15-
/** 分组或堆叠内部的间距,像素值 */
16-
readonly innerPadding?: number;
1717
/** 柱子样式配置,可选 */
1818
readonly columnStyle?: ShapeStyle | ((x: any, y: any, color?: any) => ShapeStyle);
1919
}

0 commit comments

Comments
 (0)