Skip to content

Commit

Permalink
feat: 超出自动隐藏 (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfu1 committed Sep 1, 2021
1 parent 0b37c4c commit bc77abe
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/.g2plot-plot-api/decomposition-tree-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Node title configuration.
containerStyle?: IShapeStyle;
/** title style */
style?: ILabelStyle;
/** auto ellipsis */
autoEllipsis?: boolean;
},
}
```
Expand Down
2 changes: 2 additions & 0 deletions docs/.g2plot-plot-api/decomposition-tree-graph.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ interface Data {
containerStyle?: IShapeStyle;
/** title 样式 */
style?: ILabelStyle;
/** 超出自动隐藏 */
autoEllipsis?: boolean;
},
}
```
Expand Down
2 changes: 2 additions & 0 deletions docs/.g2plot-plot-api/flow-analysis-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ Node title configuration.
containerStyle?: IShapeStyle;
/** title style */
style?: ILabelStyle;
/** auto ellipsis */
autoEllipsis?: boolean;
},
}
```
Expand Down
2 changes: 2 additions & 0 deletions docs/.g2plot-plot-api/flow-analysis-graph.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ interface Data {
containerStyle?: IShapeStyle;
/** title 样式 */
style?: ILabelStyle;
/** 超出自动隐藏 */
autoEllipsis?: boolean;
},
}
```
Expand Down
16 changes: 12 additions & 4 deletions src/graphs/flowAnalysisGraph/customItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getStatusCfg,
createMarker,
cloneBesidesImg,
useEllipsis,
} from '../utils';
import { CardNodeCfg, CardItems, IShape } from '../interface';

Expand Down Expand Up @@ -83,7 +84,11 @@ export const registerIndicatorCardNode = () => {
const paddingArray = cardPadding.map(
(item: number, index: number) => item + appendPadding[index],
);
const { style: titleStyle, containerStyle: titleContainerStyle } = titleCfg ?? {};
const {
style: titleStyle,
containerStyle: titleContainerStyle,
autoEllipsis = true,
} = titleCfg ?? {};
const {
style: itemStyle,
containerStyle: itemContainerStyle,
Expand Down Expand Up @@ -135,14 +140,17 @@ export const registerIndicatorCardNode = () => {
name: 'title-rect',
draggable: true,
});
const textStyle = {
...defaultTitleLabelStyle,
...getStyle(titleStyle, cfg, group),
};
titleTextShape = group!.addShape('text', {
attrs: {
x: paddingArray[3],
y: paddingArray[0],
textBaseline: 'top',
text: title,
...defaultTitleLabelStyle,
...getStyle(titleStyle, cfg, group),
text: autoEllipsis ? useEllipsis(title, textStyle?.fontSize, contentWidth) : title,
...textStyle,
},
name: 'title',
});
Expand Down
2 changes: 2 additions & 0 deletions src/graphs/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ export interface CardNodeCfg extends NodeCfg {
containerStyle?: IShapeStyle;
/** title 样式 */
style?: ILabelStyle;
/** 是否自动隐藏 */
autoEllipsis?: boolean;
};
items?: {
/** items 容器样式 */
Expand Down
14 changes: 14 additions & 0 deletions src/graphs/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,17 @@ export const setParentChildren = (
current.children = children;
return parendData;
};

/** 超出省略 */
export const useEllipsis = (
text: string,
fontSize: string | number = 12,
contentWidth: number = 120,
) => {
const size = isNumber(fontSize) ? fontSize : Number(fontSize.replace('px', ''));
const maxWords = Math.floor(contentWidth / size);
if (text.length <= maxWords) {
return text;
}
return text.slice(0, maxWords - 1) + '...';
};

0 comments on commit bc77abe

Please sign in to comment.