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

[docs] Add demo for Scatter Chart with linked points #16505

Merged
merged 4 commits into from
Feb 10, 2025
Merged
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
60 changes: 60 additions & 0 deletions docs/data/charts/scatter/CustomScatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as React from 'react';
import { ScatterChart } from '@mui/x-charts/ScatterChart';
import { useScatterSeries, useXScale, useYScale } from '@mui/x-charts/hooks';

const data1 = [
{ x: 95, y: 200, id: 1 },
{ x: 120, y: 100, id: 2 },
{ x: 170, y: 300, id: 3 },
{ x: 140, y: 250, id: 4 },
{ x: 150, y: 400, id: 5 },
{ x: 110, y: 280, id: 6 },
];

const data2 = [
{ x: 300, y: 300, id: 1 },
{ x: 200, y: 700, id: 2 },
{ x: 400, y: 500, id: 3 },
{ x: 340, y: 350, id: 4 },
{ x: 420, y: 280, id: 5 },
];

const series = [
{ id: 's1', data: data1, label: 'Open' },
{ id: 's2', data: data2, label: 'Closed' },
];

function LinkPoints({ seriesId, close }) {
const scatter = useScatterSeries();
const xScale = useXScale();
const yScale = useYScale();

if (!scatter || !scatter.series[seriesId]) {
return null;
}
const { color, data } = scatter.series[seriesId];

if (!data) {
return null;
}

return (
<path
fill="none"
stroke={color}
strokeWidth={2}
d={`M ${data.map(({ x, y }) => `${xScale(x)}, ${yScale(y)}`).join(' L')}${
close ? 'Z' : ''
}`}
/>
);
}

export default function CustomScatter() {
return (
<ScatterChart series={series} width={500} height={300}>
<LinkPoints seriesId="s1" />
<LinkPoints seriesId="s2" close />
</ScatterChart>
);
}
58 changes: 58 additions & 0 deletions docs/data/charts/scatter/CustomScatter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as React from 'react';
import { ScatterChart } from '@mui/x-charts/ScatterChart';
import { useScatterSeries, useXScale, useYScale } from '@mui/x-charts/hooks';

const data1 = [
{ x: 95, y: 200, id: 1 },
{ x: 120, y: 100, id: 2 },
{ x: 170, y: 300, id: 3 },
{ x: 140, y: 250, id: 4 },
{ x: 150, y: 400, id: 5 },
{ x: 110, y: 280, id: 6 },
];
const data2 = [
{ x: 300, y: 300, id: 1 },
{ x: 200, y: 700, id: 2 },
{ x: 400, y: 500, id: 3 },
{ x: 340, y: 350, id: 4 },
{ x: 420, y: 280, id: 5 },
];
const series = [
{ id: 's1', data: data1, label: 'Open' },
{ id: 's2', data: data2, label: 'Closed' },
];

function LinkPoints({ seriesId, close }: { seriesId: string; close?: boolean }) {
const scatter = useScatterSeries();
const xScale = useXScale();
const yScale = useYScale();

if (!scatter || !scatter.series[seriesId]) {
return null;
}
const { color, data } = scatter.series[seriesId];

if (!data) {
return null;
}

return (
<path
fill="none"
stroke={color}
strokeWidth={2}
d={`M ${data.map(({ x, y }) => `${xScale(x)}, ${yScale(y)}`).join(' L')}${
close ? 'Z' : ''
}`}
/>
);
}

export default function CustomScatter() {
return (
<ScatterChart series={series} width={500} height={300}>
<LinkPoints seriesId="s1" />
<LinkPoints seriesId="s2" close />
</ScatterChart>
);
}
4 changes: 4 additions & 0 deletions docs/data/charts/scatter/CustomScatter.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<ScatterChart series={series} width={500} height={300}>
<LinkPoints seriesId="s1" />
<LinkPoints seriesId="s2" close />
</ScatterChart>
11 changes: 11 additions & 0 deletions docs/data/charts/scatter/scatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,14 @@ See [Axis—Grid](/x/react-charts/axis/#grid) documentation for more information
### Shape 🚧

### Size 🚧

## Plot Customization
Copy link
Member

Choose a reason for hiding this comment

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

Should we out this above the 🚧 above? We might want to remove them instead 🤔

Copy link
Member

Choose a reason for hiding this comment

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

We can remove them. Maybe adding issues with waiting for upvote for the associated feature

Copy link
Member Author

Choose a reason for hiding this comment

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

I can do that, but what are those sections meant to contain?

Copy link
Member

Choose a reason for hiding this comment

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

Modify the size and shape of the items in the scatter chart

Copy link
Member Author

Choose a reason for hiding this comment

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

I was working on this and as far as I can tell there's no way to customize the shape of a scatter point because it always renders a circle. There is a labelMarkType, but it only applies to tooltip and legends, so would it make sense to create a slot for ScatterItem or ScatterPoint to allow it to be customized? We could potentially allow a markerType instead for a higher-level API.


You can customize the plotting of the data in a scatter chart by providing custom components as `children` of the `ScatterChart` component.

A scatter chart's series can be accessed through the `useScatterSeries` hook.
This hook returns the order of the series and information about the series themselves, including their data points, color, etc.

See [Custom components](/x/react-charts/components/) to learn how to further customize your charts.

{{"demo": "CustomScatter.js"}}