Skip to content

Commit

Permalink
feat(graphin):share common utils in graphin-circle
Browse files Browse the repository at this point in the history
  • Loading branch information
pomelo-nwu committed Jan 28, 2021
1 parent 840a5d7 commit 0f16bd5
Showing 1 changed file with 105 additions and 1 deletion.
106 changes: 105 additions & 1 deletion packages/graphin/src/shape/utils.ts
@@ -1,3 +1,5 @@
import { deepMix, isArray, isNumber } from '@antv/util';
import { NodeStyle } from '../typings/type';
/**
*
* @param shapes 元素组合的shape集合
Expand All @@ -16,7 +18,7 @@ export const setStatusStyle = (shapes: any, statusStyle: any, parseAttr: (style:
const itemShapeName = shapeItem.cfg.name;
const style = statusStyle[itemShapeName];
if (style) {
const { animate, visible, ...otherAttrs } = parseAttr(style, itemShapeName);
const { animate, visible, ...otherAttrs } = parseAttr(statusStyle, itemShapeName);
shapeItem.attr(otherAttrs);
shapeItem.cfg.visible = visible !== false;
if (animate) {
Expand All @@ -29,3 +31,105 @@ export const setStatusStyle = (shapes: any, statusStyle: any, parseAttr: (style:
console.error(error);
}
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any

export function removeDumpAttrs<T>(attrs: T): T {
Object.keys(attrs).forEach((key) => {
// @ts-ignore
if (attrs[key] === undefined) {
// @ts-ignore
delete attrs[key];
}
});
return attrs;
}

/**
* 将 size 转换为宽度和高度
* @param size
*/
export const convertSizeToWH = (size: number | number[] | undefined) => {
if (!size) return [0, 0];
let width = 0;
let height = 0;
if (isNumber(size)) {
width = size;
height = size;
} else if (isArray(size)) {
if (size.length === 1) {
const [w] = size;
width = w;
height = w;
} else if (size.length === 2) {
const [w, h] = size;
width = w;
height = h;
}
}
return [width, height];
};

export const getLabelXYByPosition = (
cfg: NodeStyle,
): {
x: number;
y: number;
textBaseline?: 'top' | 'bottom';
} => {
const { label, keyshape } = cfg;
const { size } = keyshape;

let offsetArray: number[] = [0, 0];
const { position: labelPosition, offset = offsetArray } = label;
if (typeof offset === 'number' || typeof offset === 'string') {
offsetArray = [Number(offset), Number(offset)];
}
if ((offset as number[]).length > 0) {
offsetArray = offset as number[];
}

const [offsetX, offsetY] = offsetArray;
// 默认的位置(最可能的情形),所以放在最上面
if (labelPosition === 'center') {
return { x: 0, y: 0 };
}
const wh = convertSizeToWH(size);

const width = wh[0];
const height = wh[1];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let positionAttrs: any;
switch (labelPosition) {
case 'top':
positionAttrs = {
x: 0 + offsetX,
y: -height / 2 - offsetY,
textBaseline: 'bottom', // 文本在图形的上面
};
break;
case 'bottom':
positionAttrs = {
x: 0 + offsetX,
y: height / 2 + offsetY,
textBaseline: 'top',
};
break;
case 'left':
positionAttrs = {
x: 0 - width - offsetX,
y: 0 + offsetY,
textAlign: 'right',
};
break;
default:
positionAttrs = {
x: width + offsetX,
y: 0 + offsetY,
textAlign: 'left',
};
break;
}
return positionAttrs;
};

0 comments on commit 0f16bd5

Please sign in to comment.