1
1
import React from 'react'
2
2
import useChartState from '../hooks/useChartState'
3
3
import useIsomorphicLayoutEffect from '../hooks/useIsomorphicLayoutEffect'
4
- import { axisTypeOrdinal , axisTypeTime , axisTypeUtc } from '../utils/Constants'
5
4
import { round } from '../utils/Utils'
6
5
7
6
const getElBox = el => {
@@ -23,24 +22,17 @@ export default function useMeasure({
23
22
rotation,
24
23
setRotation,
25
24
id,
26
- type,
27
25
position,
28
26
tickSizeInner,
29
27
tickSizeOuter,
30
28
labelRotation,
31
29
tickPadding,
32
- tickCount,
33
- minTickCount,
34
- maxTickCount,
35
30
vertical,
36
31
gridWidth,
37
32
gridHeight,
38
33
show,
39
34
} ) {
40
- const [ estimatedTickCount , setChartState ] = useChartState (
41
- state => state . estimatedTickCounts [ id ]
42
- )
43
- const [ tickLabelSkipIndices ] = useChartState (
35
+ const [ tickLabelSkipIndices , setChartState ] = useChartState (
44
36
state => state . tickLabelSkipIndices [ id ]
45
37
)
46
38
const [ axisDimension ] = useChartState (
@@ -71,12 +63,6 @@ export default function useMeasure({
71
63
}
72
64
73
65
let gridSize = ! vertical ? gridWidth : gridHeight
74
- let width = 0
75
- let height = 0
76
- let top = 0
77
- let bottom = 0
78
- let left = 0
79
- let right = 0
80
66
81
67
const domainDims = getElBox ( elRef . current . querySelector ( '.domain' ) )
82
68
@@ -89,19 +75,13 @@ export default function useMeasure({
89
75
) . map ( el => getElBox ( el ) )
90
76
91
77
// Determine the largest labels on the axis
92
- const [ widestMeasureLabel , tallestMeasureLabel ] = measureLabelDims . reduce (
93
- ( labels , d ) => {
94
- let [ largestW = d , largestH = d ] = labels
95
- if ( d . width > 0 && d . width > largestW . width ) {
96
- largestW = d
97
- }
98
- if ( d . height > 0 && d . height > largestH . height ) {
99
- largestH = d
100
- }
101
- return [ largestW , largestH ]
102
- } ,
103
- [ ]
104
- )
78
+ const widestMeasureLabel = measureLabelDims . reduce ( ( label , d ) => {
79
+ label = label || d
80
+ if ( d . width > 0 && d . width > label . width ) {
81
+ label = d
82
+ }
83
+ return label
84
+ } , null )
105
85
106
86
// Determine the largest labels on the axis
107
87
const [ widestRealLabel , tallestRealLabel ] = realLabelDims . reduce (
@@ -133,55 +113,8 @@ export default function useMeasure({
133
113
} , false )
134
114
}
135
115
136
- const largestMeasureLabelSize = ! vertical
137
- ? widestMeasureLabel ?. width || 0
138
- : tallestMeasureLabel ?. height || 0
139
-
140
- // Auto-fit ticks in "auto" tick mode for non-ordinal scales
141
- if ( tickCount === 'auto' && type !== 'ordinal' ) {
142
- // if it's on, determine how many ticks we could display if they were all flat
143
- // How many ticks can we fit in the available axis space?
144
- let calculatedTickCount = Math . floor (
145
- ( gridSize + largestMeasureLabelSize + tickPadding * 2 ) /
146
- ( largestMeasureLabelSize + tickPadding * 2 )
147
- )
148
-
149
- calculatedTickCount = Math . max (
150
- Math . min ( calculatedTickCount , maxTickCount ) ,
151
- minTickCount
152
- )
153
-
154
- if ( calculatedTickCount !== estimatedTickCount ) {
155
- setChartState ( old => ( {
156
- ...old ,
157
- estimatedTickCounts : {
158
- ...old . estimatedTickCounts ,
159
- [ id ] : calculatedTickCount ,
160
- } ,
161
- } ) )
162
- }
163
- }
164
-
165
116
let newTickLabelSkipIndices = [ ]
166
117
167
- // Visual Skipping of time-based axis labels if they overlap (rotation not included)
168
- if (
169
- ! rotation &&
170
- [ axisTypeTime , axisTypeUtc ] . includes ( type ) &&
171
- realLabelDims ?. length
172
- ) {
173
- realLabelDims . reduce ( ( last , d ) => {
174
- if (
175
- round ( last . right , 5 , Math . ceil ) >
176
- round ( d . left - tickPadding , 5 , Math . ceil )
177
- ) {
178
- newTickLabelSkipIndices . push ( realLabelDims . indexOf ( d ) )
179
- return last
180
- }
181
- return d
182
- } )
183
- }
184
-
185
118
if (
186
119
JSON . stringify ( newTickLabelSkipIndices ) !==
187
120
JSON . stringify ( tickLabelSkipIndices )
@@ -196,7 +129,7 @@ export default function useMeasure({
196
129
}
197
130
198
131
// Rotate ticks for non-time horizontal axes
199
- if ( ! vertical && type === axisTypeOrdinal ) {
132
+ if ( ! vertical ) {
200
133
const newRotation =
201
134
( widestMeasureLabel ?. width || 0 ) + tickPadding > smallestTickGap
202
135
? labelRotation
@@ -207,6 +140,15 @@ export default function useMeasure({
207
140
}
208
141
}
209
142
143
+ const newDimensions = {
144
+ width : 0 ,
145
+ height : 0 ,
146
+ top : 0 ,
147
+ bottom : 0 ,
148
+ left : 0 ,
149
+ right : 0 ,
150
+ }
151
+
210
152
// Axis overflow measurements
211
153
if ( ! vertical ) {
212
154
const leftMostLabelDim = realLabelDims . length
@@ -220,23 +162,23 @@ export default function useMeasure({
220
162
)
221
163
: null
222
164
223
- left = round (
165
+ newDimensions . left = round (
224
166
Math . max ( 0 , domainDims . left - leftMostLabelDim ?. left ) ,
225
- 10 ,
167
+ 5 ,
226
168
Math . ceil
227
169
)
228
170
229
- right = round (
171
+ newDimensions . right = round (
230
172
Math . max ( 0 , rightMostLabelDim ?. right - domainDims . right ) ,
231
- 10 ,
173
+ 5 ,
232
174
Math . ceil
233
175
)
234
176
235
- height = round (
177
+ newDimensions . height = round (
236
178
Math . max ( tickSizeInner , tickSizeOuter ) +
237
179
tickPadding +
238
180
( tallestRealLabel ?. height || 0 ) ,
239
- 10 ,
181
+ 5 ,
240
182
Math . ceil
241
183
)
242
184
} else {
@@ -248,36 +190,27 @@ export default function useMeasure({
248
190
labelDim . bottom > d . bottom ? labelDim : d
249
191
)
250
192
251
- top = round (
193
+ newDimensions . top = round (
252
194
Math . max ( 0 , domainDims . top - topMostLabelDim ?. top ) ,
253
- 10 ,
195
+ 5 ,
254
196
Math . ceil
255
197
)
256
198
257
- bottom = round (
199
+ newDimensions . bottom = round (
258
200
Math . max ( 0 , bottomMostLabelDim ?. bottom - domainDims . bottom ) ,
259
- 10 ,
201
+ 5 ,
260
202
Math . ceil
261
203
)
262
204
263
- width = round (
205
+ newDimensions . width = round (
264
206
Math . max ( tickSizeInner , tickSizeOuter ) +
265
207
tickPadding +
266
208
( widestRealLabel ?. width || 0 ) ,
267
- 10 ,
209
+ 5 ,
268
210
Math . ceil
269
211
)
270
212
}
271
213
272
- const newDimensions = {
273
- width,
274
- height,
275
- top,
276
- bottom,
277
- left,
278
- right,
279
- }
280
-
281
214
// Only update the axisDimensions if something has changed
282
215
if (
283
216
! axisDimension ||
@@ -297,27 +230,22 @@ export default function useMeasure({
297
230
} ) )
298
231
}
299
232
} , [
300
- axisDimension ,
301
233
elRef ,
302
- estimatedTickCount ,
303
234
gridHeight ,
304
235
gridWidth ,
305
236
id ,
306
237
labelRotation ,
307
- maxTickCount ,
308
- minTickCount ,
309
238
position ,
310
239
rotation ,
311
240
setChartState ,
312
241
setRotation ,
313
242
show ,
314
- tickCount ,
315
243
tickLabelSkipIndices ,
316
244
tickPadding ,
317
245
tickSizeInner ,
318
246
tickSizeOuter ,
319
- type ,
320
247
vertical ,
248
+ axisDimension ,
321
249
] )
322
250
323
251
// Measure after if needed
0 commit comments