Skip to content

[charts] Fix zoom filter regression #16507

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 3 commits into from
Feb 7, 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
@@ -1,29 +1,13 @@
import {
ChartRootSelector,
createSelector,
selectorChartRawXAxis,
selectorChartRawYAxis,
selectorChartZoomOptionsLookup,
} 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('x'));

const selectorChartYZoomOptionsLookup = createSelector(selectorChartRawYAxis, creatZoomLookup('y'));

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

export const selectorChartZoomData = createSelector(
selectorChartZoomState,
(zoom) => zoom.zoomData,
);

export const selectorChartZoomIsInteracting = createSelector(
selectorChartZoomState,
(zoom) => zoom.isInteracting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
getSVGPoint,
selectorChartDrawingArea,
ZoomData,
creatZoomLookup,
selectorChartZoomOptionsLookup,
} from '@mui/x-charts/internals';
import { UseChartProZoomSignature } from './useChartProZoom.types';
import { creatZoomLookup } from './creatZoomLookup';
import {
getDiff,
getHorizontalCenterRatio,
Expand All @@ -21,7 +22,6 @@ import {
preventDefault,
zoomAtPoint,
} from './useChartProZoom.utils';
import { selectorChartZoomOptionsLookup } from './useChartProZoom.selectors';

// It is helpful to avoid the need to provide the possibly auto-generated id for each axis.
function initializeZoomData(options: Record<AxisId, DefaultizedZoomOptions>) {
Expand Down Expand Up @@ -372,24 +372,20 @@ useChartProZoom.params = {
};

useChartProZoom.getDefaultizedParams = ({ params }) => {
const optionsLookup = {
...creatZoomLookup('x')(params.defaultizedXAxis),
...creatZoomLookup('y')(params.defaultizedYAxis),
};

return {
...params,
optionsLookup,
};
};

useChartProZoom.getInitialState = (params) => {
const optionsLookup = {
...creatZoomLookup('x')(params.defaultizedXAxis),
...creatZoomLookup('y')(params.defaultizedYAxis),
};
return {
zoom: {
zoomData:
params.initialZoom === undefined
? initializeZoomData(params.optionsLookup)
: params.initialZoom,
params.initialZoom === undefined ? initializeZoomData(optionsLookup) : params.initialZoom,
isInteracting: false,
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
AxisId,
UseChartSeriesSignature,
ChartPluginSignature,
DefaultizedZoomOptions,
UseChartCartesianAxisSignature,
UseChartCartesianAxisDefaultizedParameters,
ZoomData,
Expand All @@ -22,12 +20,7 @@ export interface UseChartProZoomParameters {
}

export type UseChartProZoomDefaultizedParameters = UseChartProZoomParameters &
UseChartCartesianAxisDefaultizedParameters & {
/**
* The zoom options for each axis.
*/
optionsLookup: Record<AxisId, DefaultizedZoomOptions>;
};
UseChartCartesianAxisDefaultizedParameters;

export interface UseChartProZoomState {
zoom: {
Expand Down

This file was deleted.

7 changes: 2 additions & 5 deletions packages/x-charts-pro/src/typeOverloads/modules.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { DefaultizedProps } from '@mui/x-internals/types';
import { AxisId } from '@mui/x-charts/internals';
import { AxisId, ZoomOptions } from '@mui/x-charts/internals';
import {
HeatmapItemIdentifier,
HeatmapSeriesType,
DefaultizedHeatmapSeriesType,
HeatmapValueType,
} from '../models/seriesType/heatmap';
import { ZoomOptions as ZoomOptionsPro } from '../internals/plugins/useChartProZoom/zoom.types';

declare module '@mui/x-charts/internals' {
interface ChartsSeriesConfig {
Expand All @@ -20,9 +19,7 @@ declare module '@mui/x-charts/internals' {
};
}

interface ZoomOptions extends ZoomOptionsPro {}

interface DefaultizedZoomOptions extends Required<ZoomOptionsPro> {
interface DefaultizedZoomOptions extends Required<ZoomOptions> {
axisId: AxisId;
axisDirection: 'x' | 'y';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { AxisId, DefaultizedZoomOptions } from '@mui/x-charts/internals';
import { AxisConfig, ChartsXAxisProps, ChartsYAxisProps, ScaleName } from '@mui/x-charts/models';
import { AxisConfig, ChartsXAxisProps, ChartsYAxisProps, ScaleName } from '../../../../models';
import { AxisId } from '../../../../models/axis';
import { defaultizeZoom } from './defaultizeZoom';
import { DefaultizedZoomOptions } from './useChartCartesianAxis.types';

export const creatZoomLookup =
(axisDirection: 'x' | 'y') =>
(axes: AxisConfig<ScaleName, any, ChartsXAxisProps | ChartsYAxisProps>[]) =>
(axes: AxisConfig<ScaleName, any, ChartsXAxisProps | ChartsYAxisProps>[] = []) =>
axes.reduce<Record<AxisId, DefaultizedZoomOptions>>((acc, v) => {
// @ts-ignore
const { zoom, id: axisId } = v;
const defaultizedZoom = defaultizeZoom(zoom, axisId, axisDirection);
if (defaultizedZoom) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { AxisId, DefaultizedZoomOptions, ZoomOptions } from '@mui/x-charts/internals';
import { AxisId } from '../../../../models/axis';
import { DefaultizedZoomOptions } from './useChartCartesianAxis.types';
import { ZoomOptions } from './zoom.types';

const defaultZoomOptions: Required<ZoomOptions> = {
minStart: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export type * from './useChartCartesianAxis.types';
export * from './useChartCartesianAxis.selectors';
export { defaultizeAxis } from './defaultizeAxis';
export * from './computeAxisValue';
export * from './creatZoomLookup';
Copy link
Member

Choose a reason for hiding this comment

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

Small typo in file name: createZoomLookup

export * from './zoom.types';
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { UseChartCartesianAxisSignature } from './useChartCartesianAxis.types';
import { ChartState } from '../../models/chart';
import { createAxisFilterMapper, createGetAxisFilters } from './createAxisFilterMapper';
import { ZoomAxisFilters, ZoomData } from './zoom.types';
import { creatZoomLookup } from './creatZoomLookup';

export const createZoomMap = (zoom: ZoomData[]) => {
const zoomItemMap = new Map<AxisId, ZoomData>();
Expand Down Expand Up @@ -44,7 +45,14 @@ const selectorChartZoomMap = createSelector(
(zoom) => zoom?.zoomData && createZoomMap(zoom?.zoomData),
);

const selectorChartZoomOptionsLookup = createSelector(selectorChartZoomState, () => undefined);
const selectorChartXZoomOptionsLookup = createSelector(selectorChartRawXAxis, creatZoomLookup('x'));

const selectorChartYZoomOptionsLookup = createSelector(selectorChartRawYAxis, creatZoomLookup('y'));

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

const selectorChartXFilter = createSelector(
[
Expand Down Expand Up @@ -93,7 +101,6 @@ 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 @@ -102,7 +109,6 @@ 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 @@ -7,7 +7,57 @@ export type ZoomData = { axisId: AxisId; start: number; end: number };
export type ZoomFilterMode = 'keep' | 'discard';

export interface ZoomOptions {
filterMode?: ZoomFilterMode;
/**
* The starting percentage of the zoom range. In the range of 0 to 100.
*
* @default 0
*/
minStart?: number;
/**
* The ending percentage of the zoom range. In the range of 0 to 100.
*
* @default 100
*/
maxEnd?: number;
/**
* The step size of the zooming function. Defines the granularity of the zoom.
*
* @default 5
*/
step?: number;
/**
* Restricts the minimum span size in the range of 0 to 100.
*
* If the span size is smaller than the minSpan, the span will be resized to the minSpan.
*
* @default 10
*/
minSpan?: number;
/**
* Restricts the maximum span size in the range of 0 to 100.
*
* If the span size is larger than the maxSpan, the span will be resized to the maxSpan.
*
* @default 100
*/
maxSpan?: number;
/**
* Set to `false` to disable panning. Useful when you want to pan programmatically,
* or to show only a specific section of the chart.
*
* @default true
*/
panning?: boolean;
/**
* Defines how to filter the axis data when it is outside of the zoomed range of this axis.
*
* - `keep`: The data outside the zoomed range is kept. And the other axes will stay the same.
* - `discard`: The data outside the zoomed range is discarded for the other axes.
* The other axes will be adjusted to fit the zoomed range.
*
* @default 'keep'
*/
filterMode?: 'discard' | 'keep';
}

export type ZoomAxisFilters = Record<AxisId, ExtremumFilter>;
Expand Down