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 #11898

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 1 addition & 3 deletions packages/x-charts/src/BarChart/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ const formatter: Formatter<'bar'> = (params, dataset) => {
return {
seriesOrder,
stackingGroups,
series: defaultizeValueFormatter(completedSeries, (v) =>
v === null ? '' : v.toLocaleString(),
),
series: defaultizeValueFormatter(completedSeries, (v) => (v == null ? '' : v.toLocaleString())),
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 { CartesianChartSeriesType, ChartSeriesDefaultized } from '../models/seriesType/config';
import {
ChartsTooltipCell,
ChartsTooltipPaper,
Expand Down Expand Up @@ -33,31 +33,36 @@ function DefaultChartsAxisTooltipContent(props: ChartsAxisContentProps) {
)}

<tbody>
{series.map(({ color, id, label, valueFormatter, data }: ChartSeriesDefaultized<any>) => {
const formattedValue = valueFormatter(data[dataIndex]);
if (formattedValue == null) {
return null;
}
return (
<ChartsTooltipRow key={id} className={classes.row}>
<ChartsTooltipCell className={clsx(classes.markCell, classes.cell)}>
<ChartsTooltipMark
ownerState={{ color }}
boxShadow={1}
className={classes.mark}
/>
</ChartsTooltipCell>
{series
.filter((item): item is ChartSeriesDefaultized<CartesianChartSeriesType> =>
['bar', 'line', 'scatter'].includes(item.type),
)
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: WDYT, would it make sense to extract it into a variable outside of JSX? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Very good idea. I also did it for the container

.map(({ color, id, label, valueFormatter, data }) => {
// @ts-ignore
const formattedValue = valueFormatter(data[dataIndex] ?? null);
if (formattedValue == null) {
return null;
}
return (
<ChartsTooltipRow key={id} className={classes.row}>
<ChartsTooltipCell className={clsx(classes.markCell, classes.cell)}>
<ChartsTooltipMark
ownerState={{ color }}
boxShadow={1}
className={classes.mark}
/>
</ChartsTooltipCell>

<ChartsTooltipCell className={clsx(classes.labelCell, classes.cell)}>
{label ? <Typography>{label}</Typography> : null}
</ChartsTooltipCell>
<ChartsTooltipCell className={clsx(classes.labelCell, classes.cell)}>
{label ? <Typography>{label}</Typography> : null}
</ChartsTooltipCell>

<ChartsTooltipCell className={clsx(classes.valueCell, classes.cell)}>
<Typography>{formattedValue}</Typography>
</ChartsTooltipCell>
</ChartsTooltipRow>
);
})}
<ChartsTooltipCell className={clsx(classes.valueCell, classes.cell)}>
<Typography>{formattedValue}</Typography>
</ChartsTooltipCell>
</ChartsTooltipRow>
);
})}
</tbody>
</ChartsTooltipTable>
</ChartsTooltipPaper>
Expand Down
4 changes: 1 addition & 3 deletions packages/x-charts/src/LineChart/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ const formatter: Formatter<'line'> = (params, dataset) => {
return {
seriesOrder,
stackingGroups,
series: defaultizeValueFormatter(completedSeries, (v) =>
v === null ? '' : v.toLocaleString(),
),
series: defaultizeValueFormatter(completedSeries, (v) => (v == null ? '' : v.toLocaleString())),
};
};

Expand Down
Loading