Skip to content

Commit

Permalink
[charts] Fix scatter dataset with missing data (@alexfauquette) (#15804)
Browse files Browse the repository at this point in the history
Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com>
Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
Co-authored-by: Jose C Quintas Jr <juniorquintas@gmail.com>
  • Loading branch information
3 people authored Dec 9, 2024
1 parent 5abd3ea commit 9a4e11d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
53 changes: 52 additions & 1 deletion packages/x-charts/src/ScatterChart/ScatterChart.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { createRenderer } from '@mui/internal-test-utils/createRenderer';
import { expect } from 'chai';
import { createRenderer, screen } from '@mui/internal-test-utils/createRenderer';
import { describeConformance } from 'test/utils/describeConformance';
import { ScatterChart } from '@mui/x-charts/ScatterChart';

Expand Down Expand Up @@ -38,4 +39,54 @@ describe('<ScatterChart />', () => {
],
}),
);

const isJSDOM = /jsdom/.test(window.navigator.userAgent);

it('should support dataset with missing values', async function test() {
if (isJSDOM) {
this.skip();
}

// x from 500 to 600
// y from 100 to 200
const dataset = [
{
version: 'data-0',
a1: 500,
a2: 100,
},
{
version: 'data-1',
a1: 600,
a2: 200,
},
{
version: 'data-2',
// Item with missing x-values
// a1: 500,
a2: 200,
},
{
version: 'data-2',
// Item with missing y-values
a1: 500,
// a2: 200,
},
];

render(
<ScatterChart
dataset={dataset}
series={[{ datasetKeys: { id: 'version', x: 'a1', y: 'a2' }, label: 'Series A' }]}
width={500}
height={300}
/>,
);

const labelX = await screen.findByText('100');
expect(labelX).toBeVisible();

const labelY = await screen.findByText('600');
expect(labelY).toBeVisible();
});
});
7 changes: 4 additions & 3 deletions packages/x-charts/src/ScatterChart/extremums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import {
const mergeMinMax = (
acc: ExtremumGetterResult,
val: ExtremumGetterResult,
): ExtremumGetterResult => {
return [Math.min(acc[0], val[0]), Math.max(acc[1], val[1])];
};
): ExtremumGetterResult => [
val[0] === null ? acc[0] : Math.min(acc[0], val[0]),
val[1] === null ? acc[1] : Math.max(acc[1], val[1]),
];

export const getExtremumX: ExtremumGetter<'scatter'> = (params) => {
const { series, axis, isDefaultAxis, getFilters } = params;
Expand Down
4 changes: 2 additions & 2 deletions packages/x-charts/src/ScatterChart/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const formatter: SeriesFormatter<'scatter'> = ({ series, seriesOrder }, dataset)
? (seriesData.data ?? [])
: (dataset?.map((d) => {
return {
x: d[datasetKeys.x],
y: d[datasetKeys.y],
x: d[datasetKeys.x] ?? null,
y: d[datasetKeys.y] ?? null,
z: datasetKeys.z && d[datasetKeys.z],
id: d[datasetKeys.id],
} as ScatterValueType;
Expand Down

0 comments on commit 9a4e11d

Please sign in to comment.