-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[charts] Decouple margin
and axis-size
#16418
base: master
Are you sure you want to change the base?
Changes from 12 commits
644cd1f
7ee2fe4
977dd34
f48e90b
caab7bb
1b961f7
74f320f
45d9e94
a73e755
acc67bd
0747b9e
ec16952
6424b51
5bd7329
1cf7de6
75916db
f1eb5dd
0daad2b
f1c99ba
e21062d
ec5f0a7
50d753c
419a688
cd1b875
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,32 +11,38 @@ import { | |
ChartsYAxisProps, | ||
} from '../models/axis'; | ||
import { useXAxes, useYAxes } from '../hooks'; | ||
import { DEFAULT_AXIS_SIZE } from '../constants'; | ||
|
||
// TODO: Add links to the migration docs for each prop | ||
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 | ||
* @deprecated Use `xAxis[].position="top"` instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're suggesting using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 true it is not a prop of the In this What we can do is provide a url to the migration guide where we can explain with specific examples. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I'm not sure what's the best way to proceed here. I suppose a URL to the migration guide is helpful, at least, so unless we can think of any better option, that seems good to me 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not super concerned by this point. I guess most of the users are using directly the |
||
*/ | ||
topAxis?: null | string | ChartsXAxisProps; | ||
/** | ||
* 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 | ||
* @deprecated Use `yAxis[].position="right"` instead. | ||
*/ | ||
rightAxis?: null | string | ChartsYAxisProps; | ||
/** | ||
* 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 | ||
* @deprecated Use `xAxis[].position="bottom"` instead. | ||
*/ | ||
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 | ||
* @deprecated Use `yAxis[].position="left"` instead. | ||
*/ | ||
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 {} | ||
|
@@ -51,13 +57,12 @@ export interface ChartsAxisProps { | |
|
||
const getAxisId = ( | ||
propsValue: undefined | null | AxisId | ChartsXAxisProps | ChartsYAxisProps, | ||
defaultAxisId?: AxisId, | ||
): AxisId | null => { | ||
if (propsValue == null) { | ||
return null; | ||
} | ||
if (typeof propsValue === 'object') { | ||
return propsValue.axisId ?? defaultAxisId ?? null; | ||
return propsValue.axisId ?? null; | ||
} | ||
return propsValue; | ||
}; | ||
|
@@ -69,7 +74,6 @@ const mergeProps = ( | |
) => { | ||
return typeof axisConfig === 'object' | ||
? { | ||
...axisConfig, | ||
slots: { ...slots, ...axisConfig?.slots }, | ||
slotProps: { ...slotProps, ...axisConfig?.slotProps }, | ||
} | ||
|
@@ -86,58 +90,131 @@ const mergeProps = ( | |
* - [ChartsAxis API](https://mui.com/x/api/charts/charts-axis/) | ||
*/ | ||
function ChartsAxis(props: ChartsAxisProps) { | ||
const { topAxis, leftAxis, rightAxis, bottomAxis, slots, slotProps } = props; | ||
const { | ||
topAxis: topAxisProp, | ||
rightAxis: rightAxisProp, | ||
bottomAxis: bottomAxisProp, | ||
leftAxis: leftAxisProp, | ||
slots, | ||
slotProps, | ||
} = props; | ||
const { xAxis, xAxisIds } = useXAxes(); | ||
const { yAxis, yAxisIds } = useYAxes(); | ||
|
||
const leftId = getAxisId(leftAxis === undefined ? yAxisIds[0] : leftAxis, yAxisIds[0]); | ||
const bottomId = getAxisId(bottomAxis === undefined ? xAxisIds[0] : bottomAxis, xAxisIds[0]); | ||
const topId = getAxisId(topAxis, xAxisIds[0]); | ||
const rightId = getAxisId(rightAxis, yAxisIds[0]); | ||
const topId = getAxisId(topAxisProp); | ||
const rightId = getAxisId(rightAxisProp); | ||
const bottomId = getAxisId(bottomAxisProp); | ||
const leftId = getAxisId(leftAxisProp); | ||
|
||
if (topId !== null && !xAxis[topId]) { | ||
throw new Error( | ||
[ | ||
`MUI X: id used for top axis "${topId}" is not defined.`, | ||
`Available ids are: ${xAxisIds.join(', ')}.`, | ||
].join('\n'), | ||
); | ||
} | ||
if (leftId !== null && !yAxis[leftId]) { | ||
throw new Error( | ||
[ | ||
`MUI X: id used for left axis "${leftId}" is not defined.`, | ||
`Available ids are: ${yAxisIds.join(', ')}.`, | ||
].join('\n'), | ||
); | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (topId !== null && !xAxis[topId]) { | ||
throw new Error( | ||
[ | ||
`MUI X: id used for top axis "${topId}" is not defined.`, | ||
`Available ids are: ${xAxisIds.join(', ')}.`, | ||
].join('\n'), | ||
); | ||
} | ||
if (rightId !== null && !yAxis[rightId]) { | ||
throw new Error( | ||
[ | ||
`MUI X: id used for right axis "${rightId}" is not defined.`, | ||
`Available ids are: ${yAxisIds.join(', ')}.`, | ||
].join('\n'), | ||
); | ||
} | ||
if (bottomId !== null && !xAxis[bottomId]) { | ||
throw new Error( | ||
[ | ||
`MUI X: id used for bottom axis "${bottomId}" is not defined.`, | ||
`Available ids are: ${xAxisIds.join(', ')}.`, | ||
].join('\n'), | ||
); | ||
} | ||
if (leftId !== null && !yAxis[leftId]) { | ||
throw new Error( | ||
[ | ||
`MUI X: id used for left axis "${leftId}" is not defined.`, | ||
`Available ids are: ${yAxisIds.join(', ')}.`, | ||
].join('\n'), | ||
); | ||
} | ||
} | ||
if (rightId !== null && !yAxis[rightId]) { | ||
throw new Error( | ||
[ | ||
`MUI X: id used for right axis "${rightId}" is not defined.`, | ||
`Available ids are: ${yAxisIds.join(', ')}.`, | ||
].join('\n'), | ||
); | ||
} | ||
if (bottomId !== null && !xAxis[bottomId]) { | ||
throw new Error( | ||
[ | ||
`MUI X: id used for bottom axis "${bottomId}" is not defined.`, | ||
`Available ids are: ${xAxisIds.join(', ')}.`, | ||
].join('\n'), | ||
); | ||
} | ||
const topAxisProps = mergeProps(topAxis, slots, slotProps); | ||
const bottomAxisProps = mergeProps(bottomAxis, slots, slotProps); | ||
const leftAxisProps = mergeProps(leftAxis, slots, slotProps); | ||
const rightAxisProps = mergeProps(rightAxis, slots, slotProps); | ||
|
||
const topAxes = topId | ||
? [xAxis[topId]] | ||
: xAxisIds.map((id) => xAxis[id]).filter((axis) => axis.position === 'top'); | ||
const rightAxes = rightId | ||
? [yAxis[rightId]] | ||
: yAxisIds.map((id) => yAxis[id]).filter((axis) => axis.position === 'right'); | ||
const bottomAxes = bottomId | ||
? [xAxis[bottomId]] | ||
: xAxisIds.map((id) => xAxis[id]).filter((axis) => axis.position === 'bottom'); | ||
const leftAxes = leftId | ||
? [yAxis[leftId]] | ||
: yAxisIds.map((id) => yAxis[id]).filter((axis) => axis.position === 'left'); | ||
|
||
const completeTopAxisProps = topAxes.map((axis) => ({ | ||
...axis, | ||
...mergeProps(axis, slots, slotProps), | ||
})); | ||
const completeRightAxisProps = rightAxes.map((axis) => ({ | ||
...axis, | ||
...mergeProps(axis, slots, slotProps), | ||
})); | ||
const completeBottomAxisProps = ( | ||
bottomAxes.length === 0 && !xAxis[xAxisIds[0]].position ? [xAxis[xAxisIds[0]]] : bottomAxes | ||
).map((axis) => ({ ...axis, ...mergeProps(axis, slots, slotProps) })); | ||
const completeLeftAxisProps = ( | ||
leftAxes.length === 0 && !yAxis[yAxisIds[0]].position ? [yAxis[yAxisIds[0]]] : leftAxes | ||
).map((axis) => ({ ...axis, ...mergeProps(axis, slots, slotProps) })); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be simplified if you setup by default |
||
|
||
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} />} | ||
{completeTopAxisProps.map((axis, i, arr) => ( | ||
<ChartsXAxis | ||
key={axis.id} | ||
{...axis} | ||
position="top" | ||
axisId={axis.id} | ||
offset={arr | ||
.slice(0, i) | ||
.reduce((acc, curr) => acc + (curr.height ?? DEFAULT_AXIS_SIZE), axis.offset ?? 0)} | ||
/> | ||
))} | ||
{completeRightAxisProps.map((axis, i, arr) => ( | ||
<ChartsYAxis | ||
key={axis.id} | ||
{...axis} | ||
position="right" | ||
axisId={axis.id} | ||
offset={arr | ||
.slice(0, i) | ||
.reduce((acc, curr) => acc + (curr.width ?? DEFAULT_AXIS_SIZE), axis.offset ?? 0)} | ||
/> | ||
))} | ||
{completeBottomAxisProps.map((axis, i, arr) => ( | ||
<ChartsXAxis | ||
key={axis.id} | ||
{...axis} | ||
position="bottom" | ||
axisId={axis.id} | ||
offset={arr | ||
.slice(0, i) | ||
.reduce((acc, curr) => acc + (curr.height ?? DEFAULT_AXIS_SIZE), axis.offset ?? 0)} | ||
/> | ||
))} | ||
{completeLeftAxisProps.map((axis, i, arr) => ( | ||
<ChartsYAxis | ||
key={axis.id} | ||
{...axis} | ||
position="left" | ||
axisId={axis.id} | ||
offset={arr | ||
.slice(0, i) | ||
.reduce((acc, curr) => acc + (curr.width ?? DEFAULT_AXIS_SIZE), axis.offset ?? 0)} | ||
/> | ||
))} | ||
</React.Fragment> | ||
); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be done in this PR or as a follow-up? Just making sure it doesn't fall through the cracks 😄