@@ -44,44 +44,54 @@ 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
+ let distance = getMinXTranslation ( 0 , 0 , style ?. angle ) ;
76
77
77
- currentTextLimit = textPosition - ( direction * ( gapRatio * distance ) ) / 2 ;
78
- if ( labelIndex > 0 && direction * currentTextLimit < direction * previousTextLimit ) {
78
+ if ( labelIndex > 0 && direction * ( textPosition - minGap ) < direction * previousTextLimit ) {
79
+ return { ...item , width : 0 , height : 0 , skipLabel : true } ;
80
+ }
81
+
82
+ const { width, height } = getTickSize ( item ) ;
83
+
84
+ distance = getMinXTranslation ( width , height , style ?. angle ) ;
85
+
86
+ const currentTextLimit = textPosition - ( direction * distance ) / 2 ;
87
+ if ( labelIndex > 0 && direction * ( currentTextLimit - minGap ) < direction * previousTextLimit ) {
79
88
// Except for the first label, we skip all label that overlap with the last accepted.
80
89
// Notice that the early return prevents `previousTextLimit` from being updated.
81
- return { ...item , skipLabel : true } ;
90
+ return { ...item , width , height , skipLabel : true } ;
82
91
}
83
- previousTextLimit = textPosition + ( direction * ( gapRatio * distance ) ) / 2 ;
84
- return item ;
92
+
93
+ previousTextLimit = textPosition + ( direction * distance ) / 2 ;
94
+ return { ...item , width, height } ;
85
95
} ) ;
86
96
}
87
97
0 commit comments