@@ -27,6 +27,7 @@ export default (View) => {
2727 IProps extends AxisProps < TRecord > = AxisProps < TRecord >
2828 > extends Component < IProps & ChartChildProps , { } > {
2929 axisStyle : Style = { } ;
30+ maxLabelWidth : number = 0 ;
3031 ticks : Tick [ ] ;
3132
3233 constructor ( props : IProps & ChartChildProps ) {
@@ -383,25 +384,31 @@ export default (View) => {
383384 return false ;
384385 }
385386
386- findLabelsToHide ( ticks ) {
387- const { props, context } = this ;
388- const { coord } = props ;
387+ hasOverlap ( ticks ) {
388+ const { context } = this ;
389389 const { measureText } = context ;
390390
391391 const tickCount = ticks . length ;
392392
393393 const { label } = this . axisStyle ;
394394
395- let maxLabelWidth = 0 ;
396395 for ( let i = 0 ; i < tickCount ; i ++ ) {
397396 const tick = ticks [ i ] ;
398397 const { labelStyle = { } , text } = tick ;
399398 const bbox = measureText ( labelStyle . text || text , { ...label , ...labelStyle } ) ;
400399 tick . labelWidth = bbox . width ;
401- maxLabelWidth = Math . max ( maxLabelWidth , bbox . width ) ;
400+ this . maxLabelWidth = Math . max ( this . maxLabelWidth , bbox . width ) ;
402401 }
402+ return this . hasOverlapAtSeq ( ticks , 1 ) ;
403+ }
403404
404- const initialSeq = Math . floor ( maxLabelWidth / ( coord . width / ( tickCount - 1 ) ) ) ;
405+ findLabelsToHide ( ticks ) {
406+ const { props } = this ;
407+ const { coord } = props ;
408+
409+ const tickCount = ticks . length ;
410+
411+ const initialSeq = Math . floor ( this . maxLabelWidth / ( coord . width / ( tickCount - 1 ) ) ) ;
405412
406413 const range = tickCount - 1 ;
407414 const maxSeq = Math . floor ( range / 2 ) ;
@@ -432,24 +439,29 @@ export default (View) => {
432439 }
433440 }
434441
435- // 主要是计算coord的布局
442+ // 计算坐标轴布局并更新坐标系
436443 updateCoord ( ) {
437444 const { props } = this ;
438445 const { chart, labelAutoRotate = false , labelAutoHide = false } = props ;
439446 const dimType = this . _getDimType ( ) ;
440447 const ticks = this . getTicks ( ) ;
441448
442- if ( labelAutoRotate && dimType === 'x' ) {
443- this . findSuitableRotation ( ticks ) ;
444- this . ticks = ticks ;
445- }
446- if ( labelAutoHide && dimType === 'x' ) {
447- this . findLabelsToHide ( ticks ) ;
449+ if ( ( labelAutoRotate || labelAutoHide ) && dimType === 'x' && this . hasOverlap ( ticks ) ) {
450+ if ( labelAutoRotate ) {
451+ this . findSuitableRotation ( ticks ) ;
452+ }
453+
454+ if ( labelAutoHide ) {
455+ this . findLabelsToHide ( ticks ) ;
456+ }
457+
448458 this . ticks = ticks ;
449459 }
450460
461+ // 测量并获取布局信息
451462 const layout = this . measureLayout ( ticks ) ;
452463
464+ // 更新图表的坐标系
453465 chart . updateCoordFor ( this , layout ) ;
454466 }
455467
0 commit comments