-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[charts] Add an overlay for "no data" or "loading" states (mui#12817)
- Loading branch information
1 parent
dffcd41
commit bdd885b
Showing
40 changed files
with
600 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as React from 'react'; | ||
import { styled } from '@mui/material/styles'; | ||
import Stack from '@mui/material/Stack'; | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
import { useDrawingArea, useXScale, useYScale } from '@mui/x-charts/hooks'; | ||
|
||
const ratios = [0.2, 0.8, 0.6, 0.5]; | ||
|
||
const LoadingReact = styled('rect')({ | ||
opacity: 0.2, | ||
fill: 'lightgray', | ||
}); | ||
|
||
const LoadingText = styled('text')(({ theme }) => ({ | ||
stroke: 'none', | ||
fill: theme.palette.text.primary, | ||
shapeRendering: 'crispEdges', | ||
textAnchor: 'middle', | ||
dominantBaseline: 'middle', | ||
})); | ||
|
||
function LoadingOverlay() { | ||
const xScale = useXScale(); | ||
const yScale = useYScale(); | ||
const { left, width, height } = useDrawingArea(); | ||
|
||
const bandWidth = xScale.bandwidth(); | ||
|
||
const [bottom, top] = yScale.range(); | ||
|
||
return ( | ||
<g> | ||
{xScale.domain().map((item, index) => { | ||
const ratio = ratios[index % ratios.length]; | ||
const barHeight = ratio * (bottom - top); | ||
|
||
return ( | ||
<LoadingReact | ||
x={xScale(item)} | ||
width={bandWidth} | ||
y={bottom - barHeight} | ||
height={height} | ||
/> | ||
); | ||
})} | ||
<LoadingText x={left + width / 2} y={top + height / 2}> | ||
Loading data ... | ||
</LoadingText> | ||
</g> | ||
); | ||
} | ||
|
||
export default function CustomOverlay() { | ||
return ( | ||
<Stack direction={{ md: 'row', xs: 'column' }} sx={{ width: '100%' }}> | ||
<BarChart | ||
slotProps={{ | ||
noDataOverlay: { message: 'No data to display in this chart' }, | ||
}} | ||
series={[]} | ||
margin={{ top: 10, right: 10, left: 25, bottom: 25 }} | ||
height={150} | ||
/> | ||
<BarChart | ||
loading | ||
xAxis={[ | ||
{ scaleType: 'band', data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] }, | ||
]} | ||
slots={{ loadingOverlay: LoadingOverlay }} | ||
series={[]} | ||
margin={{ top: 10, right: 10, left: 25, bottom: 25 }} | ||
height={150} | ||
/> | ||
</Stack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as React from 'react'; | ||
import { styled } from '@mui/material/styles'; | ||
import Stack from '@mui/material/Stack'; | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
import { useDrawingArea, useXScale, useYScale } from '@mui/x-charts/hooks'; | ||
|
||
const ratios = [0.2, 0.8, 0.6, 0.5]; | ||
|
||
const LoadingReact = styled('rect')({ | ||
opacity: 0.2, | ||
fill: 'lightgray', | ||
}); | ||
|
||
const LoadingText = styled('text')(({ theme }) => ({ | ||
stroke: 'none', | ||
fill: theme.palette.text.primary, | ||
shapeRendering: 'crispEdges', | ||
textAnchor: 'middle', | ||
dominantBaseline: 'middle', | ||
})); | ||
|
||
function LoadingOverlay() { | ||
const xScale = useXScale<'band'>(); | ||
const yScale = useYScale(); | ||
const { left, width, height } = useDrawingArea(); | ||
|
||
const bandWidth = xScale.bandwidth(); | ||
|
||
const [bottom, top] = yScale.range(); | ||
|
||
return ( | ||
<g> | ||
{xScale.domain().map((item, index) => { | ||
const ratio = ratios[index % ratios.length]; | ||
const barHeight = ratio * (bottom - top); | ||
|
||
return ( | ||
<LoadingReact | ||
x={xScale(item)} | ||
width={bandWidth} | ||
y={bottom - barHeight} | ||
height={height} | ||
/> | ||
); | ||
})} | ||
<LoadingText x={left + width / 2} y={top + height / 2}> | ||
Loading data ... | ||
</LoadingText> | ||
</g> | ||
); | ||
} | ||
|
||
export default function CustomOverlay() { | ||
return ( | ||
<Stack direction={{ md: 'row', xs: 'column' }} sx={{ width: '100%' }}> | ||
<BarChart | ||
slotProps={{ | ||
noDataOverlay: { message: 'No data to display in this chart' }, | ||
}} | ||
series={[]} | ||
margin={{ top: 10, right: 10, left: 25, bottom: 25 }} | ||
height={150} | ||
/> | ||
<BarChart | ||
loading | ||
xAxis={[ | ||
{ scaleType: 'band', data: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] }, | ||
]} | ||
slots={{ loadingOverlay: LoadingOverlay }} | ||
series={[]} | ||
margin={{ top: 10, right: 10, left: 25, bottom: 25 }} | ||
height={150} | ||
/> | ||
</Stack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as React from 'react'; | ||
import Stack from '@mui/material/Stack'; | ||
import { LineChart } from '@mui/x-charts/LineChart'; | ||
|
||
const emptySeries = { | ||
series: [], | ||
margin: { top: 10, right: 10, left: 25, bottom: 25 }, | ||
height: 150, | ||
}; | ||
|
||
export default function Overlay() { | ||
return ( | ||
<Stack direction={{ md: 'row', xs: 'column' }} sx={{ width: '100%' }}> | ||
<LineChart loading {...emptySeries} /> | ||
<LineChart {...emptySeries} /> | ||
</Stack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as React from 'react'; | ||
import Stack from '@mui/material/Stack'; | ||
import { LineChart } from '@mui/x-charts/LineChart'; | ||
|
||
const emptySeries = { | ||
series: [], | ||
margin: { top: 10, right: 10, left: 25, bottom: 25 }, | ||
height: 150, | ||
}; | ||
|
||
export default function Overlay() { | ||
return ( | ||
<Stack direction={{ md: 'row', xs: 'column' }} sx={{ width: '100%' }}> | ||
<LineChart loading {...emptySeries} /> | ||
<LineChart {...emptySeries} /> | ||
</Stack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<LineChart loading {...emptySeries} /> | ||
<LineChart {...emptySeries} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as React from 'react'; | ||
import Stack from '@mui/material/Stack'; | ||
import { LineChart } from '@mui/x-charts/LineChart'; | ||
|
||
const emptySeries = { | ||
series: [], | ||
margin: { top: 10, right: 10, left: 25, bottom: 25 }, | ||
height: 150, | ||
}; | ||
|
||
export default function OverlayWithAxis() { | ||
return ( | ||
<Stack direction={{ md: 'row', xs: 'column' }} sx={{ width: '100%' }}> | ||
<LineChart | ||
loading | ||
xAxis={[{ data: [0, 1, 2, 4, 5] }]} | ||
yAxis={[{ min: 0, max: 10 }]} | ||
{...emptySeries} | ||
/> | ||
<LineChart | ||
yAxis={[{ min: -5, max: 5 }]} | ||
xAxis={[ | ||
{ | ||
scaleType: 'time', | ||
data: [ | ||
new Date(2019, 0, 1), | ||
new Date(2020, 0, 1), | ||
new Date(2021, 0, 1), | ||
new Date(2022, 0, 1), | ||
], | ||
tickNumber: 3, | ||
}, | ||
]} | ||
{...emptySeries} | ||
/> | ||
</Stack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import * as React from 'react'; | ||
import Stack from '@mui/material/Stack'; | ||
import { LineChart } from '@mui/x-charts/LineChart'; | ||
|
||
const emptySeries = { | ||
series: [], | ||
margin: { top: 10, right: 10, left: 25, bottom: 25 }, | ||
height: 150, | ||
}; | ||
|
||
export default function OverlayWithAxis() { | ||
return ( | ||
<Stack direction={{ md: 'row', xs: 'column' }} sx={{ width: '100%' }}> | ||
<LineChart | ||
loading | ||
xAxis={[{ data: [0, 1, 2, 4, 5] }]} | ||
yAxis={[{ min: 0, max: 10 }]} | ||
{...emptySeries} | ||
/> | ||
<LineChart | ||
yAxis={[{ min: -5, max: 5 }]} | ||
xAxis={[ | ||
{ | ||
scaleType: 'time', | ||
data: [ | ||
new Date(2019, 0, 1), | ||
new Date(2020, 0, 1), | ||
new Date(2021, 0, 1), | ||
new Date(2022, 0, 1), | ||
], | ||
tickNumber: 3, | ||
}, | ||
]} | ||
{...emptySeries} | ||
/> | ||
</Stack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.