Skip to content

Commit

Permalink
[charts] Pass the axis index to extremum getter (mui#14641)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfauquette authored Sep 16, 2024
1 parent c5e0158 commit edec100
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const createAxisFilterMapper =
if (scaleType === 'point' || scaleType === 'band') {
extremums = [0, (axis.data?.length ?? 1) - 1];
} else {
extremums = getAxisExtremum(axis, extremumGetter, axisIndex === 0, formattedSeries);
extremums = getAxisExtremum(axis, extremumGetter, axisIndex, formattedSeries);
}

let min: number | Date;
Expand Down
1 change: 1 addition & 0 deletions packages/x-charts/src/BarChart/extremums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const buildData = (
id: 'id',
data,
},
axisIndex: 0,
isDefaultAxis: true,
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export function computeValue({
const completeAxis: DefaultizedAxisConfig<ChartsAxisProps> = {};
allAxis.forEach((eachAxis, axisIndex) => {
const axis = eachAxis as Readonly<AxisConfig<ScaleName, any, Readonly<ChartsAxisProps>>>;
const isDefaultAxis = axisIndex === 0;
const zoomOption = zoomOptions?.[axis.id];
const zoom = zoomData?.find(({ axisId }) => axisId === axis.id);
const zoomRange: [number, number] = zoom ? [zoom.start, zoom.end] : [0, 100];
Expand All @@ -98,7 +97,7 @@ export function computeValue({
const [minData, maxData] = getAxisExtremum(
axis,
extremumGetters,
isDefaultAxis,
axisIndex,
formattedSeries,
zoom === undefined && !zoomOption ? getFilters : undefined, // Do not apply filtering if zoom is already defined.
);
Expand Down
17 changes: 5 additions & 12 deletions packages/x-charts/src/context/CartesianProvider/getAxisExtremum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const axisExtremumCallback = <T extends CartesianChartSeriesType>(
chartType: T,
axis: AxisConfig,
getters: ExtremumGettersConfig<T>,
isDefaultAxis: boolean,
axisIndex: number,
formattedSeries: FormattedSeries,
getFilters?: GetZoomAxisFilters,
): ExtremumGetterResult => {
Expand All @@ -19,7 +19,8 @@ const axisExtremumCallback = <T extends CartesianChartSeriesType>(
const [minChartTypeData, maxChartTypeData] = getter?.({
series,
axis,
isDefaultAxis,
axisIndex,
isDefaultAxis: axisIndex === 0,
getFilters,
}) ?? [Infinity, -Infinity];

Expand All @@ -31,23 +32,15 @@ const axisExtremumCallback = <T extends CartesianChartSeriesType>(
export const getAxisExtremum = (
axis: AxisConfig,
getters: ExtremumGettersConfig,
isDefaultAxis: boolean,
axisIndex: number,
formattedSeries: FormattedSeries,
getFilters?: GetZoomAxisFilters,
) => {
const charTypes = Object.keys(getters) as CartesianChartSeriesType[];

const extremums = charTypes.reduce<ExtremumGetterResult>(
(acc, charType) =>
axisExtremumCallback(
acc,
charType,
axis,
getters,
isDefaultAxis,
formattedSeries,
getFilters,
),
axisExtremumCallback(acc, charType, axis, getters, axisIndex, formattedSeries, getFilters),
[Infinity, -Infinity],
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type ExtremumGettersConfig<T extends ChartSeriesType = CartesianChartSeri
type ExtremumGetterParams<T extends ChartSeriesType> = {
series: Record<SeriesId, ChartSeries<T>>;
axis: AxisConfig;
axisIndex: number;
isDefaultAxis: boolean;
getFilters?: (params: {
currentAxisId: AxisId | undefined;
Expand Down

0 comments on commit edec100

Please sign in to comment.