-
-
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
+600
−27
Merged
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions
22
packages/x-charts/src/ChartsOverlay/ChartsLoadingOverlay.tsx
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,22 @@ | ||
import * as React from 'react'; | ||
import { styled } from '@mui/material/styles'; | ||
import { useDrawingArea } from '../hooks/useDrawingArea'; | ||
import type { CommonOverlayProps } from './ChartsOverlay'; | ||
|
||
const StyledText = styled('text')(({ theme }) => ({ | ||
stroke: 'none', | ||
fill: theme.palette.text.primary, | ||
shapeRendering: 'crispEdges', | ||
textAnchor: 'middle', | ||
dominantBaseline: 'middle', | ||
})); | ||
|
||
export function ChartsLoadingOverlay(props: CommonOverlayProps) { | ||
const { top, left, height, width } = useDrawingArea(); | ||
|
||
return ( | ||
<StyledText x={left + width / 2} y={top + height / 2} {...props}> | ||
Loading data ... | ||
</StyledText> | ||
); | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/x-charts/src/ChartsOverlay/ChartsNoDataOverlay.tsx
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,22 @@ | ||
import * as React from 'react'; | ||
import { styled } from '@mui/material/styles'; | ||
import { useDrawingArea } from '../hooks/useDrawingArea'; | ||
import type { CommonOverlayProps } from './ChartsOverlay'; | ||
|
||
const StyledText = styled('text')(({ theme }) => ({ | ||
stroke: 'none', | ||
fill: theme.palette.text.primary, | ||
shapeRendering: 'crispEdges', | ||
textAnchor: 'middle', | ||
dominantBaseline: 'middle', | ||
})); | ||
|
||
export function ChartsNoDataOverlay(props: CommonOverlayProps) { | ||
const { top, left, height, width } = useDrawingArea(); | ||
|
||
return ( | ||
<StyledText x={left + width / 2} y={top + height / 2} {...props}> | ||
No data to display | ||
</StyledText> | ||
); | ||
} |
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,64 @@ | ||
import * as React from 'react'; | ||
import { SxProps, Theme } from '@mui/material/styles'; | ||
import { ChartsLoadingOverlay } from './ChartsLoadingOverlay'; | ||
import { useSeries } from '../hooks/useSeries'; | ||
import { SeriesId } from '../models/seriesType/common'; | ||
import { ChartsNoDataOverlay } from './ChartsNoDataOverlay'; | ||
|
||
export function useNoData() { | ||
const seriesPerType = useSeries(); | ||
|
||
return Object.values(seriesPerType).every((seriesOfGivenType) => { | ||
if (!seriesOfGivenType) { | ||
return true; | ||
} | ||
const { series, seriesOrder } = seriesOfGivenType; | ||
|
||
return seriesOrder.every((seriesId: SeriesId) => series[seriesId].data.length === 0); | ||
}); | ||
} | ||
|
||
export type CommonOverlayProps = React.SVGAttributes<SVGTextElement> & { | ||
sx?: SxProps<Theme>; | ||
}; | ||
|
||
export interface ChartsOverlaySlots { | ||
/** | ||
* Loading overlay component rendered when the chart is in a loading state. | ||
alexfauquette marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @default ChartsLoadingOverlay | ||
*/ | ||
loadingOverlay?: React.ElementType<CommonOverlayProps>; | ||
/** | ||
* No data overlay component rendered when the chart has no data to display. | ||
alexfauquette marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @default ChartsNoDataOverlay | ||
*/ | ||
noDataOverlay?: React.ElementType<CommonOverlayProps>; | ||
JCQuintas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
export interface ChartsOverlaySlotProps { | ||
loadingOverlay?: Partial<CommonOverlayProps>; | ||
noDataOverlay?: Partial<CommonOverlayProps>; | ||
} | ||
|
||
export interface ChartsOverlayProps { | ||
/** | ||
* If `true`, a loading overlay is displayed. | ||
*/ | ||
loading?: boolean; | ||
|
||
slots?: ChartsOverlaySlots; | ||
slotProps?: ChartsOverlaySlotProps; | ||
} | ||
|
||
export function ChartsOverlay(props: ChartsOverlayProps) { | ||
const noData = useNoData(); | ||
|
||
if (props.loading) { | ||
const LoadingOverlay = props.slots?.loadingOverlay ?? ChartsLoadingOverlay; | ||
return <LoadingOverlay {...props.slotProps?.loadingOverlay} />; | ||
} | ||
if (noData) { | ||
const NoDataOverlay = props.slots?.noDataOverlay ?? ChartsNoDataOverlay; | ||
return <NoDataOverlay {...props.slotProps?.noDataOverlay} />; | ||
} | ||
return null; | ||
} |
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,3 @@ | ||
export { ChartsOverlay } from './ChartsOverlay'; | ||
export { ChartsLoadingOverlay } from './ChartsLoadingOverlay'; | ||
export { ChartsNoDataOverlay } from './ChartsNoDataOverlay'; |
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
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.
I guess you don't want to introduce a localization system for this text 😆
But people should be able to pass
slotProps={{ loadingOverlay: { children: 'Chargement des données' } }}
and right now I don't think it worksThe following might do the trick:
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.
Sorry I didn't see it was in draft 👼
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.
No issue I was motivated to introduce a localization system, but the notion of props might be better as long as we do not see other translation needs
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.
I agree with your approach
If this will likely be the only text in the foreseeable future, a prop is fine
If we think of some other texts that might be introduced soon, the localization system is required