Skip to content

[charts] Provide dataIndex in LinePlot and AreaPlot onItemClick#23144

Merged
JCQuintas merged 5 commits into
mui:masterfrom
JCQuintas:fix/charts-lineplot-onitemclick-dataindex
Jul 16, 2026
Merged

[charts] Provide dataIndex in LinePlot and AreaPlot onItemClick#23144
JCQuintas merged 5 commits into
mui:masterfrom
JCQuintas:fix/charts-lineplot-onitemclick-dataindex

Conversation

@JCQuintas

@JCQuintas JCQuintas commented Jul 14, 2026

Copy link
Copy Markdown
Member

Fixes #12840

When using composition, LinePlot and AreaPlot onItemClick callbacks (and therefore LineChart's onLineClick/onAreaClick) delivered dataIndex: undefined when clicking on a path.

This PR adds an internal useLineItemClickHandler hook that computes the dataIndex from the click's x position using the existing getAxisIndex inversion — the same closest-point semantics used by the axis tooltip, highlight, and onAxisClick — 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 onAxisClick under composition already works on master; a regression test for it is included.

Changelog

LinePlot and AreaPlot onItemClick (and LineChart's onLineClick/onAreaClick) now provide the dataIndex of the closest data point to the click.

The identifier passed to LinePlot, AreaPlot, and MarkPlot onItemClick is now typed as the new LineItemClickIdentifier, where dataIndex is required. The callback is no longer fired in the pathological case where no data point can be resolved from the click position.

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]>
@JCQuintas
JCQuintas requested a review from alexfauquette as a code owner July 14, 2026 09:35
@code-infra-dashboard

code-infra-dashboard Bot commented Jul 14, 2026

Copy link
Copy Markdown

Deploy preview

https://deploy-preview-23144--material-ui-x.netlify.app/
QR code for https://deploy-preview-23144--material-ui-x.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/x-data-grid 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-pro 0B(0.00%) 0B(0.00%)
@mui/x-data-grid-premium 0B(0.00%) 0B(0.00%)
@mui/x-charts 🔺+262B(+0.07%) 🔺+112B(+0.09%)
@mui/x-charts-pro 🔺+262B(+0.05%) 🔺+101B(+0.06%)
@mui/x-charts-premium 🔺+262B(+0.04%) 🔺+110B(+0.06%)
@mui/x-date-pickers 0B(0.00%) 0B(0.00%)
@mui/x-date-pickers-pro 0B(0.00%) 0B(0.00%)
@mui/x-tree-view 0B(0.00%) 0B(0.00%)
@mui/x-tree-view-pro 0B(0.00%) 0B(0.00%)
@mui/x-scheduler 0B(0.00%) 0B(0.00%)
@mui/x-scheduler-premium 0B(0.00%) 0B(0.00%)
@mui/x-chat 0B(0.00%) 0B(0.00%)
@mui/x-license 0B(0.00%) 0B(0.00%)

Details of bundle changes

Performance

Total duration: 1,580.33 ms -184.86 ms(-10.5%) | Renders: 63 (+0) | Paint: 2,196.84 ms -280.74 ms(-11.3%)

Test Duration Renders
BarChart with big data amount 81.69 ms ▼-40.07 ms(-32.9%) 2 (+0)
Area chart with big data amount (no marks) 45.93 ms ▼-24.58 ms(-34.9%) 2 (+0)
ScatterChartPro with big data amount and zoomed in (batch renderer) 24.28 ms ▼-8.41 ms(-25.7%) 2 (+0)
RadialBarChart stacked with multiple series 22.64 ms ▼-7.95 ms(-26.0%) 2 (+0)
RadarChart with many axes and multiple series 6.13 ms ▼-1.92 ms(-23.9%) 2 (+0)

…and 1 more (+20 within noise) — details


Check out the code infra dashboard for more information about this PR.

@JCQuintas JCQuintas self-assigned this Jul 14, 2026
@JCQuintas JCQuintas added type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. scope: charts Changes related to the charts. type: bug It doesn't behave as expected. and removed type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. labels Jul 14, 2026

@alexfauquette alexfauquette left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we could add a dedicated type for onItemClick context where the dataIndex is requireded

Looks like the build failed. Not sure if it's a flacky CI or real issue

@JCQuintas

Copy link
Copy Markdown
Member Author

flacky CI or real issue

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 = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think the naming is correct. The WithData suffix is already used in the sankey when the identifier as additional data:

  • XxxIdentifier is the minimal piece of information needed to identify an item
  • XxxIdentifierWithData is 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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Idea was to mainly keep the same patterns we use, but no strong feelings

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

@JCQuintas
JCQuintas merged commit efda33f into mui:master Jul 16, 2026
23 checks passed
@JCQuintas
JCQuintas deleted the fix/charts-lineplot-onitemclick-dataindex branch July 16, 2026 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: charts Changes related to the charts. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[charts] LinePlot onItemClick should provide dataIndex property

2 participants