@@ -5,29 +5,73 @@ import pick from "lodash/pick";
5
5
6
6
import { ValueOrAccessor } from "../types/prop-types" ;
7
7
8
- // Private Functions
8
+ export type ElementPadding = {
9
+ top : number ;
10
+ bottom : number ;
11
+ left : number ;
12
+ right : number ;
13
+ } ;
14
+
15
+ export type MaybePointData = {
16
+ x ?: number ;
17
+ x0 ?: number ;
18
+ x1 ?: number ;
19
+ y ?: number ;
20
+ y0 ?: number ;
21
+ y1 ?: number ;
22
+ _x ?: number ;
23
+ _x0 ?: number ;
24
+ _x1 ?: number ;
25
+ _y ?: number ;
26
+ _y0 ?: number ;
27
+ _y1 ?: number ;
28
+ _voronoiX ?: number ;
29
+ _voronoiY ?: number ;
30
+ } ;
9
31
10
- function getCartesianRange ( props , axis ) {
11
- // determine how to lay the axis and what direction positive and negative are
12
- const vertical = axis !== "x" ;
13
- const padding = getPadding ( props ) ;
32
+ /**
33
+ * Determine the range of a cartesian axis
34
+ */
35
+ function getCartesianRange ( options : {
36
+ axis : "x" | "y" ;
37
+ height : number ;
38
+ width : number ;
39
+ padding : ElementPadding ;
40
+ } ) : [ number , number ] {
41
+ const vertical = options . axis !== "x" ;
14
42
if ( vertical ) {
15
- return [ props . height - padding . bottom , padding . top ] ;
43
+ return [ options . height - options . padding . bottom , options . padding . top ] ;
16
44
}
17
- return [ padding . left , props . width - padding . right ] ;
45
+ return [ options . padding . left , options . width - options . padding . right ] ;
18
46
}
19
47
20
- function getPolarRange ( props , axis ) {
21
- if ( axis === "x" ) {
22
- const startAngle = degreesToRadians ( props . startAngle || 0 ) ;
23
- const endAngle = degreesToRadians ( props . endAngle || 360 ) ;
48
+ /**
49
+ * Determine the range of a polar axis in radians
50
+ */
51
+ function getPolarRange ( options : {
52
+ axis : "x" | "y" ;
53
+ innerRadius ?: number ;
54
+ startAngle ?: number ;
55
+ endAngle ?: number ;
56
+ padding : ElementPadding ;
57
+ height : number ;
58
+ width : number ;
59
+ } ) : [ number , number ] {
60
+ if ( options . axis === "x" ) {
61
+ const startAngle = degreesToRadians ( options . startAngle || 0 ) ;
62
+ const endAngle = degreesToRadians ( options . endAngle || 360 ) ;
24
63
return [ startAngle , endAngle ] ;
25
64
}
26
- return [ props . innerRadius || 0 , getRadius ( props ) ] ;
65
+ return [
66
+ options . innerRadius || 0 ,
67
+ getRadius ( {
68
+ height : options . height ,
69
+ width : options . width ,
70
+ padding : options . padding ,
71
+ } ) ,
72
+ ] ;
27
73
}
28
74
29
- // Exported Functions
30
-
31
75
/**
32
76
* Creates an object composed of the inverted keys and values of object.
33
77
* If object contains duplicate values, subsequent values overwrite property assignments of previous values.
@@ -65,21 +109,36 @@ export function omit<T, Keys extends keyof T>(
65
109
return newObject ;
66
110
}
67
111
68
- export function getPoint ( datum ) {
69
- const exists = ( val ) => val !== undefined ;
112
+ /**
113
+ * Coalesce the x and y values from a data point
114
+ */
115
+ export function getPoint ( datum : MaybePointData ) : MaybePointData {
70
116
const { _x, _x1, _x0, _voronoiX, _y, _y1, _y0, _voronoiY } = datum ;
71
- const defaultX = exists ( _x1 ) ? _x1 : _x ;
72
- const defaultY = exists ( _y1 ) ? _y1 : _y ;
117
+ const defaultX = _x1 ?? _x ;
118
+ const defaultY = _y1 ?? _y ;
119
+
73
120
const point = {
74
- x : exists ( _voronoiX ) ? _voronoiX : defaultX ,
75
- x0 : exists ( _x0 ) ? _x0 : _x ,
76
- y : exists ( _voronoiY ) ? _voronoiY : defaultY ,
77
- y0 : exists ( _y0 ) ? _y0 : _y ,
121
+ x : _voronoiX ?? defaultX ,
122
+ x0 : _x0 ?? _x ,
123
+ y : _voronoiY ?? defaultY ,
124
+ y0 : _y0 ?? _y ,
78
125
} ;
126
+
79
127
return defaults ( { } , point , datum ) ;
80
128
}
81
129
82
- export function scalePoint ( props , datum ) {
130
+ /**
131
+ * Scale a point based on the origin, direction, and given scale function
132
+ */
133
+ export function scalePoint (
134
+ props : {
135
+ scale : { x : ( x ?: number ) => number ; y : ( y ?: number ) => number } ;
136
+ polar ?: boolean ;
137
+ horizontal ?: boolean ;
138
+ origin ?: { x : number ; y : number } ;
139
+ } ,
140
+ datum : MaybePointData ,
141
+ ) {
83
142
const { scale, polar, horizontal } = props ;
84
143
const d = getPoint ( datum ) ;
85
144
const origin = props . origin || { x : 0 , y : 0 } ;
@@ -95,8 +154,12 @@ export function scalePoint(props, datum) {
95
154
} ;
96
155
}
97
156
98
- export function getPadding ( props , name = "padding" ) {
99
- const padding = props [ name ] ;
157
+ /**
158
+ * Returns a padding value from a number or partial padding values
159
+ */
160
+ export function getPadding (
161
+ padding ?: number | Partial < ElementPadding > ,
162
+ ) : ElementPadding {
100
163
const paddingVal = typeof padding === "number" ? padding : 0 ;
101
164
const paddingObj = typeof padding === "object" ? padding : { } ;
102
165
return {
@@ -107,7 +170,10 @@ export function getPadding(props, name = "padding") {
107
170
} ;
108
171
}
109
172
110
- export function isTooltip ( component ) {
173
+ /**
174
+ * Returns true if the component is defined as a tooltip
175
+ */
176
+ export function isTooltip ( component ?: { type ?: { role ?: string } } ) {
111
177
const labelRole = component && component . type && component . type . role ;
112
178
return labelRole === "tooltip" ;
113
179
}
@@ -140,6 +206,9 @@ export function getStyles(style, defaultStyles) {
140
206
} ;
141
207
}
142
208
209
+ /**
210
+ * Returns the value of a prop or accessor function with the given props
211
+ */
143
212
export function evaluateProp < TValue > (
144
213
prop : ValueOrAccessor < TValue , Record < string , any > > ,
145
214
props : Record < string , any > ,
@@ -168,15 +237,29 @@ export function radiansToDegrees(radians) {
168
237
return typeof radians === "number" ? radians / ( Math . PI / 180 ) : radians ;
169
238
}
170
239
171
- export function getRadius ( props ) {
172
- const { left, right, top, bottom } = getPadding ( props ) ;
173
- const { width, height } = props ;
240
+ /**
241
+ * Get the maximum radius that will fit in the container
242
+ */
243
+ export function getRadius ( options : {
244
+ height : number ;
245
+ width : number ;
246
+ padding : ElementPadding ;
247
+ } ) {
248
+ const { width, height, padding } = options ;
249
+ const { left, right, top, bottom } = padding ;
174
250
return Math . min ( width - left - right , height - top - bottom ) / 2 ;
175
251
}
176
252
177
- export function getPolarOrigin ( props ) {
253
+ /**
254
+ * Returns the origin for a polar chart within the padded area
255
+ */
256
+ export function getPolarOrigin ( props : {
257
+ height : number ;
258
+ width : number ;
259
+ padding : ElementPadding ;
260
+ } ) : { x : number ; y : number } {
178
261
const { width, height } = props ;
179
- const { top, bottom, left, right } = getPadding ( props ) ;
262
+ const { top, bottom, left, right } = getPadding ( props . padding ) ;
180
263
const radius = Math . min ( width - left - right , height - top - bottom ) / 2 ;
181
264
const offsetWidth = width / 2 + left - right ;
182
265
const offsetHeight = height / 2 + top - bottom ;
@@ -186,15 +269,43 @@ export function getPolarOrigin(props) {
186
269
} ;
187
270
}
188
271
189
- export function getRange ( props , axis ) {
272
+ /**
273
+ * Determine the range of an axis based on the given props
274
+ */
275
+ export function getRange (
276
+ props : {
277
+ range ?: [ number , number ] ;
278
+ polar ?: boolean ;
279
+ innerRadius ?: number ;
280
+ startAngle ?: number ;
281
+ endAngle ?: number ;
282
+ height : number ;
283
+ width : number ;
284
+ padding : number | Partial < ElementPadding > ;
285
+ } ,
286
+ axis : "x" | "y" ,
287
+ ) {
190
288
if ( props . range && props . range [ axis ] ) {
191
289
return props . range [ axis ] ;
192
290
} else if ( props . range && Array . isArray ( props . range ) ) {
193
291
return props . range ;
194
292
}
195
293
return props . polar
196
- ? getPolarRange ( props , axis )
197
- : getCartesianRange ( props , axis ) ;
294
+ ? getPolarRange ( {
295
+ axis,
296
+ innerRadius : props . innerRadius ,
297
+ startAngle : props . startAngle ,
298
+ endAngle : props . endAngle ,
299
+ height : props . height ,
300
+ width : props . width ,
301
+ padding : getPadding ( props . padding ) ,
302
+ } )
303
+ : getCartesianRange ( {
304
+ axis,
305
+ height : props . height ,
306
+ width : props . width ,
307
+ padding : getPadding ( props . padding ) ,
308
+ } ) ;
198
309
}
199
310
200
311
/**
@@ -392,7 +503,10 @@ export function reduceChildren<
392
503
* @returns {Boolean } returns true if the props object contains `horizontal: true` of if any
393
504
* children or nested children are horizontal
394
505
*/
395
- export function isHorizontal ( props ) {
506
+ export function isHorizontal ( props : {
507
+ horizontal ?: boolean ;
508
+ children ?: React . ReactNode ;
509
+ } ) {
396
510
if ( props . horizontal !== undefined || ! props . children ) {
397
511
return props . horizontal ;
398
512
}
0 commit comments