Skip to content
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

[charts] Fix Tooltip crash with out of range lines (@alexfauquette) #11901

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion packages/x-charts/src/BarChart/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
};
};

Expand Down
29 changes: 12 additions & 17 deletions packages/x-charts/src/ChartsTooltip/ChartsAxisTooltipContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand Down Expand Up @@ -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]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -33,8 +33,9 @@ function DefaultChartsAxisTooltipContent(props: ChartsAxisContentProps) {
)}

<tbody>
{series.map(({ color, id, label, valueFormatter, data }: ChartSeriesDefaultized<any>) => {
const formattedValue = valueFormatter(data[dataIndex]);
{series.filter(isCartesianSeries).map(({ color, id, label, valueFormatter, data }) => {
// @ts-ignore
const formattedValue = valueFormatter(data[dataIndex] ?? null);
if (formattedValue == null) {
return null;
}
Expand Down
16 changes: 15 additions & 1 deletion packages/x-charts/src/ChartsTooltip/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as React from 'react';
import { AxisInteractionData, ItemInteractionData } from '../context/InteractionProvider';
import { SVGContext } from '../context/DrawingProvider';
import { ChartSeriesType } from '../models/seriesType/config';
import {
CartesianChartSeriesType,
ChartSeriesDefaultized,
ChartSeriesType,
} from '../models/seriesType/config';

export function generateVirtualElement(mousePosition: { x: number; y: number } | null) {
if (mousePosition === null) {
Expand Down Expand Up @@ -85,3 +89,13 @@ export function getTooltipHasData(

return hasAxisXData || hasAxisYData;
}

export function isCartesianSeriesType(seriesType: string): seriesType is CartesianChartSeriesType {
return ['bar', 'line', 'scatter'].includes(seriesType);
}

export function isCartesianSeries(
series: ChartSeriesDefaultized<ChartSeriesType>,
): series is ChartSeriesDefaultized<CartesianChartSeriesType> {
return isCartesianSeriesType(series.type);
}
2 changes: 1 addition & 1 deletion packages/x-charts/src/LineChart/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const formatter: Formatter<'line'> = (params, dataset) => {
return {
seriesOrder,
stackingGroups,
series: defaultizeValueFormatter(completedSeries, (v) => v?.toLocaleString()),
series: defaultizeValueFormatter(completedSeries, (v) => (v == null ? '' : v.toLocaleString())),
};
};

Expand Down
Loading