-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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] Allow Zoom
to be controllable
#13858
Conversation
export default function ZoomControlled() { | ||
const [zoom, setZoom] = React.useState<ZoomData[]>([ | ||
{ | ||
axisId: 'my-x-axis', |
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.
Currently axisId
is required. Should we allow {start,end}
to be passed when there is only one zoom enabled?
|
||
// This function is used to initialize the zoom data when it is not provided by the user. | ||
// It is helpful to avoid the need to provide the possibly auto-generated id for each axis. | ||
export const initializeZoomData = ( |
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.
Currently the user can use an empty array in the controlled zoom. We then fill it up correctly on the first user interaction. Though I'm not sure we should enforce the correct usage from the start?
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.
I'm not sure we should enforce the correct usage from the start?
I don't see issue asking users to start with the correct behavior.
I assume most of the use-cases will be useState([ { axisId: 'my-x-axis', start: 0, end: 100 } ])
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.
We got to intialize the data regardless for uncontrolled charts 😅
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.
From its usage it does not really look like an initializer.
I would expect that initializeZoomData
is used to initialize the state. For example in the default value of the useControlledProps
. But you use it at the beginning of every zoom interaction.
Either you consider it more as a cleaner function that ensure the axis are relevant with the options.
Then I would move the usage of this function in the value = React.useMemo(...
of the provider to call it only once.
Or it's an initializer and it should be called once in the useControlledProps
.
Notice that this solution does not support the case of a user adding/removing the zoom option of an axis. It's an edge case so no need for it to be fixed. Maybe other a highlight in the docs such as
You can not switch an axis from
zoom: false
tozoom: true
.
The current workaround is to control thezoomData
and add this new zoomable axis to it.
Deploy preview: https://deploy-preview-13858--material-ui-x.netlify.app/ Updated pages: |
packages/x-charts-pro/src/context/ZoomProvider/ZoomProvider.tsx
Outdated
Show resolved
Hide resolved
packages/x-charts-pro/src/context/ZoomProvider/ZoomProvider.tsx
Outdated
Show resolved
Hide resolved
packages/x-charts-pro/src/ResponsiveChartContainerPro/ResponsiveChartContainerPro.tsx
Outdated
Show resolved
Hide resolved
|
||
// This function is used to initialize the zoom data when it is not provided by the user. | ||
// It is helpful to avoid the need to provide the possibly auto-generated id for each axis. | ||
export const initializeZoomData = ( |
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.
I'm not sure we should enforce the correct usage from the start?
I don't see issue asking users to start with the correct behavior.
I assume most of the use-cases will be useState([ { axisId: 'my-x-axis', start: 0, end: 100 } ])
scripts/x-charts-pro.exports.json
Outdated
{ "name": "ZAxisContextProviderProps", "kind": "TypeAlias" }, | ||
{ "name": "ZoomContext", "kind": "Variable" }, | ||
{ "name": "ZoomData", "kind": "TypeAlias" }, | ||
{ "name": "ZoomOptions", "kind": "TypeAlias" }, | ||
{ "name": "ZoomProps", "kind": "TypeAlias" }, | ||
{ "name": "ZoomProvider", "kind": "Function" } |
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.
What about moving them in a .types.ts
files such that we only export types
{ "name": "ZAxisContextProviderProps", "kind": "TypeAlias" }, | |
{ "name": "ZoomContext", "kind": "Variable" }, | |
{ "name": "ZoomData", "kind": "TypeAlias" }, | |
{ "name": "ZoomOptions", "kind": "TypeAlias" }, | |
{ "name": "ZoomProps", "kind": "TypeAlias" }, | |
{ "name": "ZoomProvider", "kind": "Function" } | |
{ "name": "ZoomData", "kind": "TypeAlias" }, | |
{ "name": "ZoomOptions", "kind": "TypeAlias" }, | |
{ "name": "ZoomProps", "kind": "TypeAlias" }, |
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.
I've changed it to only export things that are actually useful for the user. Also documented useZoom
and <ZoomSetup/>
a bit
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
@alexfauquette this is ready for review |
packages/x-charts-pro/src/ChartContainerPro/ChartContainerPro.tsx
Outdated
Show resolved
Hide resolved
|
||
// This function is used to initialize the zoom data when it is not provided by the user. | ||
// It is helpful to avoid the need to provide the possibly auto-generated id for each axis. | ||
export const initializeZoomData = ( |
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.
From its usage it does not really look like an initializer.
I would expect that initializeZoomData
is used to initialize the state. For example in the default value of the useControlledProps
. But you use it at the beginning of every zoom interaction.
Either you consider it more as a cleaner function that ensure the axis are relevant with the options.
Then I would move the usage of this function in the value = React.useMemo(...
of the provider to call it only once.
Or it's an initializer and it should be called once in the useControlledProps
.
Notice that this solution does not support the case of a user adding/removing the zoom option of an axis. It's an edge case so no need for it to be fixed. Maybe other a highlight in the docs such as
You can not switch an axis from
zoom: false
tozoom: true
.
The current workaround is to control thezoomData
and add this new zoomable axis to it.
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.
🚀
Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com> Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com> Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
setZoomData([])
tosetZoomData(() => [])
to work with controllable statesetZoomData
usage changeBasic usage