@@ -44,44 +44,53 @@ function addLabelDimension(
44
44
} : Pick < ChartsXAxisProps , 'tickLabelInterval' | 'tickLabelStyle' > &
45
45
Pick < AxisDefaultized , 'reverse' > & { isMounted : boolean } ,
46
46
) : ( TickItemType & LabelExtraData ) [ ] {
47
- const withDimension = xTicks . map ( ( tick ) => {
47
+ const getTickSize = ( tick : TickItemType ) => {
48
48
if ( ! isMounted || tick . formattedValue === undefined ) {
49
- return { ... tick , width : 0 , height : 0 } ;
49
+ return { width : 0 , height : 0 } ;
50
50
}
51
+
51
52
const tickSizes = getWordsByLines ( { style, needsComputation : true , text : tick . formattedValue } ) ;
53
+
52
54
return {
53
- ...tick ,
54
55
width : Math . max ( ...tickSizes . map ( ( size ) => size . width ) ) ,
55
56
height : Math . max ( tickSizes . length * tickSizes [ 0 ] . height ) ,
56
57
} ;
57
- } ) ;
58
+ } ;
58
59
59
- if ( typeof tickLabelInterval === 'function' ) {
60
- return withDimension . map ( ( item , index ) => ( {
61
- ...item ,
62
- skipLabel : ! tickLabelInterval ( item . value , index ) ,
63
- } ) ) ;
64
- }
60
+ // FIXME: Add this back
61
+ // if (typeof tickLabelInterval === 'function') {
62
+ // return withDimension.map((item, index) => ({
63
+ // ...item,
64
+ // skipLabel: !tickLabelInterval(item.value, index),
65
+ // }));
66
+ // }
65
67
66
68
// Filter label to avoid overlap
67
- let currentTextLimit = 0 ;
68
69
let previousTextLimit = 0 ;
69
70
const direction = reverse ? - 1 : 1 ;
70
- return withDimension . map ( ( item , labelIndex ) => {
71
- const { width, offset, labelOffset, height } = item ;
71
+ const minGap = 8 ;
72
72
73
- const distance = getMinXTranslation ( width , height , style ?. angle ) ;
73
+ return xTicks . map ( ( item , labelIndex ) => {
74
+ const { offset, labelOffset } = item ;
74
75
const textPosition = offset + labelOffset ;
75
- const gapRatio = 1.2 ; // Ratio applied to the minimal distance to add some margin.
76
76
77
- currentTextLimit = textPosition - ( direction * ( gapRatio * distance ) ) / 2 ;
78
- if ( labelIndex > 0 && direction * currentTextLimit < direction * previousTextLimit ) {
77
+ if ( labelIndex > 0 && direction * ( textPosition - minGap ) < direction * previousTextLimit ) {
78
+ return { ...item , width : 0 , height : 0 , skipLabel : true } ;
79
+ }
80
+
81
+ const { width, height } = getTickSize ( item ) ;
82
+
83
+ const distance = getMinXTranslation ( width , height , style ?. angle ) ;
84
+
85
+ const currentTextLimit = textPosition - ( direction * distance ) / 2 ;
86
+ if ( labelIndex > 0 && direction * ( currentTextLimit - minGap ) < direction * previousTextLimit ) {
79
87
// Except for the first label, we skip all label that overlap with the last accepted.
80
88
// Notice that the early return prevents `previousTextLimit` from being updated.
81
- return { ...item , skipLabel : true } ;
89
+ return { ...item , width , height , skipLabel : true } ;
82
90
}
83
- previousTextLimit = textPosition + ( direction * ( gapRatio * distance ) ) / 2 ;
84
- return item ;
91
+
92
+ previousTextLimit = textPosition + ( direction * distance ) / 2 ;
93
+ return { ...item , width, height } ;
85
94
} ) ;
86
95
}
87
96
0 commit comments