Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加 appendPadding 调节padding #2502

Merged
merged 8 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions docs/manual/concepts/visual-channel.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ chart.interval().position('班级*人数');
所以班级的映射存在两个视觉通道 1:n。

```javascript
chart
.interval()
.position('班级*人数')
.color('班级');
chart.interval().position('班级*人数').color('班级');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处的修改,由于commit 的勾子触发编辑器自动格式化引起的变化。这个需修改commit的勾子。

```

- 人数和班级共同决定颜色
Expand Down Expand Up @@ -131,10 +128,7 @@ G2 中的视觉通道作为标记的属性存在,需要支持以下功能:
示例:

```javascript
chart
.point()
.position('a*b')
.color('c');
chart.point().position('a*b').color('c');

chart
.interval()
Expand Down
9 changes: 3 additions & 6 deletions docs/manual/developer/registerlabel.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ registerGeometryLabel('custom', CustomLabel);

// Step 3
// 使用
chart
.line()
.position('x*y')
.label('y', {
type: 'custom',
});
chart.line().position('x*y').label('y', {
type: 'custom',
});
```

自定义 Label 需要继承  GeometryLabel 基类,通过覆写相应的方法来定义 label 的渲染配置,关于  GeometryLabel 类的详细介绍请阅读 [API 文档](../../api/g2/#registergeometrylabel)。
Expand Down
9 changes: 3 additions & 6 deletions docs/manual/developer/registerlabel.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ registerGeometryLabel('custom', CustomLabel);

// Step 3
// 使用
chart
.line()
.position('x*y')
.label('y', {
type: 'custom',
});
chart.line().position('x*y').label('y', {
type: 'custom',
});
```

自定义 Label 需要继承  GeometryLabel 基类,通过覆写相应的方法来定义 label 的渲染配置,关于  GeometryLabel 类的详细介绍请阅读 [API 文档](../../api/g2/#registergeometrylabel)。
Expand Down
2 changes: 2 additions & 0 deletions src/chart/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default class Chart extends View {
height,
autoFit = false,
padding,
appendPadding,
renderer = 'canvas',
pixelRatio,
localRefresh = true,
Expand Down Expand Up @@ -73,6 +74,7 @@ export default class Chart extends View {
middleGroup: canvas.addGroup({ zIndex: GROUP_Z_INDEX.MID }),
foregroundGroup: canvas.addGroup({ zIndex: GROUP_Z_INDEX.FORE }),
padding,
appendPadding,
visible,
options,
limitInPlot,
Expand Down
5 changes: 3 additions & 2 deletions src/chart/layout/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Controller } from '../controller/base';
import View from '../view';
import { parsePadding } from '../../util/padding';
import { calculatePadding } from './auto';

// 布局函数的定义
Expand All @@ -26,10 +27,10 @@ export default function defaultLayout(view: View): void {

// 1. 自动加 auto padding -> absolute padding
const padding = calculatePadding(view);

// 2. 计算出新的 coordinateBBox
const newCoordinateBBox = view.viewBBox.shrink(padding);
view.coordinateBBox = view.viewBBox.shrink(padding).shrink(parsePadding(view.appendPadding));

view.coordinateBBox = newCoordinateBBox;
view.adjustCoordinate();

// 3. 根据最新的 coordinate 重新布局组件
Expand Down
2 changes: 1 addition & 1 deletion src/chart/layout/padding-cal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class PaddingCal {
* 四周增加 padding
* @param padding
*/
public shrink(padding: number[]): PaddingCal {
public shrink(padding: Padding): PaddingCal {
const [top, right, bottom, left] = padding;

this.top += top;
Expand Down
5 changes: 5 additions & 0 deletions src/chart/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
TooltipOption,
ViewCfg,
ViewPadding,
ViewAppendPadding,
} from '../interface';

import { GROUP_Z_INDEX, LAYER, PLOT_EVENTS, VIEW_LIFE_CIRCLE } from '../constant';
Expand Down Expand Up @@ -85,6 +86,8 @@ export class View extends Base {
public coordinateBBox: BBox;
/** view 的 padding 大小,传入的配置(不是解析之后的值)。 */
public padding: ViewPadding;
/** padding的基础上增加的调整值 */
public appendPadding: ViewAppendPadding;
/** G.Canvas 实例。 */
public canvas: ICanvas;
/** 存储自动计算的 padding 值 */
Expand Down Expand Up @@ -150,6 +153,7 @@ export class View extends Base {
foregroundGroup,
region = { start: { x: 0, y: 0 }, end: { x: 1, y: 1 } },
padding,
appendPadding,
theme,
options,
limitInPlot,
Expand All @@ -162,6 +166,7 @@ export class View extends Base {
this.foregroundGroup = foregroundGroup;
this.region = region;
this.padding = padding;
this.appendPadding = appendPadding;
// 接受父 view 传入的参数
this.options = { ...this.options, ...options };
this.limitInPlot = limitInPlot;
Expand Down
17 changes: 16 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,13 @@ export interface ChartCfg {
* 2. padding: [ 10, 30, 30 ]
*/
readonly padding?: ViewPadding;
/**
* 图表的内边距会在图表的padding的基础上加上appendPadding,使用方式参考 CSS 盒模型。
* @example
* 1. appendPadding: 20
* 2. appendPadding: [ 10, 30, 30 ]
*/
readonly appendPadding?: ViewAppendPadding;
/**
* 是否开启局部刷新,默认开启。
*/
Expand Down Expand Up @@ -800,6 +807,13 @@ export interface ViewCfg {
* 2. padding: [ 10, 30, 30 ]
*/
readonly padding?: ViewPadding;
/**
* 设置图表的内边距在padding的基础上增加appendPading的调整。
* @example
* 1. padding: 20
* 2. padding: [ 10, 30, 30 ]
*/
readonly appendPadding?: ViewAppendPadding;
/** 设置 view 实例主题。 */
readonly theme?: LooseObject | string;
/**
Expand Down Expand Up @@ -2162,8 +2176,9 @@ export type Renderer = 'svg' | 'canvas';
export type Datum = Record<string, any>;
export type Data = Datum[];
export type ActionCallback = (context: IInteractionContext) => void;
export type Padding = number[];
export type Padding = [number, number, number, number];
export type ViewPadding = number | number[] | 'auto';
export type ViewAppendPadding = number | number[];
export type Position = [number, number];
export type AttributeType = 'position' | 'size' | 'color' | 'shape';
export type ShapeVertices = RangePoint[] | Point[] | Point[][];
Expand Down
4 changes: 2 additions & 2 deletions src/util/bbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class BBox {
* @param gap
*/
public shrink(gap: Padding): BBox {
const [top, right, bottom, left] = parsePadding(gap);
hustcc marked this conversation as resolved.
Show resolved Hide resolved
const [top, right, bottom, left] = gap;

return new BBox(this.x + left, this.y + top, this.width - left - right, this.height - top - bottom);
}
Expand All @@ -185,7 +185,7 @@ export class BBox {
* @param bbox
* @returns [top, right, bottom, left]
*/
public exceed(bbox: BBox): number[] {
public exceed(bbox: BBox): Padding {
return [
Math.max(-this.minY + bbox.minY, 0),
Math.max(this.maxX - bbox.maxX, 0),
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/chart/chart-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ describe('Chart', () => {
container: div,
width: 800,
height: 600,
padding: 10,
padding: 5,
appendPadding: 5,
autoFit: false,
visible: false,
});
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/chart/layout/padding-cal-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('padding-cal', () => {

it('shrink', () => {
const pc = new PaddingCal();
const p = [1, 2, 3, 4];
const p: [number, number, number, number] = [1, 2, 3, 4];
pc.shrink(p);
expect(pc.getPadding()).toEqual(p);
});
Expand Down