1
1
'use client' ;
2
2
import * as React from 'react' ;
3
+ import { useChartContext } from '@mui/x-charts/context/ChartProvider' ;
3
4
import { AxisConfig , D3Scale } from '../models/axis' ;
4
5
import { isBandScale } from '../internals/isBandScale' ;
5
6
import { isInfinity } from '../internals/isInfinity' ;
@@ -85,6 +86,7 @@ export function useTicks(
85
86
options : {
86
87
scale : D3Scale ;
87
88
valueFormatter ?: AxisConfig [ 'valueFormatter' ] ;
89
+ direction : 'x' | 'y' ;
88
90
} & Pick < TickParams , 'tickNumber' | 'tickInterval' | 'tickPlacement' | 'tickLabelPlacement' > ,
89
91
) : TickItemType [ ] {
90
92
const {
@@ -94,7 +96,9 @@ export function useTicks(
94
96
tickInterval,
95
97
tickPlacement = 'extremities' ,
96
98
tickLabelPlacement : tickLabelPlacementProp ,
99
+ direction,
97
100
} = options ;
101
+ const { instance } = useChartContext ( ) ;
98
102
99
103
return React . useMemo ( ( ) => {
100
104
// band scale
@@ -157,20 +161,41 @@ export function useTicks(
157
161
}
158
162
const tickLabelPlacement = tickLabelPlacementProp ;
159
163
const ticks = typeof tickInterval === 'object' ? tickInterval : scale . ticks ( tickNumber ) ;
160
- return ticks . map ( ( value : any , i ) => {
161
- return {
162
- value,
163
- formattedValue :
164
- valueFormatter ?.( value , { location : 'tick' , scale } ) ??
165
- scale . tickFormat ( tickNumber ) ( value ) ,
166
- offset : scale ( value ) ,
167
- // Allowing the label to be placed in the middle of a continuous scale is weird.
168
- // But it is useful in some cases, like funnel categories with a linear scale.
169
- labelOffset :
170
- tickLabelPlacement === 'middle'
171
- ? scale ( ticks [ i - 1 ] ?? 0 ) - ( scale ( value ) + scale ( ticks [ i - 1 ] ?? 0 ) ) / 2
172
- : 0 ,
173
- } ;
174
- } ) ;
175
- } , [ scale , tickInterval , tickNumber , valueFormatter , tickPlacement , tickLabelPlacementProp ] ) ;
164
+
165
+ // Ticks inside the drawing area
166
+ const visibleTicks = [ ] ;
167
+
168
+ for ( let i = 0 ; i < ticks . length ; i += 1 ) {
169
+ const value = ticks [ i ] ;
170
+ const offset = scale ( value ) ;
171
+ const point = direction === 'x' ? { x : offset , y : 0 } : { x : 0 , y : offset } ;
172
+
173
+ if ( instance . isPointInside ( point , { direction } ) ) {
174
+ visibleTicks . push ( {
175
+ value,
176
+ formattedValue :
177
+ valueFormatter ?.( value , { location : 'tick' , scale } ) ??
178
+ scale . tickFormat ( tickNumber ) ( value ) ,
179
+ offset,
180
+ // Allowing the label to be placed in the middle of a continuous scale is weird.
181
+ // But it is useful in some cases, like funnel categories with a linear scale.
182
+ labelOffset :
183
+ tickLabelPlacement === 'middle'
184
+ ? scale ( ticks [ i - 1 ] ?? 0 ) - ( offset + scale ( ticks [ i - 1 ] ?? 0 ) ) / 2
185
+ : 0 ,
186
+ } ) ;
187
+ }
188
+ }
189
+
190
+ return visibleTicks ;
191
+ } , [
192
+ scale ,
193
+ tickLabelPlacementProp ,
194
+ tickInterval ,
195
+ tickNumber ,
196
+ tickPlacement ,
197
+ valueFormatter ,
198
+ direction ,
199
+ instance ,
200
+ ] ) ;
176
201
}
0 commit comments