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 3 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
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
8 changes: 6 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 Down Expand Up @@ -27,8 +28,11 @@ export default function defaultLayout(view: View): void {
// 1. 自动加 auto padding -> absolute padding
const padding = calculatePadding(view);

// 2. 计算出 coordinateBBox
view.coordinateBBox = view.viewBBox.shrink(padding);
// 2. 计算出 coordinateBBox, 增加 appendPadding 调整
view.coordinateBBox = view.viewBBox.shrink(padding).shrink(parsePadding(view.appendPadding));

view.coordinateBBox;

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 @@ -44,6 +44,7 @@ import {
ViewCfg,
ViewOption,
ViewPadding,
ViewAppendPadding,
} from '../interface';

import { GROUP_Z_INDEX, LAYER, PLOT_EVENTS, VIEW_LIFE_CIRCLE } from '../constant';
Expand Down Expand Up @@ -89,6 +90,8 @@ export class View extends Base {
public coordinateBBox: BBox;
/** view 的 padding 大小,传入的配置(不是解析之后的值)。 */
public padding: ViewPadding;
/** padding的基础上增加的调整值 */
public appendPadding: ViewAppendPadding;
/** G.Canvas 实例。 */
public canvas: ICanvas;

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;
this.themeObject = mergeTheme({}, theme);
// 接受父 view 传入的参数
this.options = { ...this.options, ...options };
Expand Down
17 changes: 16 additions & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,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 @@ -734,6 +741,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 @@ -1910,8 +1924,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
6 changes: 3 additions & 3 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 Expand Up @@ -230,4 +230,4 @@ export function toPoints(bbox: Partial<BBox>): any[] {
[bbox.maxX, bbox.maxY],
[bbox.minX, bbox.maxY]
];
}
}
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