@@ -6,7 +6,6 @@ const DEFAULT_CONFIG = {
66 anchorOffset : '10px' , // 锚点的偏移量
77 inflectionOffset : '30px' , // 拐点的偏移量
88 sidePadding : '15px' , // 文本距离画布四边的距离
9- height : '64px' , // 文本的行高
109 adjustOffset : '30' , // 发生调整时的偏移量
1110 triggerOn : 'click' , // 点击行为触发的时间类型
1211 // activeShape: true, // 当有图形被选中的时候,是否激活图形
@@ -18,7 +17,7 @@ const DEFAULT_CONFIG = {
1817 label1OffsetY : '-4px' ,
1918 label2OffsetY : '4px' ,
2019 type : 'default' , // 标签布局类型:default 或 spider
21- adjustRatio : 1 / 2 , // 调整高度的阈值比例
20+ adjustRatio : 1 , // 调整高度的阈值比例
2221 showAnchor : true ,
2322} ;
2423
@@ -75,7 +74,7 @@ export interface PieLabelProps {
7574 onClick ?: ( ev ) => void ;
7675 /**
7776 * 调整高度的阈值比例,用于判断是否使用两段式连线
78- * @default 0.5
77+ * @default 1
7978 */
8079 adjustRatio ?: number ;
8180 /**
@@ -102,14 +101,15 @@ export default (View) => {
102101 didMount ( ) { }
103102
104103 getLabels ( props ) {
104+ const { context } = this ;
105105 const {
106106 chart,
107107 coord,
108108 anchorOffset,
109109 inflectionOffset,
110110 label1,
111111 label2,
112- height : itemHeight ,
112+ height : itemHeight = context . px2hd ( '64px' ) ,
113113 sidePadding,
114114 } = props ;
115115
@@ -320,14 +320,33 @@ export default (View) => {
320320 } = props ;
321321 const { measureText, px2hd } = this . context ;
322322 const { center, radius, height : coordHeight , width : coordWidth } = coord ;
323+ const geometry = chart . getGeometrys ( ) [ 0 ] ;
324+ const records = geometry . flatRecords ( ) ;
325+
326+ // 高度计算,拿第一项数据作为计算依据
327+ const label1Text = isFunction ( label1 ) ? label1 ( records [ 0 ] ?. origin , records [ 0 ] ) : label1 ;
328+ const label2Text = isFunction ( label2 ) ? label2 ( records [ 0 ] ?. origin , records [ 0 ] ) : label2 ;
329+ const height =
330+ measureText ( label1Text . text , {
331+ fontSize : '24px' ,
332+ lineHeight : '24px' ,
333+ ...label1Text ,
334+ } ) . height +
335+ measureText ( label2Text . text , {
336+ fontSize : '24px' ,
337+ lineHeight : '24px' ,
338+ ...label2Text ,
339+ } ) . height +
340+ px2hd ( label1OffsetY ) +
341+ px2hd ( label2OffsetY ) +
342+ 2 ;
343+
344+ const maxCountForOneSide = Math . floor ( coordHeight / ( itemHeight || height ) ) ;
323345
324- const maxCountForOneSide = Math . floor ( coordHeight / itemHeight ) ;
325346 const maxCount = maxCountForOneSide * 2 ;
326347
327- const geometry = chart . getGeometrys ( ) [ 0 ] ;
328- const records = geometry
329- . flatRecords ( )
330- // 按角度大到小排序
348+ // 按角度大到小排序
349+ const showrecords = records
331350 . sort ( ( a , b ) => {
332351 const angle1 = a . xMax - a . xMin ;
333352 const angle2 = b . xMax - b . xMin ;
@@ -344,7 +363,7 @@ export default (View) => {
344363 // label 的最大宽度
345364 const labelWidth =
346365 coordWidth / 2 - radius - anchorOffset - inflectionOffset - 2 * sidePadding ;
347- records . forEach ( ( record ) => {
366+ showrecords . forEach ( ( record ) => {
348367 const { xMin, xMax, color, origin } = record ;
349368
350369 // 锚点角度
@@ -368,26 +387,9 @@ export default (View) => {
368387 color,
369388 label1 : isFunction ( label1 ) ? label1 ( origin , record ) : label1 ,
370389 label2 : isFunction ( label2 ) ? label2 ( origin , record ) : label2 ,
371- height : 0 ,
390+ height : height ,
372391 } ;
373392
374- const height =
375- measureText ( label . label1 . text , {
376- fontSize : '24px' ,
377- lineHeight : '24px' ,
378- ...label . label1 ,
379- } ) . height +
380- measureText ( label . label2 . text , {
381- fontSize : '24px' ,
382- lineHeight : '24px' ,
383- ...label . label2 ,
384- } ) . height +
385- px2hd ( label1OffsetY ) +
386- px2hd ( label2OffsetY ) +
387- 2 ;
388-
389- label . height = height ;
390-
391393 // 判断文本的方向
392394 if ( side === 'left' ) {
393395 halves [ 0 ] . push ( label ) ;
@@ -431,7 +433,7 @@ export default (View) => {
431433 }
432434 } ) ;
433435
434- labels = labels . concat ( adjustPosition ( half , showSide , props , labelWidth ) ) ;
436+ labels = labels . concat ( adjustPosition ( half , showSide , props , labelWidth , center , radius ) ) ;
435437 } ) ;
436438
437439 return labels ;
0 commit comments