@@ -10,64 +10,91 @@ import { AxisConfig, ScaleName } from '../../../../models';
10
10
import { ChartsXAxisProps , ChartsYAxisProps } from '../../../../models/axis' ;
11
11
import { DatasetType } from '../../../../models/seriesType/config' ;
12
12
13
- export function defaultizeAxis (
13
+ export function defaultizeXAxis (
14
14
inAxis : readonly MakeOptional < AxisConfig < ScaleName , any , ChartsXAxisProps > , 'id' > [ ] | undefined ,
15
15
dataset : Readonly < DatasetType > | undefined ,
16
- axisName : 'x' ,
17
- ) : AxisConfig < ScaleName , any , ChartsXAxisProps > [ ] ;
18
- export function defaultizeAxis (
19
- inAxis : readonly MakeOptional < AxisConfig < ScaleName , any , ChartsYAxisProps > , 'id' > [ ] | undefined ,
20
- dataset : Readonly < DatasetType > | undefined ,
21
- axisName : 'y' ,
22
- ) : AxisConfig < ScaleName , any , ChartsYAxisProps > [ ] ;
23
- export function defaultizeAxis (
24
- inAxis : readonly MakeOptional < AxisConfig , 'id' > [ ] | undefined ,
25
- dataset : Readonly < DatasetType > | undefined ,
26
- axisName : 'x' | 'y' ,
27
- ) : AxisConfig [ ] {
28
- const DEFAULT_AXIS_KEY = axisName === 'x' ? DEFAULT_X_AXIS_KEY : DEFAULT_Y_AXIS_KEY ;
29
-
16
+ ) : Array < AxisConfig < ScaleName , any , ChartsXAxisProps > > {
30
17
const offsets = {
31
18
top : 0 ,
32
- right : 0 ,
33
19
bottom : 0 ,
34
- left : 0 ,
35
20
none : 0 ,
36
21
} ;
37
22
38
23
const inputAxes =
39
- inAxis && inAxis . length > 0 ? inAxis : [ { id : DEFAULT_AXIS_KEY , scaleType : 'linear' as const } ] ;
24
+ inAxis && inAxis . length > 0
25
+ ? inAxis
26
+ : [ { id : DEFAULT_X_AXIS_KEY , scaleType : 'linear' as const } ] ;
40
27
41
28
const parsedAxes = inputAxes . map ( ( axisConfig , index ) => {
42
29
const dataKey = axisConfig . dataKey ;
43
- const defaultPosition = axisName === 'x' ? ( 'bottom' as const ) : ( 'left' as const ) ;
44
30
45
31
const position = axisConfig . position ?? 'none' ;
46
- const dimension = axisName === 'x' ? 'height' : 'width' ;
47
-
48
- const height =
49
- axisName === 'x'
50
- ? DEFAULT_AXIS_SIZE_HEIGHT + ( axisConfig . label ? AXIS_LABEL_DEFAULT_HEIGHT : 0 )
51
- : 0 ;
52
- const width =
53
- axisName === 'y'
54
- ? DEFAULT_AXIS_SIZE_WIDTH + ( axisConfig . label ? AXIS_LABEL_DEFAULT_HEIGHT : 0 )
55
- : 0 ;
32
+ const defaultHeight =
33
+ DEFAULT_AXIS_SIZE_HEIGHT + ( axisConfig . label ? AXIS_LABEL_DEFAULT_HEIGHT : 0 ) ;
56
34
57
35
const sharedConfig = {
58
- id : `defaultized-${ axisName } -axis-${ index } ` ,
36
+ id : `defaultized-x -axis-${ index } ` ,
59
37
// The fist axis is defaultized to the bottom/left
60
- ...( index === 0 ? { position : defaultPosition } : { } ) ,
61
- height,
62
- width,
38
+ ...( index === 0 ? ( { position : 'bottom' } as const ) : { } ) ,
39
+ height : defaultHeight ,
40
+ offset : offsets [ position ] ,
41
+ ...axisConfig ,
42
+ } ;
43
+
44
+ // Increment the offset for the next axis
45
+ if ( position !== 'none' ) {
46
+ offsets [ position ] += axisConfig . height ?? defaultHeight ;
47
+ }
48
+
49
+ // If `dataKey` is NOT provided
50
+ if ( dataKey === undefined || axisConfig . data !== undefined ) {
51
+ return sharedConfig ;
52
+ }
53
+
54
+ if ( dataset === undefined ) {
55
+ throw new Error ( `MUI X: x-axis uses \`dataKey\` but no \`dataset\` is provided.` ) ;
56
+ }
57
+
58
+ // If `dataKey` is provided
59
+ return {
60
+ ...sharedConfig ,
61
+ data : dataset . map ( ( d ) => d [ dataKey ] ) ,
62
+ } ;
63
+ } ) ;
64
+
65
+ return parsedAxes ;
66
+ }
67
+
68
+ export function defaultizeYAxis (
69
+ inAxis : readonly MakeOptional < AxisConfig < ScaleName , any , ChartsYAxisProps > , 'id' > [ ] | undefined ,
70
+ dataset : Readonly < DatasetType > | undefined ,
71
+ ) : Array < AxisConfig < ScaleName , any , ChartsYAxisProps > > {
72
+ const offsets = { right : 0 , left : 0 , none : 0 } ;
73
+
74
+ const inputAxes =
75
+ inAxis && inAxis . length > 0
76
+ ? inAxis
77
+ : [ { id : DEFAULT_Y_AXIS_KEY , scaleType : 'linear' as const } ] ;
78
+
79
+ const parsedAxes = inputAxes . map ( ( axisConfig , index ) => {
80
+ const dataKey = axisConfig . dataKey ;
81
+
82
+ const position = axisConfig . position ?? 'none' ;
83
+ const defaultWidth =
84
+ DEFAULT_AXIS_SIZE_WIDTH + ( axisConfig . label ? AXIS_LABEL_DEFAULT_HEIGHT : 0 ) ;
85
+
86
+ const sharedConfig = {
87
+ id : `defaultized-y-axis-${ index } ` ,
88
+ // The first axis is defaultized to the left
89
+ ...( index === 0 ? ( { position : 'left' } as const ) : { } ) ,
90
+ width : defaultWidth ,
63
91
offset : offsets [ position ] ,
64
92
...axisConfig ,
65
93
} ;
66
94
67
95
// Increment the offset for the next axis
68
96
if ( position !== 'none' ) {
69
- offsets [ position ] +=
70
- ( axisConfig as any ) [ dimension ] ?? ( dimension === 'height' ? height : width ) ;
97
+ offsets [ position ] += axisConfig . width ?? defaultWidth ;
71
98
}
72
99
73
100
// If `dataKey` is NOT provided
@@ -76,7 +103,7 @@ export function defaultizeAxis(
76
103
}
77
104
78
105
if ( dataset === undefined ) {
79
- throw new Error ( `MUI X: ${ axisName } -axis uses \`dataKey\` but no \`dataset\` is provided.` ) ;
106
+ throw new Error ( `MUI X: y -axis uses \`dataKey\` but no \`dataset\` is provided.` ) ;
80
107
}
81
108
82
109
// If `dataKey` is provided
0 commit comments