Skip to content

Commit d5588a0

Browse files
Aarebeccayangtao.yangtao
andauthored
refactor(facet): 添加了spacing属性,支持配置分面图之间的间距 (#3614)
* docs(column): 修正了字体颜色 strooke -> fill * refactor(facet): 添加了spacing参数,支持配置分面图之间的间距 * refactor(facet): 完善了方法注释,规范了方法命名和文档描述; * docs(facet): 更新了facet文档描述 * refactor(facet): 补充了单测 * refactor(facet): 完善了注释 Co-authored-by: yangtao.yangtao <[email protected]>
1 parent 552500d commit d5588a0

File tree

6 files changed

+84
-22
lines changed

6 files changed

+84
-22
lines changed

docs/common/facet-cfg.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
#### FacetCfg.spacing
2+
3+
<description> _[number | string, number | string]_ **optional**</description>
4+
5+
分面子图之间的横向、纵向间隔大小,百分比形式表示占 view 宽高比例,数值形式表示像素值大小。支持百分比形式和数值形式混合表示,如:
6+
7+
- [像素值, 像素值]: `[10, 20]`
8+
- [像素值, 百分比]: `[20, '50%']`
9+
110
#### FacetCfg.padding
211

312
<description> _number | number[] | 'auto'_ **optional**</description>

examples/case/column/demo/column11.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ chart
5858
return originData.value + '%';
5959
},
6060
style: {
61-
stroke: '#fff'
61+
fill: '#fff'
6262
}
6363
};
6464
});

src/facet/facet.ts

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deepMix, each, every, get, isNil } from '@antv/util';
1+
import { deepMix, each, every, get, isNil, isNumber } from '@antv/util';
22
import { LAYER } from '../constant';
33
import { IGroup } from '../dependents';
44
import { AxisCfg, Condition, Datum, FacetCfg, FacetData, FacetDataFilter, Region } from '../interface';
@@ -153,11 +153,9 @@ export abstract class Facet<C extends FacetCfg<FacetData> = FacetCfg<FacetData>,
153153
*/
154154
private createFacetViews(): View[] {
155155
// 使用分面数据 创建分面 view
156-
return this.facets.map(
157-
(facet): View => {
158-
return this.facetToView(facet);
159-
}
160-
);
156+
return this.facets.map((facet): View => {
157+
return this.facetToView(facet);
158+
});
161159
}
162160

163161
/**
@@ -173,6 +171,31 @@ export abstract class Facet<C extends FacetCfg<FacetData> = FacetCfg<FacetData>,
173171
});
174172
}
175173

174+
/**
175+
* 解析 spacing
176+
*/
177+
private parseSpacing() {
178+
/**
179+
* @example
180+
*
181+
* // 仅使用百分比或像素值
182+
* // 横向间隔为 10%,纵向间隔为 10%
183+
* ['10%', '10%']
184+
* // 横向间隔为 10px,纵向间隔为 10px
185+
* [10, 10]
186+
*
187+
* // 同时使用百分比和像素值
188+
* ['10%', 10]
189+
* // 横向间隔为 10%,纵向间隔为 10px
190+
*/
191+
const { width, height } = this.view.viewBBox;
192+
const { spacing } = this.cfg;
193+
return spacing.map((s: number, idx: number) => {
194+
if (isNumber(s)) return s / (idx === 0 ? width : height);
195+
else return parseFloat(s) / 100;
196+
});
197+
}
198+
176199
// 其他一些提供给子类使用的方法
177200

178201
/**
@@ -206,30 +229,37 @@ export abstract class Facet<C extends FacetCfg<FacetData> = FacetCfg<FacetData>,
206229
* @param yIndex y 方向 index
207230
*/
208231
protected getRegion(rows: number, cols: number, xIndex: number, yIndex: number): Region {
209-
// x, y 方向均分 100% 宽高
210-
const xRatio = 1 / (cols === 0 ? 1 : cols);
211-
const yRatio = 1 / (rows === 0 ? 1 : rows);
212-
232+
const [xSpacing, ySpacing] = this.parseSpacing();
233+
// 每两个分面区域横向间隔xSPacing, 纵向间隔ySpacing
234+
// 每个分面区域的横纵占比
235+
/**
236+
* ratio * num + spacing * (num - 1) = 1
237+
* => ratio = (1 - (spacing * (num - 1))) / num
238+
* = (1 + spacing) / num - spacing
239+
*
240+
* num 对应 cols/rows
241+
* spacing 对应 xSpacing/ySpacing
242+
*/
243+
const xRatio = (1 + xSpacing) / (cols === 0 ? 1 : cols) - xSpacing;
244+
const yRatio = (1 + ySpacing) / (rows === 0 ? 1 : rows) - ySpacing;
245+
246+
// 得到第 index 个分面区域百分比位置
213247
const start = {
214-
x: xRatio * xIndex,
215-
y: yRatio * yIndex,
248+
x: (xRatio + xSpacing) * xIndex,
249+
y: (yRatio + ySpacing) * yIndex,
216250
};
217-
218251
const end = {
219-
x: xRatio * (xIndex + 1),
220-
y: yRatio * (yIndex + 1),
221-
};
222-
223-
return {
224-
start,
225-
end,
252+
x: start.x + xRatio,
253+
y: start.y + yRatio,
226254
};
255+
return { start, end };
227256
}
228257

229258
protected getDefaultCfg() {
230259
return {
231260
eachView: undefined,
232261
showTitle: true,
262+
spacing: [0, 0],
233263
padding: 10,
234264
fields: [],
235265
};

src/interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,8 @@ export interface FacetCfg<D> {
16651665
readonly type?: string;
16661666
/** view 创建回调。 */
16671667
readonly eachView: (innerView: View, facet?: D) => any;
1668+
/** 分面 view 之间的间隔, 百分比或像素值 */
1669+
readonly spacing?: [number | string, number | string];
16681670
/** facet view padding。 */
16691671
readonly padding?: ViewPadding;
16701672
/** 是否显示标题。 */

tests/unit/facet/list/index-spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ describe('facet rect', () => {
4646
start: { x: 0, y: 1 / 2 },
4747
end: { x: 1 / 3, y: 1 },
4848
});
49-
5049
// @ts-ignore
5150
const facetData0 = chart.facetInstance.facets[0] as ListData;
5251
expect(facetData0.total).toBe(5);

tests/unit/facet/mirror/index-spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ describe('facet mirror transpose = true', () => {
169169
fields: ['gender'],
170170
transpose: true,
171171
padding: [0, 48, 0, 0],
172+
spacing: ['10%', '10%'],
172173
eachView(view) {
173174
view.interval().position('age*total_percentage').color('gender', ['#1890ff', '#f04864']);
174175
},
@@ -237,6 +238,27 @@ describe('facet mirror transpose = true', () => {
237238
expect(facetInstance.destroyed).toBe(false);
238239
});
239240

241+
it('spacing', () => {
242+
const {
243+
start: { x: sx0, y: sy0 },
244+
end: { x: ex0, y: ey0 },
245+
// @ts-ignore
246+
} = chart.views[0].region;
247+
expect(sx0).toBeCloseTo(0);
248+
expect(sy0).toBeCloseTo(0);
249+
expect(ex0).toBeCloseTo(0.5 - 0.1 / 2);
250+
expect(ey0).toBeCloseTo(1);
251+
const {
252+
start: { x: sx1, y: sy1 },
253+
end: { x: ex1, y: ey1 },
254+
// @ts-ignore
255+
} = chart.views[1].region;
256+
expect(sx1).toBeCloseTo(0.5 + 0.1 / 2);
257+
expect(sy1).toBeCloseTo(0);
258+
expect(ex1).toBeCloseTo(1);
259+
expect(ey1).toBeCloseTo(1);
260+
});
261+
240262
it('clear', () => {
241263
// @ts-ignore
242264
const facetInstance = chart.facetInstance;

0 commit comments

Comments
 (0)