[charts] Provide dataIndex in LinePlot and AreaPlot onItemClick#23144
Conversation
When clicking a line or area path, the onItemClick callback only received the seriesId, making it impossible to know which data point the click was closest to. This was especially limiting when using composition, where the LinePlot/AreaPlot onItemClick props are the only item click handlers available. Derive dataIndex from the click position using the same closest-point logic as the axis interaction (getAxisIndex), resolved against the series' own x-axis. Fixes mui#12840 Co-Authored-By: Claude Fable 5 <[email protected]>
Deploy previewhttps://deploy-preview-23144--material-ui-x.netlify.app/Bundle size
PerformanceTotal duration: 1,580.33 ms -184.86 ms(-10.5%) | Renders: 63 (+0) | Paint: 2,196.84 ms -280.74 ms(-11.3%)
…and 1 more (+20 within noise) — details Check out the code infra dashboard for more information about this PR. |
probably just a fluke, there were some similar problems in other prs |
Introduce LineItemIdentifierWithData with a required dataIndex for the LinePlot, AreaPlot, and MarkPlot onItemClick callbacks, and use it as the line series itemIdentifierWithData like other cartesian series. The click handler now skips the callback in the pathological case where no data point can be resolved instead of delivering undefined.
| * An object that identifies a single line together with the data point the interaction targets. | ||
| * Used for item interactions that always resolve a data point, like `onItemClick`. | ||
| */ | ||
| export type LineItemIdentifierWithData = { |
There was a problem hiding this comment.
I don't think the naming is correct. The WithData suffix is already used in the sankey when the identifier as additional data:
XxxIdentifieris the minimal piece of information needed to identify an itemXxxIdentifierWithDatais the same identifier plus some extra data like the value of a link
Here you don't add extra information to an identifier, you make it more restrictive
IMO it would make more sens to introduce a LineItemClickIdentifier type
There was a problem hiding this comment.
Idea was to mainly keep the same patterns we use, but no strong feelings
There was a problem hiding this comment.
It's mostly to prevent future conflict with the other part that rely on the identifiers (highligh, focus, and tooltip) that for now uses the presence of dataIndex to distinguish an interaction on the series from an interaction on the mark
Fixes #12840
When using composition,
LinePlotandAreaPlotonItemClickcallbacks (and thereforeLineChart'sonLineClick/onAreaClick) delivereddataIndex: undefinedwhen clicking on a path.This PR adds an internal
useLineItemClickHandlerhook that computes thedataIndexfrom the click's x position using the existinggetAxisIndexinversion — the same closest-point semantics used by the axis tooltip, highlight, andonAxisClick— resolved against the clicked series' own x-axis. Both plots now use it.Out of scope (per the maintainer discussion in the issue): click tolerance near a line without hitting the path — that is covered by the experimental position-based pointer interaction feature. Container-level
onAxisClickunder composition already works on master; a regression test for it is included.Changelog
LinePlotandAreaPlotonItemClick(andLineChart'sonLineClick/onAreaClick) now provide thedataIndexof the closest data point to the click.The identifier passed to
LinePlot,AreaPlot, andMarkPlotonItemClickis now typed as the newLineItemClickIdentifier, wheredataIndexis required. The callback is no longer fired in the pathological case where no data point can be resolved from the click position.