From 69ea3dbfb0a14908d6d5b1b1f126be003b62ce47 Mon Sep 17 00:00:00 2001 From: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Date: Thu, 1 Feb 2024 13:38:58 +0100 Subject: [PATCH] [charts] Fix Tooltip crash with out of range lines (#11898) --- packages/x-charts/src/BarChart/formatter.ts | 2 +- .../ChartsAxisTooltipContent.tsx | 29 ++++++++----------- .../DefaultChartsAxisTooltipContent.tsx | 7 +++-- packages/x-charts/src/ChartsTooltip/utils.tsx | 16 +++++++++- packages/x-charts/src/LineChart/formatter.ts | 2 +- 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/packages/x-charts/src/BarChart/formatter.ts b/packages/x-charts/src/BarChart/formatter.ts index 12f08d21692e3..3a2253a689433 100644 --- a/packages/x-charts/src/BarChart/formatter.ts +++ b/packages/x-charts/src/BarChart/formatter.ts @@ -86,7 +86,7 @@ const formatter: Formatter<'bar'> = (params, dataset) => { return { seriesOrder, stackingGroups, - series: defaultizeValueFormatter(completedSeries, (v) => v?.toLocaleString()), + series: defaultizeValueFormatter(completedSeries, (v) => (v == null ? '' : v.toLocaleString())), }; }; diff --git a/packages/x-charts/src/ChartsTooltip/ChartsAxisTooltipContent.tsx b/packages/x-charts/src/ChartsTooltip/ChartsAxisTooltipContent.tsx index 9b0109cfefb14..3d2bd104c2ec5 100644 --- a/packages/x-charts/src/ChartsTooltip/ChartsAxisTooltipContent.tsx +++ b/packages/x-charts/src/ChartsTooltip/ChartsAxisTooltipContent.tsx @@ -5,14 +5,11 @@ import { useSlotProps } from '@mui/base/utils'; import { AxisInteractionData } from '../context/InteractionProvider'; import { SeriesContext } from '../context/SeriesContextProvider'; import { CartesianContext } from '../context/CartesianContextProvider'; -import { - CartesianChartSeriesType, - ChartSeriesDefaultized, - ChartSeriesType, -} from '../models/seriesType/config'; +import { ChartSeriesDefaultized, ChartSeriesType } from '../models/seriesType/config'; import { AxisDefaultized } from '../models/axis'; import { ChartsTooltipClasses } from './chartsTooltipClasses'; import { DefaultChartsAxisTooltipContent } from './DefaultChartsAxisTooltipContent'; +import { isCartesianSeriesType } from './utils'; export type ChartsAxisContentProps = { /** @@ -63,19 +60,17 @@ function ChartsAxisTooltipContent(props: { const relevantSeries = React.useMemo(() => { const rep: any[] = []; - ( - Object.keys(series).filter((seriesType) => - ['bar', 'line', 'scatter'].includes(seriesType), - ) as CartesianChartSeriesType[] - ).forEach((seriesType) => { - series[seriesType]!.seriesOrder.forEach((seriesId) => { - const item = series[seriesType]!.series[seriesId]; - const axisKey = isXaxis ? item.xAxisKey : item.yAxisKey; - if (axisKey === undefined || axisKey === USED_AXIS_ID) { - rep.push(series[seriesType]!.series[seriesId]); - } + Object.keys(series) + .filter(isCartesianSeriesType) + .forEach((seriesType) => { + series[seriesType]!.seriesOrder.forEach((seriesId) => { + const item = series[seriesType]!.series[seriesId]; + const axisKey = isXaxis ? item.xAxisKey : item.yAxisKey; + if (axisKey === undefined || axisKey === USED_AXIS_ID) { + rep.push(series[seriesType]!.series[seriesId]); + } + }); }); - }); return rep; }, [USED_AXIS_ID, isXaxis, series]); diff --git a/packages/x-charts/src/ChartsTooltip/DefaultChartsAxisTooltipContent.tsx b/packages/x-charts/src/ChartsTooltip/DefaultChartsAxisTooltipContent.tsx index 27ca0f3158cc7..f7e4d43c613c0 100644 --- a/packages/x-charts/src/ChartsTooltip/DefaultChartsAxisTooltipContent.tsx +++ b/packages/x-charts/src/ChartsTooltip/DefaultChartsAxisTooltipContent.tsx @@ -2,7 +2,6 @@ import * as React from 'react'; import PropTypes from 'prop-types'; import clsx from 'clsx'; import Typography from '@mui/material/Typography'; -import { ChartSeriesDefaultized } from '../models/seriesType/config'; import { ChartsTooltipCell, ChartsTooltipPaper, @@ -11,6 +10,7 @@ import { ChartsTooltipRow, } from './ChartsTooltipTable'; import type { ChartsAxisContentProps } from './ChartsAxisTooltipContent'; +import { isCartesianSeries } from './utils'; function DefaultChartsAxisTooltipContent(props: ChartsAxisContentProps) { const { series, axis, dataIndex, axisValue, sx, classes } = props; @@ -33,8 +33,9 @@ function DefaultChartsAxisTooltipContent(props: ChartsAxisContentProps) { )}
- {series.map(({ color, id, label, valueFormatter, data }: ChartSeriesDefaultized