-
-
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] Add onClick
support
#11411
[charts] Add onClick
support
#11411
Conversation
packages/x-charts/src/ChartsOnClickHandler/ChartsOnClickHandler.tsx
Outdated
Show resolved
Hide resolved
packages/x-charts/src/ChartsOnClickHandler/ChartsOnClickHandler.tsx
Outdated
Show resolved
Hide resolved
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
1 similar comment
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
ca12099
to
5ae198a
Compare
5ae198a
to
e93cf2c
Compare
e93cf2c
to
f09505f
Compare
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
Signed-off-by: Alexandre Fauquette <[email protected]>
This pull request has conflicts, please resolve those before we can evaluate the pull request. |
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.
Superb work! 👍 👏 😍
Leaving some general nitpicks and raising a discussion point below.
Have you considered going with one event handler with a type prop to identify the type on the consumer side? 🤔
I've checked and it seems that both Nivo and Highcharts have only a single click
handler. 🤷
At a glance it seems that we are adding quite a bit of complexity and it's a bit unclear if it is warranted. 🤔
</Box> | ||
|
||
<Stack direction="column" sx={{ width: { xs: '100%', md: '40%' } }}> | ||
<Typography>click data</Typography> |
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.
Nitpick, but it feels like this section could use a bit extra "eye candy" to be a bit more clear and UX-friendly. 🤔
It also feels like these demos could use a reset
button to clear the clicked data. 🤷
cc @noraleonte
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.
<Typography>click data</Typography> | |
<Box sx={{display:'flex', justifyContent:'space-between'}}> | |
<Typography>Click on the chart!</Typography> | |
<IconButton aria-label="reset"> // reset button | |
<UndoOutlinedIcon/> | |
</IconButton> | |
</Box> |
I agree, the reset button would make the experience a lot better 👌 We can do something simple, like this. And some slight improvement of the text
I made test for line chart:
The complexity added by having multiple way of listening click event allows to adapt the chart behavior. For example modifying the pointer on hover if something is listening to the click. I think the complexity is the largest for those components. Tree view, gauge, and other don't have those axes triggering issues, so they will only get itemClick |
Gotcha. Seems fair. 👌
Do you feel that it still makes sense to go for consistent naming ( |
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.
Amazing work! 🍰 🎉
Leaving a small suggestion to follow up on Lukas' comment 👍
</Box> | ||
|
||
<Stack direction="column" sx={{ width: { xs: '100%', md: '40%' } }}> | ||
<Typography>click data</Typography> |
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.
<Typography>click data</Typography> | |
<Box sx={{display:'flex', justifyContent:'space-between'}}> | |
<Typography>Click on the chart!</Typography> | |
<IconButton aria-label="reset"> // reset button | |
<UndoOutlinedIcon/> | |
</IconButton> | |
</Box> |
I agree, the reset button would make the experience a lot better 👌 We can do something simple, like this. And some slight improvement of the text
code={`// Data from item click | ||
${itemData ? JSON.stringify(itemData, null, 2) : '// click on the chart'} | ||
`} |
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.
code={`// Data from item click | |
${itemData ? JSON.stringify(itemData, null, 2) : '// click on the chart'} | |
`} | |
code={ | |
itemData ? `// Data from item click: | |
${JSON.stringify(itemData, null, 2)}` : '// The data will appear here' | |
} |
Maybe rephrasing these comments a bit could make the expected outcome a bit clearer 🤔
It feel safer because we could have |
Hey thanks for the work, but one important thing is missing :) |
@wascou have you checked this documentation section? |
Thanks @wascou for the heads up, I forgot to document how to use composition with scatter charts. The best documentation for now might be the code itself :) mui-x/packages/x-charts/src/ScatterChart/ScatterChart.tsx Lines 127 to 130 in 00b6b27
mui-x/packages/x-charts/src/ScatterChart/ScatterChart.tsx Lines 141 to 145 in 00b6b27
The Both are available only in v7 |
ah ok, no BC for this one ? I have to wait for a stable v7 then |
Ok, works perfectly on my side. Voronoi i interresting but not very accurate when you have a lot of points in your scatter plot. I also put the |
Signed-off-by: Alexandre Fauquette <[email protected]>
Fixes #10549
This PR is have commits per component impacted. I recommend to have first a look a documentation for the DX aspect, and a per commit review for code quality
Decision
I went with the
onAxisClick
/onItemClick
such that easy usecase are handled with easy solution. The more complex ones are:onAxisClick
For docs, I'm moving to a docs per component to explain edge cases only in the docs of the charts for which they are relevant. It's easier to explain why
onClick
is more complex than usual in the scatter charts than in a dedicated pageIt's a attempt to take best of the different proposed PRs
Current solution
I'm adding an
<ChartsHandleOnClick onclick={onClick} />
that can be added anywhere.This simply contain a
useEffect
that adds an event listener to thesvgRef
which will call theonClick
callback with the event, plus some data if an item or an axis is active according to our internal state.Why such a solution
The
onClick
is not passed to each element, but listen at the<svg/>
level for multiple reasons:Issues
The main drawback I can see is the mouse pointer.
<svg/>
level to modify the mouse pointer when there is something to click I would need to modify style by looking at the context to see if there is an interactive item or axis selected.onClick
returns multiple values, I can not know if devs will use only item data, or axis data. So I can't put a meaning full style.Ideas
I've some ideas of improvement
Idea 1
The first one is to distinguish item and axis click with distinct
onItemClick
andonAxisClick
This should allow to setup good default styling:
onAxisClick
is defined the drawing area getcursor: pointer
onItemClick
is defined the drawing area getcursor: pointer
when the activeitem
is notnull
.Idea 2
A second one is to add
onClick
to element slots with additional data. Such that for simple use cases, Devs could doNote
For now I did not update the Pie chart interaction, but will have to be coherent with other charts API
Changelog
🚀 All charts have click handler. Test their respective docs demonstration to know more about the data format:
Big thanks to @giladappsforce and @yaredtsy for their contribution on exploring this feature
Breaking change
Pie charts
onClick
get renamedonItemClick
for consistency with other charts click callback.