Skip to content

[charts] Fix zoom option reactivity #16262

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

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { AxisId, DefaultizedZoomOptions } from '@mui/x-charts/internals';
import { AxisConfig, ChartsXAxisProps, ChartsYAxisProps, ScaleName } from '@mui/x-charts/models';
import { defaultizeZoom } from './defaultizeZoom';

export const creatZoomLookup = (
axes: AxisConfig<ScaleName, any, ChartsXAxisProps | ChartsYAxisProps>[],
) =>
axes.reduce<Record<AxisId, DefaultizedZoomOptions>>((acc, v) => {
const { zoom, id: axisId } = v;
const defaultizedZoom = defaultizeZoom(zoom, axisId, 'x');
if (defaultizedZoom) {
acc[axisId] = defaultizedZoom;
}
return acc;
}, {});
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { ChartRootSelector, createSelector } from '@mui/x-charts/internals';
import {
ChartRootSelector,
createSelector,
selectorChartRawXAxis,
selectorChartRawYAxis,
} from '@mui/x-charts/internals';
import { UseChartProZoomSignature } from './useChartProZoom.types';
import { creatZoomLookup } from './creatZoomLookup';

export const selectorChartZoomState: ChartRootSelector<UseChartProZoomSignature> = (state) =>
state.zoom;

const selectorChartXZoomOptionsLookup = createSelector(selectorChartRawXAxis, creatZoomLookup);

const selectorChartYZoomOptionsLookup = createSelector(selectorChartRawYAxis, creatZoomLookup);

export const selectorChartZoomOptionsLookup = createSelector(
selectorChartZoomState,
(zoom) => zoom.optionsLookup,
[selectorChartXZoomOptionsLookup, selectorChartYZoomOptionsLookup],
(xLookup, yLookup) => ({ ...xLookup, ...yLookup }),
);

export const selectorChartZoomData = createSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ZoomData,
} from '@mui/x-charts/internals';
import { UseChartProZoomSignature } from './useChartProZoom.types';
import { defaultizeZoom } from './defaultizeZoom';
import { creatZoomLookup } from './creatZoomLookup';
import {
getDiff,
getHorizontalCenterRatio,
Expand Down Expand Up @@ -80,6 +80,7 @@ export const useChartProZoom: ChartPlugin<UseChartProZoomSignature> = ({
() => Object.values(optionsLookup).some((v) => v.panning) || false,
[optionsLookup],
);

const isDraggingRef = React.useRef(false);
const touchStartRef = React.useRef<{ x: number; y: number; zoomData: ZoomData[] } | null>(null);
React.useEffect(() => {
Expand Down Expand Up @@ -372,22 +373,8 @@ useChartProZoom.params = {

useChartProZoom.getDefaultizedParams = ({ params }) => {
const optionsLookup = {
...params.defaultizedXAxis.reduce<Record<AxisId, DefaultizedZoomOptions>>((acc, v) => {
const { zoom, id: axisId } = v;
const defaultizedZoom = defaultizeZoom(zoom, axisId, 'x');
if (defaultizedZoom) {
acc[axisId] = defaultizedZoom;
}
return acc;
}, {}),
...params.defaultizedYAxis.reduce<Record<AxisId, DefaultizedZoomOptions>>((acc, v) => {
const { zoom, id: axisId } = v;
const defaultizedZoom = defaultizeZoom(zoom, axisId, 'y');
if (defaultizedZoom) {
acc[axisId] = defaultizedZoom;
}
return acc;
}, {}),
...creatZoomLookup(params.defaultizedXAxis),
...creatZoomLookup(params.defaultizedYAxis),
};

return {
Expand All @@ -399,7 +386,6 @@ useChartProZoom.getDefaultizedParams = ({ params }) => {
useChartProZoom.getInitialState = (params) => {
return {
zoom: {
optionsLookup: params.optionsLookup,
zoomData:
params.initialZoom === undefined
? initializeZoomData(params.optionsLookup)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ export type UseChartProZoomDefaultizedParameters = UseChartProZoomParameters &

export interface UseChartProZoomState {
zoom: {
/**
* The zoom options for each axis.
*/
optionsLookup: Record<AxisId, DefaultizedZoomOptions>;
/**
* Whether the user is currently interacting with the chart.
* This is useful to disable animations while the user is interacting.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { useChartCartesianAxis } from './useChartCartesianAxis';
export type * from './useChartCartesianAxis.types';
export * from './useChartCartesianAxis.selectors';
export { defaultizeAxis } from './defaultizeAxis';
export * from './computeAxisValue';
export * from './zoom.types';
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ const selectorChartZoomMap = createSelector(
(zoom) => zoom?.zoomData && createZoomMap(zoom?.zoomData),
);

const selectorChartZoomOptionsLookup = createSelector(
selectorChartZoomState,
(zoom) => zoom?.optionsLookup,
);
const selectorChartZoomOptionsLookup = createSelector(selectorChartZoomState, () => undefined);

const selectorChartXFilter = createSelector(
[
Expand Down Expand Up @@ -96,6 +93,7 @@ const selectorChartZoomAxisFilters = createSelector(
}

const xFilters = xAxis.reduce<ZoomAxisFilters>((acc, axis, index) => {
// @ts-expect-error The type is defined in the pro package
const filter = xMapper(axis, index);
if (filter !== null) {
acc[axis.id] = filter;
Expand All @@ -104,6 +102,7 @@ const selectorChartZoomAxisFilters = createSelector(
}, {});

const yFilters = yAxis.reduce<ZoomAxisFilters>((acc, axis, index) => {
// @ts-expect-error The type is defined in the pro package
const filter = yMapper(axis, index);
if (filter !== null) {
acc[axis.id] = filter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export interface UseChartCartesianAxisState {
* @ignore - state populated by the useChartProZoomPlugin
*/
zoom?: {
optionsLookup: Record<AxisId, DefaultizedZoomOptions>;
isInteracting: boolean;
zoomData: ZoomData[];
};
Expand Down
Loading