-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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] Add an overlay for "no data" or "loading" states #12817
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
923f917
[charts] Add an overlay for "no data" or "loading" states
alexfauquette 30b4359
Merge remote-tracking branch 'upstream/master' into add-loading
alexfauquette 9db7bf5
ALlow to customize message
alexfauquette c736cbc
document the feature
alexfauquette 40c965d
fix TS
alexfauquette f9a3617
fix axis proptypes with no data
alexfauquette 836b6ca
Remove axis if nothing to display
alexfauquette f7b005d
support all charts
alexfauquette af601be
remove tooltip when loading
alexfauquette 6436860
fix TS
alexfauquette 6c92120
fix docs:api
alexfauquette 38e031e
remove double space
alexfauquette b78a659
rewrite JSdocs
alexfauquette 49ecd96
refine docs
alexfauquette File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are missing the font here no?
I would expect to see
theme.typography.body1
or.body2
depending on what makes the most sense (maybe body2?)