From 7c4c924302e43376a66d511cff8f8ef32f62c8a2 Mon Sep 17 00:00:00 2001 From: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Date: Fri, 8 Nov 2024 12:25:36 +0100 Subject: [PATCH] [charts] Fix log scale with `null` data (#15337) --- packages/x-charts/src/BarChart/extremums.test.ts | 1 + packages/x-charts/src/LineChart/extremums.ts | 8 ++++++-- .../src/context/PluginProvider/ExtremumGetter.types.ts | 4 ++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/x-charts/src/BarChart/extremums.test.ts b/packages/x-charts/src/BarChart/extremums.test.ts index f0ba41d65048..b3d861ddedac 100644 --- a/packages/x-charts/src/BarChart/extremums.test.ts +++ b/packages/x-charts/src/BarChart/extremums.test.ts @@ -20,6 +20,7 @@ const buildData = ( ] : [], layout, + valueFormatter: () => '', }, }, axis: { diff --git a/packages/x-charts/src/LineChart/extremums.ts b/packages/x-charts/src/LineChart/extremums.ts index 1640dbc7b91d..3330f58e8e6e 100644 --- a/packages/x-charts/src/LineChart/extremums.ts +++ b/packages/x-charts/src/LineChart/extremums.ts @@ -12,11 +12,15 @@ type GetValues = (d: [number, number]) => [number, number]; function getSeriesExtremums( getValues: GetValues, + data: (number | null)[], stackedData: [number, number][], filter?: ExtremumFilter, ): [number, number] { return stackedData.reduce<[number, number]>( (seriesAcc, stackedValue, index) => { + if (data[index] === null) { + return seriesAcc; + } const [base, value] = getValues(stackedValue); if ( filter && @@ -41,7 +45,7 @@ export const getExtremumY: ExtremumGetter<'line'> = (params) => { }) .reduce( (acc, seriesId) => { - const { area, stackedData } = series[seriesId]; + const { area, stackedData, data } = series[seriesId]; const isArea = area !== undefined; const filter = getFilters?.({ @@ -57,7 +61,7 @@ export const getExtremumY: ExtremumGetter<'line'> = (params) => { ? (d) => d : (d) => [d[1], d[1]]; - const seriesExtremums = getSeriesExtremums(getValues, stackedData, filter); + const seriesExtremums = getSeriesExtremums(getValues, data, stackedData, filter); const [seriesMin, seriesMax] = seriesExtremums; return [Math.min(seriesMin, acc[0]), Math.max(seriesMax, acc[1])]; diff --git a/packages/x-charts/src/context/PluginProvider/ExtremumGetter.types.ts b/packages/x-charts/src/context/PluginProvider/ExtremumGetter.types.ts index 86257071fbbb..744aa3fd066b 100644 --- a/packages/x-charts/src/context/PluginProvider/ExtremumGetter.types.ts +++ b/packages/x-charts/src/context/PluginProvider/ExtremumGetter.types.ts @@ -1,6 +1,6 @@ import type { CartesianChartSeriesType, - ChartSeries, + ChartSeriesDefaultized, ChartSeriesType, } from '../../models/seriesType/config'; import type { AxisConfig, AxisId } from '../../models/axis'; @@ -11,7 +11,7 @@ export type ExtremumGettersConfig = { - series: Record>; + series: Record>; axis: AxisConfig; axisIndex: number; isDefaultAxis: boolean;