Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[charts] Use voronoi cells to trigger interaction with scatter items #10981

Merged
merged 14 commits into from
Dec 8, 2023

Conversation

alexfauquette
Copy link
Member

@alexfauquette alexfauquette commented Nov 9, 2023

Fix #9358

Changelog

  • 馃殌 The scatter charts now use voronoi to trigger items

Users needed to hover the item to highlight the scatter item or show the tooltip.
Now they can interact with data by triggering the closest element. See the docs page for more info.

@alexfauquette alexfauquette marked this pull request as draft November 9, 2023 16:53
@alexfauquette alexfauquette added the component: charts This is the name of the generic UI component, not the React module! label Nov 9, 2023
@alexfauquette alexfauquette changed the title [charts] Use voronoi cells to trigger interaction with scatter items [WIP][charts] Use voronoi cells to trigger interaction with scatter items Nov 9, 2023
@mui-bot
Copy link

mui-bot commented Nov 9, 2023

Deploy preview: https://deploy-preview-10981--material-ui-x.netlify.app/

Updated pages:

Generated by 馃毇 dangerJS against 1167199

@github-actions github-actions bot added the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 20, 2023
Copy link

This pull request has conflicts, please resolve those before we can evaluate the pull request.

@github-actions github-actions bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged label Nov 30, 2023
docs/data/charts/scatter/scatter.md Outdated Show resolved Hide resolved
docs/data/charts/scatter/scatter.md Outdated Show resolved Hide resolved
docs/data/charts/scatter/scatter.md Outdated Show resolved Hide resolved
docs/data/charts/scatter/scatter.md Outdated Show resolved Hide resolved
@@ -94,7 +95,7 @@ function Scatter(props: ScatterProps) {
key={dataPoint.id}
cx={0}
cy={0}
r={markerSize}
r={(dataPoint.isHighlighted ? 1.2 : 1) * markerSize}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r={(dataPoint.isHighlighted ? 1.2 : 1) * markerSize}
r={markerSize}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Highcharts also do increase the point radius, when highlighted, but that is probably a separate story/functionality and would need to be controllable. 馃し 馃檲

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so we agree it's interesting to discuss this point, but not for this PR :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, clearly. 馃憤

@@ -71,6 +93,9 @@ const dataReducer: React.Reducer<Omit<InteractionState, 'dispatch'>, Interaction
return { ...prevState, item: null };

case 'updateAxis':
if (action.data.x === prevState.axis.x && action.data.y === prevState.axis.y) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This update is not directly for voronoi. It's a way to "improve perf" If axis do not change, we do not create a new object to avoid triggering rerender at each mouse movement

@@ -5,6 +5,9 @@ import { SVGContext, DrawingContext } from '../context/DrawingProvider';
import { isBandScale } from '../internals/isBandScale';
import { AxisDefaultized } from '../models/axis';

function getAsANumber(value: number | Date) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix TS errors

alexfauquette and others added 2 commits December 4, 2023 12:12
Signed-off-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
@alexfauquette alexfauquette changed the title [WIP][charts] Use voronoi cells to trigger interaction with scatter items [charts] Use voronoi cells to trigger interaction with scatter items Dec 4, 2023
@alexfauquette alexfauquette marked this pull request as ready for review December 4, 2023 11:21
@LukasTy LukasTy added the enhancement This is not a bug, nor a new feature label Dec 7, 2023
Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a major improvement, great work! 馃挴 馃挋
Leaving some nitpicks. 馃槈

docs/data/charts/scatter/scatter.md Outdated Show resolved Hide resolved
docs/data/charts/scatter/VoronoiInteraction.tsx Outdated Show resolved Hide resolved
docs/data/charts/scatter/scatter.md Outdated Show resolved Hide resolved
docs/data/charts/scatter/scatter.md Outdated Show resolved Hide resolved
// TODO: A perf optimisation of voronoi could be to use the last point as the intial point for the next search.
const handleMouseMove = (event: MouseEvent) => {
// Get mouse coordinate in global SVG space
const pt = svgRef.current!.createSVGPoint();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Have you considered using a full word (i.e.: point, svgPoint) for this variable?
Or do you have an argument as to why the shorthand form is better? 馃

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mostly because it's a trash variable. I create it to access the matrixTransform method and then it's over, So I avoid to pick a meaining full variable name

An option could be to create a helper getSVGPoint

const svgPoint = getSVGPoint(svgRef, event)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. 馃憣
Yup, the suggested helper does sound nice. 馃憤

/**
* Defines the maximal distance between a scatter point and the pointer that triggers the interaction.
* If `undefined`, the radius is assumed to be infinite.
* @default undefined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered going with a specific default? 馃
I checked that Highcharts also have the same behavior, but when exploring the demo, I felt that 25 seemed the most natural to me. 馃 馃し
What do others think? @flaviendelangle @joserodolfofreitas?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not considered it because nivo don't even allow this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, if it's only my opinion, we can disregard it. There is nothing wrong with this default. 馃憣
In that case, WDYT about aligning the demo with defaults?
It's what initially tripped me a bit because after seeing the demo I expected the default radius to be 50. 馃檲

@@ -94,7 +95,7 @@ function Scatter(props: ScatterProps) {
key={dataPoint.id}
cx={0}
cy={0}
r={markerSize}
r={(dataPoint.isHighlighted ? 1.2 : 1) * markerSize}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Highcharts also do increase the point radius, when highlighted, but that is probably a separate story/functionality and would need to be controllable. 馃し 馃檲

packages/x-charts/src/ScatterChart/ScatterChart.tsx Outdated Show resolved Hide resolved
packages/x-charts/src/context/InteractionProvider.tsx Outdated Show resolved Hide resolved
alexfauquette and others added 5 commits December 7, 2023 14:40
Co-authored-by: Lukas <llukas.tyla@gmail.com>
Signed-off-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
@alexfauquette alexfauquette merged commit 5196d57 into mui:next Dec 8, 2023
17 checks passed
@alexfauquette alexfauquette deleted the scatter-interaction branch December 8, 2023 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: charts This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[charts] Improve tooltip trigger in scatter charts
3 participants