Skip to content

Commit 6023e01

Browse files
authored
feat: legend增加adaptive属性 (#2124)
* feat: legend增加adaptive属性 * fix: 优化重复代码
1 parent 9ec7031 commit 6023e01

12 files changed

+295
-9
lines changed

packages/f2/src/components/legend/legendView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default (props) => {
5555
valueStyle,
5656
valuePrefix,
5757
onClick,
58+
layoutMode,
5859
} = props;
5960

6061
const formatValue = (value, valuePrefix = ': ') => {
@@ -76,7 +77,7 @@ export default (props) => {
7677
<group
7778
className="legend-item"
7879
style={{
79-
width: itemWidth,
80+
...(layoutMode == 'adaptive' ? {} : { width: itemWidth }),
8081
display: 'flex',
8182
flexDirection: 'row',
8283
alignItems: 'center',

packages/f2/src/components/legend/withLegend.tsx

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ export interface LegendProps {
2727
*/
2828
position?: 'right' | 'left' | 'top' | 'bottom';
2929
/**
30-
* 图例宽度
30+
* 布局模式:'uniform' 统一宽度(默认),'adaptive' 自适应宽度
31+
*/
32+
33+
layoutMode?: 'uniform' | 'adaptive';
34+
35+
/**
36+
* 图例宽度 uniform 模式下生效
3137
*/
3238
width?: number | string;
3339
/**
@@ -142,19 +148,28 @@ export default (View) => {
142148
width: customWidth,
143149
height: customHeight,
144150
position = 'top',
151+
layoutMode = 'uniform',
145152
} = props;
146153
const items = this.getItems();
147154
if (!items || !items.length) return;
148155
const { left, top, width: layoutWidth, height: layoutHeight } = parentLayout;
149156
const width = context.px2hd(customWidth) || layoutWidth;
150157
const node = computeLayout(this, this.render());
158+
151159
const { width: itemMaxWidth, height: itemMaxHeight } = this.getMaxItemBox(node);
152-
// 每行最多的个数
153-
const lineMaxCount = Math.max(1, Math.floor(width / itemMaxWidth));
154-
const itemCount = items.length;
155-
// legend item 的行数
156-
const lineCount = Math.ceil(itemCount / lineMaxCount);
157-
const itemWidth = width / lineMaxCount;
160+
161+
let lineCount, itemWidth;
162+
if (layoutMode === 'adaptive') {
163+
lineCount = 1; // adaptive模式先不考虑多行情况
164+
itemWidth = undefined;
165+
} else {
166+
// uniform模式
167+
const lineMaxCount = Math.max(1, Math.floor(width / itemMaxWidth));
168+
const itemCount = items.length;
169+
lineCount = Math.ceil(itemCount / lineMaxCount);
170+
itemWidth = width / lineMaxCount;
171+
}
172+
158173
const autoHeight = itemMaxHeight * lineCount;
159174
const style: GroupStyleProps = {
160175
left,
@@ -167,31 +182,35 @@ export default (View) => {
167182
alignItems: 'center',
168183
justifyContent: 'flex-start',
169184
};
185+
170186
// 如果只有一行,2端对齐
171187
if (lineCount === 1) {
172188
style.justifyContent = 'space-between';
173189
}
190+
174191
if (position === 'top') {
175192
style.height = customHeight ? customHeight : autoHeight;
176193
}
177194
if (position === 'left') {
178195
style.flexDirection = 'column';
179196
style.justifyContent = 'center';
197+
style.alignItems = layoutMode === 'adaptive' ? 'flex-start' : 'center';
180198
style.width = itemMaxWidth;
181199
style.height = customHeight ? customHeight : layoutHeight;
182200
}
183201
if (position === 'right') {
184202
style.flexDirection = 'column';
185203
style.alignItems = 'flex-start';
186204
style.justifyContent = 'center';
205+
style.left = left + (width - itemMaxWidth);
187206
style.width = itemMaxWidth;
188207
style.height = customHeight ? customHeight : layoutHeight;
189-
style.left = left + (width - itemMaxWidth);
190208
}
191209
if (position === 'bottom') {
192210
style.top = top + (layoutHeight - autoHeight);
193211
style.height = customHeight ? customHeight : autoHeight;
194212
}
213+
195214
this.itemWidth = itemWidth;
196215
this.legendStyle = style;
197216
}
5.5 KB
Loading
10.2 KB
Loading
9.08 KB
Loading
9.19 KB
Loading
10.2 KB
Loading
5.39 KB
Loading
5.73 KB
Loading
5.78 KB
Loading

0 commit comments

Comments
 (0)