Skip to content

Commit 33e0b7f

Browse files
[charts] Improve axis tooltip performances (#17398)
1 parent 152ab6a commit 33e0b7f

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

packages/x-charts/src/internals/plugins/featurePlugins/useChartCartesianAxis/useChartCartesianInteraction.selectors.ts

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isDeepEqual } from '@mui/x-internals/isDeepEqual';
12
import { createSelector } from '../../utils/selectors';
23
import { AxisId, ChartsAxisProps } from '../../../../models/axis';
34
import {
@@ -109,9 +110,22 @@ export const selectorChartsInteractionTooltipXAxes = createSelector(
109110

110111
return axes.axisIds
111112
.filter((id) => axes.axis[id].triggerTooltip)
112-
.map((axisId) => ({ axisId, dataIndex: getAxisIndex(axes.axis[axisId], value) }))
113+
.map(
114+
(axisId): AxisItemIdentifier => ({
115+
axisId,
116+
dataIndex: getAxisIndex(axes.axis[axisId], value),
117+
}),
118+
)
113119
.filter(({ dataIndex }) => dataIndex >= 0);
114120
},
121+
{
122+
memoizeOptions: {
123+
// Keep the same reference if array content is the same.
124+
// If possible, avoid this pattern by creating selectors that
125+
// uses string/number as arguments.
126+
resultEqualityCheck: isDeepEqual,
127+
},
128+
},
115129
);
116130

117131
/**
@@ -126,9 +140,22 @@ export const selectorChartsInteractionTooltipYAxes = createSelector(
126140

127141
return axes.axisIds
128142
.filter((id) => axes.axis[id].triggerTooltip)
129-
.map((axisId) => ({ axisId, dataIndex: getAxisIndex(axes.axis[axisId], value) }))
143+
.map(
144+
(axisId): AxisItemIdentifier => ({
145+
axisId,
146+
dataIndex: getAxisIndex(axes.axis[axisId], value),
147+
}),
148+
)
130149
.filter(({ dataIndex }) => dataIndex >= 0);
131150
},
151+
{
152+
memoizeOptions: {
153+
// Keep the same reference if array content is the same.
154+
// If possible, avoid this pattern by creating selectors that
155+
// uses string/number as arguments.
156+
resultEqualityCheck: isDeepEqual,
157+
},
158+
},
132159
);
133160

134161
/**
@@ -138,3 +165,5 @@ export const selectorChartsInteractionAxisTooltip = createSelector(
138165
[selectorChartsInteractionTooltipXAxes, selectorChartsInteractionTooltipYAxes],
139166
(xTooltip, yTooltip) => xTooltip.length > 0 || yTooltip.length > 0,
140167
);
168+
169+
export type AxisItemIdentifier = { axisId: string; dataIndex: number };

packages/x-charts/src/internals/plugins/featurePlugins/useChartPolarAxis/useChartPolarInteraction.selectors.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { isDeepEqual } from '@mui/x-internals/isDeepEqual';
12
import { AxisId, ChartsAxisProps } from '../../../../models/axis';
23
import { createSelector } from '../../utils/selectors';
4+
import type { AxisItemIdentifier } from '../useChartCartesianAxis/useChartCartesianInteraction.selectors';
35
import {
46
selectorChartsInteractionPointerX,
57
selectorChartsInteractionPointerY,
@@ -96,9 +98,17 @@ export const selectorChartsInteractionTooltipRotationAxes = createSelector(
9698
}
9799

98100
return axes.axisIds
99-
.map((axisId, axisIndex) => ({ axisId, dataIndex: indexes[axisIndex] }))
101+
.map((axisId, axisIndex): AxisItemIdentifier => ({ axisId, dataIndex: indexes[axisIndex] }))
100102
.filter(({ axisId, dataIndex }) => axes.axis[axisId].triggerTooltip && dataIndex >= 0);
101103
},
104+
{
105+
memoizeOptions: {
106+
// Keep the same reference if array content is the same.
107+
// If possible, avoid this pattern by creating selectors that
108+
// uses string/number as arguments.
109+
resultEqualityCheck: isDeepEqual,
110+
},
111+
},
102112
);
103113

104114
/**

0 commit comments

Comments
 (0)