Skip to content

Commit

Permalink
[material-ui][docs] Replace recharts with MUI X charts on the Dashboa…
Browse files Browse the repository at this point in the history
…rd template (mui#40107)
  • Loading branch information
alexfauquette authored Jan 9, 2024
1 parent 4d70037 commit 191d684
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 226 deletions.
82 changes: 44 additions & 38 deletions docs/data/material/getting-started/templates/dashboard/Chart.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as React from 'react';
import { useTheme } from '@mui/material/styles';
import { LineChart, Line, XAxis, YAxis, Label, ResponsiveContainer } from 'recharts';
import { LineChart, axisClasses } from '@mui/x-charts';

import Title from './Title';

// Generate Sales Data
function createData(time, amount) {
return { time, amount };
return { time, amount: amount ?? null };
}

const data = [
Expand All @@ -17,7 +18,7 @@ const data = [
createData('15:00', 2000),
createData('18:00', 2400),
createData('21:00', 2400),
createData('24:00', undefined),
createData('24:00'),
];

export default function Chart() {
Expand All @@ -26,46 +27,51 @@ export default function Chart() {
return (
<React.Fragment>
<Title>Today</Title>
<ResponsiveContainer>
<div style={{ width: '100%', flexGrow: 1, overflow: 'hidden' }}>
<LineChart
data={data}
dataset={data}
margin={{
top: 16,
right: 16,
bottom: 0,
left: 24,
right: 20,
left: 70,
bottom: 30,
}}
>
<XAxis
dataKey="time"
stroke={theme.palette.text.secondary}
style={theme.typography.body2}
/>
<YAxis
stroke={theme.palette.text.secondary}
style={theme.typography.body2}
>
<Label
angle={270}
position="left"
style={{
textAnchor: 'middle',
fill: theme.palette.text.primary,
xAxis={[
{
scaleType: 'point',
dataKey: 'time',
tickNumber: 2,
tickLabelStyle: theme.typography.body2,
},
]}
yAxis={[
{
label: 'Sales ($)',
labelStyle: {
...theme.typography.body1,
}}
>
Sales ($)
</Label>
</YAxis>
<Line
isAnimationActive={false}
type="monotone"
dataKey="amount"
stroke={theme.palette.primary.main}
dot={false}
/>
</LineChart>
</ResponsiveContainer>
fill: theme.palette.text.primary,
},
tickLabelStyle: theme.typography.body2,
max: 2500,
tickNumber: 3,
},
]}
series={[
{
dataKey: 'amount',
showMark: false,
color: theme.palette.primary.light,
},
]}
sx={{
[`.${axisClasses.root} line`]: { stroke: theme.palette.text.secondary },
[`.${axisClasses.root} text`]: { fill: theme.palette.text.secondary },
[`& .${axisClasses.left} .${axisClasses.label}`]: {
transform: 'translateX(-25px)',
},
}}
/>
</div>
</React.Fragment>
);
}
87 changes: 48 additions & 39 deletions docs/data/material/getting-started/templates/dashboard/Chart.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as React from 'react';
import { useTheme } from '@mui/material/styles';
import { LineChart, Line, XAxis, YAxis, Label, ResponsiveContainer } from 'recharts';
import { LineChart, axisClasses } from '@mui/x-charts';
import { ChartsTextStyle } from '@mui/x-charts/ChartsText';
import Title from './Title';

// Generate Sales Data
function createData(time: string, amount?: number) {
return { time, amount };
function createData(
time: string,
amount?: number,
): { time: string; amount: number | null } {
return { time, amount: amount ?? null };
}

const data = [
Expand All @@ -17,7 +21,7 @@ const data = [
createData('15:00', 2000),
createData('18:00', 2400),
createData('21:00', 2400),
createData('24:00', undefined),
createData('24:00'),
];

export default function Chart() {
Expand All @@ -26,46 +30,51 @@ export default function Chart() {
return (
<React.Fragment>
<Title>Today</Title>
<ResponsiveContainer>
<div style={{ width: '100%', flexGrow: 1, overflow: 'hidden' }}>
<LineChart
data={data}
dataset={data}
margin={{
top: 16,
right: 16,
bottom: 0,
left: 24,
right: 20,
left: 70,
bottom: 30,
}}
>
<XAxis
dataKey="time"
stroke={theme.palette.text.secondary}
style={theme.typography.body2}
/>
<YAxis
stroke={theme.palette.text.secondary}
style={theme.typography.body2}
>
<Label
angle={270}
position="left"
style={{
textAnchor: 'middle',
xAxis={[
{
scaleType: 'point',
dataKey: 'time',
tickNumber: 2,
tickLabelStyle: theme.typography.body2 as ChartsTextStyle,
},
]}
yAxis={[
{
label: 'Sales ($)',
labelStyle: {
...(theme.typography.body1 as ChartsTextStyle),
fill: theme.palette.text.primary,
...theme.typography.body1,
}}
>
Sales ($)
</Label>
</YAxis>
<Line
isAnimationActive={false}
type="monotone"
dataKey="amount"
stroke={theme.palette.primary.main}
dot={false}
/>
</LineChart>
</ResponsiveContainer>
},
tickLabelStyle: theme.typography.body2 as ChartsTextStyle,
max: 2500,
tickNumber: 3,
},
]}
series={[
{
dataKey: 'amount',
showMark: false,
color: theme.palette.primary.light,
},
]}
sx={{
[`.${axisClasses.root} line`]: { stroke: theme.palette.text.secondary },
[`.${axisClasses.root} text`]: { fill: theme.palette.text.secondary },
[`& .${axisClasses.left} .${axisClasses.label}`]: {
transform: 'translateX(-25px)',
},
}}
/>
</div>
</React.Fragment>
);
}
1 change: 0 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
"react-transition-group": "^4.4.5",
"react-virtuoso": "^4.6.2",
"react-window": "^1.8.10",
"recharts": "2.10.3",
"rimraf": "^5.0.5",
"styled-components": "^6.1.6",
"stylis": "4.2.0",
Expand Down
Loading

0 comments on commit 191d684

Please sign in to comment.