-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChartsAxis.tsx
237 lines (226 loc) · 7.79 KB
/
ChartsAxis.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import * as React from 'react';
import PropTypes from 'prop-types';
import { CartesianContext } from '../context/CartesianContextProvider';
import { ChartsXAxis } from '../ChartsXAxis';
import { ChartsYAxis } from '../ChartsYAxis';
import {
ChartsAxisSlotComponentProps,
ChartsAxisSlotsComponent,
ChartsXAxisProps,
ChartsYAxisProps,
} from '../models/axis';
export interface ChartsAxisProps {
/**
* Indicate which axis to display the top of the charts.
* Can be a string (the id of the axis) or an object `ChartsXAxisProps`.
* @default null
*/
topAxis?: null | string | ChartsXAxisProps;
/**
* Indicate which axis to display the bottom of the charts.
* Can be a string (the id of the axis) or an object `ChartsXAxisProps`.
* @default xAxisIds[0] The id of the first provided axis
*/
bottomAxis?: null | string | ChartsXAxisProps;
/**
* Indicate which axis to display the left of the charts.
* Can be a string (the id of the axis) or an object `ChartsYAxisProps`.
* @default yAxisIds[0] The id of the first provided axis
*/
leftAxis?: null | string | ChartsYAxisProps;
/**
* Indicate which axis to display the right of the charts.
* Can be a string (the id of the axis) or an object `ChartsYAxisProps`.
* @default null
*/
rightAxis?: null | string | ChartsYAxisProps;
/**
* Overridable component slots.
* @default {}
*/
slots?: ChartsAxisSlotsComponent;
/**
* The props used for each component slot.
* @default {}
*/
slotProps?: ChartsAxisSlotComponentProps;
}
const getAxisId = (
propsValue: undefined | null | string | ChartsXAxisProps | ChartsYAxisProps,
): string | null => {
if (propsValue == null) {
return null;
}
if (typeof propsValue === 'object') {
return propsValue.axisId;
}
return propsValue;
};
const mergeProps = (
axisConfig: undefined | null | string | ChartsXAxisProps | ChartsYAxisProps,
slots?: Partial<ChartsAxisSlotsComponent>,
slotProps?: Partial<ChartsAxisSlotComponentProps>,
) => {
return typeof axisConfig === 'object'
? {
...axisConfig,
slots: { ...slots, ...axisConfig?.slots },
slotProps: { ...slotProps, ...axisConfig?.slotProps },
}
: { slots, slotProps };
};
function ChartsAxis(props: ChartsAxisProps) {
const { topAxis, leftAxis, rightAxis, bottomAxis, slots, slotProps } = props;
const { xAxis, xAxisIds, yAxis, yAxisIds } = React.useContext(CartesianContext);
// TODO: use for plotting line without ticks or any thing
// const drawingArea = React.useContext(DrawingContext);
const leftId = getAxisId(leftAxis === undefined ? yAxisIds[0] : leftAxis);
const bottomId = getAxisId(bottomAxis === undefined ? xAxisIds[0] : bottomAxis);
const topId = getAxisId(topAxis);
const rightId = getAxisId(rightAxis);
if (topId !== null && !xAxis[topId]) {
throw Error(`MUI: id used for top axis "${topId}" is not defined`);
}
if (leftId !== null && !yAxis[leftId]) {
throw Error(`MUI: id used for left axis "${leftId}" is not defined`);
}
if (rightId !== null && !yAxis[rightId]) {
throw Error(`MUI: id used for right axis "${rightId}" is not defined`);
}
if (bottomId !== null && !xAxis[bottomId]) {
throw Error(`MUI: id used for bottom axis "${bottomId}" is not defined`);
}
const topAxisProps = mergeProps(topAxis, slots, slotProps);
const bottomAxisProps = mergeProps(bottomAxis, slots, slotProps);
const leftAxisProps = mergeProps(leftAxis, slots, slotProps);
const rightAxisProps = mergeProps(rightAxis, slots, slotProps);
return (
<React.Fragment>
{topId && <ChartsXAxis {...topAxisProps} position="top" axisId={topId} />}
{bottomId && <ChartsXAxis {...bottomAxisProps} position="bottom" axisId={bottomId} />}
{leftId && <ChartsYAxis {...leftAxisProps} position="left" axisId={leftId} />}
{rightId && <ChartsYAxis {...rightAxisProps} position="right" axisId={rightId} />}
</React.Fragment>
);
}
ChartsAxis.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* Indicate which axis to display the bottom of the charts.
* Can be a string (the id of the axis) or an object `ChartsXAxisProps`.
* @default xAxisIds[0] The id of the first provided axis
*/
bottomAxis: PropTypes.oneOfType([
PropTypes.shape({
axisId: PropTypes.string.isRequired,
classes: PropTypes.object,
disableLine: PropTypes.bool,
disableTicks: PropTypes.bool,
fill: PropTypes.string,
label: PropTypes.string,
labelFontSize: PropTypes.number,
position: PropTypes.oneOf(['bottom', 'top']),
slotProps: PropTypes.object,
slots: PropTypes.object,
stroke: PropTypes.string,
tickFontSize: PropTypes.number,
tickMaxStep: PropTypes.number,
tickMinStep: PropTypes.number,
tickNumber: PropTypes.number,
tickSize: PropTypes.number,
}),
PropTypes.string,
]),
/**
* Indicate which axis to display the left of the charts.
* Can be a string (the id of the axis) or an object `ChartsYAxisProps`.
* @default yAxisIds[0] The id of the first provided axis
*/
leftAxis: PropTypes.oneOfType([
PropTypes.shape({
axisId: PropTypes.string.isRequired,
classes: PropTypes.object,
disableLine: PropTypes.bool,
disableTicks: PropTypes.bool,
fill: PropTypes.string,
label: PropTypes.string,
labelFontSize: PropTypes.number,
position: PropTypes.oneOf(['left', 'right']),
slotProps: PropTypes.object,
slots: PropTypes.object,
stroke: PropTypes.string,
tickFontSize: PropTypes.number,
tickMaxStep: PropTypes.number,
tickMinStep: PropTypes.number,
tickNumber: PropTypes.number,
tickSize: PropTypes.number,
}),
PropTypes.string,
]),
/**
* Indicate which axis to display the right of the charts.
* Can be a string (the id of the axis) or an object `ChartsYAxisProps`.
* @default null
*/
rightAxis: PropTypes.oneOfType([
PropTypes.shape({
axisId: PropTypes.string.isRequired,
classes: PropTypes.object,
disableLine: PropTypes.bool,
disableTicks: PropTypes.bool,
fill: PropTypes.string,
label: PropTypes.string,
labelFontSize: PropTypes.number,
position: PropTypes.oneOf(['left', 'right']),
slotProps: PropTypes.object,
slots: PropTypes.object,
stroke: PropTypes.string,
tickFontSize: PropTypes.number,
tickMaxStep: PropTypes.number,
tickMinStep: PropTypes.number,
tickNumber: PropTypes.number,
tickSize: PropTypes.number,
}),
PropTypes.string,
]),
/**
* The props used for each component slot.
* @default {}
*/
slotProps: PropTypes.object,
/**
* Overridable component slots.
* @default {}
*/
slots: PropTypes.object,
/**
* Indicate which axis to display the top of the charts.
* Can be a string (the id of the axis) or an object `ChartsXAxisProps`.
* @default null
*/
topAxis: PropTypes.oneOfType([
PropTypes.shape({
axisId: PropTypes.string.isRequired,
classes: PropTypes.object,
disableLine: PropTypes.bool,
disableTicks: PropTypes.bool,
fill: PropTypes.string,
label: PropTypes.string,
labelFontSize: PropTypes.number,
position: PropTypes.oneOf(['bottom', 'top']),
slotProps: PropTypes.object,
slots: PropTypes.object,
stroke: PropTypes.string,
tickFontSize: PropTypes.number,
tickMaxStep: PropTypes.number,
tickMinStep: PropTypes.number,
tickNumber: PropTypes.number,
tickSize: PropTypes.number,
}),
PropTypes.string,
]),
} as any;
export { ChartsAxis };