@@ -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 }
0 commit comments