-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[charts] Add
label
to be displayed inside bars in BarChart (#12988)
Signed-off-by: Jose C Quintas Jr <[email protected]> Co-authored-by: Lukas <[email protected]> Co-authored-by: Alexandre Fauquette <[email protected]>
- Loading branch information
1 parent
11decfc
commit 5585ea1
Showing
31 changed files
with
717 additions
and
9 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
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,14 @@ | ||
import * as React from 'react'; | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
|
||
export default function BarLabel() { | ||
return ( | ||
<BarChart | ||
xAxis={[{ scaleType: 'band', data: ['group A', 'group B', 'group C'] }]} | ||
series={[{ data: [4, 3, 5] }, { data: [1, 6, 3] }, { data: [2, 5, 6] }]} | ||
width={500} | ||
height={300} | ||
barLabel="value" | ||
/> | ||
); | ||
} |
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,14 @@ | ||
import * as React from 'react'; | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
|
||
export default function BarLabel() { | ||
return ( | ||
<BarChart | ||
xAxis={[{ scaleType: 'band', data: ['group A', 'group B', 'group C'] }]} | ||
series={[{ data: [4, 3, 5] }, { data: [1, 6, 3] }, { data: [2, 5, 6] }]} | ||
width={500} | ||
height={300} | ||
barLabel="value" | ||
/> | ||
); | ||
} |
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,7 @@ | ||
<BarChart | ||
xAxis={[{ scaleType: 'band', data: ['group A', 'group B', 'group C'] }]} | ||
series={[{ data: [4, 3, 5] }, { data: [1, 6, 3] }, { data: [2, 5, 6] }]} | ||
width={500} | ||
height={300} | ||
barLabel="value" | ||
/> |
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 { BarChart } from '@mui/x-charts/BarChart'; | ||
|
||
export default function CustomLabels() { | ||
return ( | ||
<BarChart | ||
series={[ | ||
{ data: [4, 2, 5, 4, 1], stack: 'A', label: 'Series A1' }, | ||
{ data: [2, 8, 1, 3, 1], stack: 'A', label: 'Series A2' }, | ||
{ data: [14, 6, 5, 8, 9], label: 'Series B1' }, | ||
]} | ||
barLabel={(item, context) => { | ||
if ((item.value ?? 0) > 10) { | ||
return 'High'; | ||
} | ||
return context.bar.height < 60 ? null : item.value?.toString(); | ||
}} | ||
width={600} | ||
height={350} | ||
/> | ||
); | ||
} |
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 { BarChart } from '@mui/x-charts/BarChart'; | ||
|
||
export default function CustomLabels() { | ||
return ( | ||
<BarChart | ||
series={[ | ||
{ data: [4, 2, 5, 4, 1], stack: 'A', label: 'Series A1' }, | ||
{ data: [2, 8, 1, 3, 1], stack: 'A', label: 'Series A2' }, | ||
{ data: [14, 6, 5, 8, 9], label: 'Series B1' }, | ||
]} | ||
barLabel={(item, context) => { | ||
if ((item.value ?? 0) > 10) { | ||
return 'High'; | ||
} | ||
return context.bar.height < 60 ? null : item.value?.toString(); | ||
}} | ||
width={600} | ||
height={350} | ||
/> | ||
); | ||
} |
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,15 @@ | ||
<BarChart | ||
series={[ | ||
{ data: [4, 2, 5, 4, 1], stack: 'A', label: 'Series A1' }, | ||
{ data: [2, 8, 1, 3, 1], stack: 'A', label: 'Series A2' }, | ||
{ data: [14, 6, 5, 8, 9], label: 'Series B1' }, | ||
]} | ||
barLabel={(item, context) => { | ||
if ((item.value ?? 0) > 10) { | ||
return 'High'; | ||
} | ||
return context.bar.height < 60 ? null : item.value?.toString(); | ||
}} | ||
width={600} | ||
height={350} | ||
/> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as React from 'react'; | ||
import ApiPage from 'docs/src/modules/components/ApiPage'; | ||
import mapApiPageTranslations from 'docs/src/modules/utils/mapApiPageTranslations'; | ||
import jsonPageContent from './bar-label.json'; | ||
|
||
export default function Page(props) { | ||
const { descriptions, pageContent } = props; | ||
return <ApiPage descriptions={descriptions} pageContent={pageContent} />; | ||
} | ||
|
||
Page.getInitialProps = () => { | ||
const req = require.context( | ||
'docsx/translations/api-docs/charts/bar-label', | ||
false, | ||
/\.\/bar-label.*.json$/, | ||
); | ||
const descriptions = mapApiPageTranslations(req); | ||
|
||
return { | ||
descriptions, | ||
pageContent: jsonPageContent, | ||
}; | ||
}; |
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,41 @@ | ||
{ | ||
"props": {}, | ||
"name": "BarLabel", | ||
"imports": [ | ||
"import { BarLabel } from '@mui/x-charts/BarChart';", | ||
"import { BarLabel } from '@mui/x-charts';" | ||
], | ||
"slots": [ | ||
{ | ||
"name": "barLabel", | ||
"description": "The component that renders the bar label.", | ||
"default": "BarLabel", | ||
"class": null | ||
} | ||
], | ||
"classes": [ | ||
{ | ||
"key": "faded", | ||
"className": "MuiBarLabel-faded", | ||
"description": "Styles applied to the root element if it is faded.", | ||
"isGlobal": false | ||
}, | ||
{ | ||
"key": "highlighted", | ||
"className": "MuiBarLabel-highlighted", | ||
"description": "Styles applied to the root element if it is highlighted.", | ||
"isGlobal": false | ||
}, | ||
{ | ||
"key": "root", | ||
"className": "MuiBarLabel-root", | ||
"description": "Styles applied to the root element.", | ||
"isGlobal": false | ||
} | ||
], | ||
"muiName": "MuiBarLabel", | ||
"filename": "/packages/x-charts/src/BarChart/BarLabel/BarLabel.tsx", | ||
"inheritance": null, | ||
"demos": "<ul><li><a href=\"/x/react-charts/bars/\">Charts - Bars</a></li></ul>", | ||
"cssComponent": false | ||
} |
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
18 changes: 18 additions & 0 deletions
18
docs/translations/api-docs/charts/bar-label/bar-label.json
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 @@ | ||
{ | ||
"componentDescription": "", | ||
"propDescriptions": {}, | ||
"classDescriptions": { | ||
"faded": { | ||
"description": "Styles applied to {{nodeName}} if {{conditions}}.", | ||
"nodeName": "the root element", | ||
"conditions": "it is faded" | ||
}, | ||
"highlighted": { | ||
"description": "Styles applied to {{nodeName}} if {{conditions}}.", | ||
"nodeName": "the root element", | ||
"conditions": "it is highlighted" | ||
}, | ||
"root": { "description": "Styles applied to the root element." } | ||
}, | ||
"slotDescriptions": { "barLabel": "The component that renders the bar label." } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as React from 'react'; | ||
import { styled, useThemeProps } from '@mui/material/styles'; | ||
import { animated } from '@react-spring/web'; | ||
import PropTypes from 'prop-types'; | ||
import { barLabelClasses } from './barLabelClasses'; | ||
import { BarLabelOwnerState } from './BarLabel.types'; | ||
|
||
export const BarLabelComponent = styled(animated.text, { | ||
name: 'MuiBarLabel', | ||
slot: 'Root', | ||
overridesResolver: (_, styles) => [ | ||
{ [`&.${barLabelClasses.faded}`]: styles.faded }, | ||
{ [`&.${barLabelClasses.highlighted}`]: styles.highlighted }, | ||
styles.root, | ||
], | ||
})(({ theme }) => ({ | ||
...theme?.typography?.body2, | ||
stroke: 'none', | ||
fill: (theme.vars || theme)?.palette?.text?.primary, | ||
transition: 'opacity 0.2s ease-in, fill 0.2s ease-in', | ||
textAnchor: 'middle', | ||
dominantBaseline: 'central', | ||
pointerEvents: 'none', | ||
opacity: 1, | ||
[`&.${barLabelClasses.faded}`]: { | ||
opacity: 0.3, | ||
}, | ||
})); | ||
|
||
export type BarLabelProps = Omit<React.SVGProps<SVGTextElement>, 'ref' | 'id'> & BarLabelOwnerState; | ||
|
||
function BarLabel(props: BarLabelProps) { | ||
const themeProps = useThemeProps({ props, name: 'MuiBarLabel' }); | ||
|
||
const { seriesId, dataIndex, color, isFaded, isHighlighted, classes, ...otherProps } = themeProps; | ||
|
||
return <BarLabelComponent {...otherProps} />; | ||
} | ||
|
||
BarLabel.propTypes = { | ||
// ----------------------------- Warning -------------------------------- | ||
// | These PropTypes are generated from the TypeScript type definitions | | ||
// | To update them edit the TypeScript types and run "yarn proptypes" | | ||
// ---------------------------------------------------------------------- | ||
classes: PropTypes.object, | ||
dataIndex: PropTypes.number.isRequired, | ||
isFaded: PropTypes.bool.isRequired, | ||
isHighlighted: PropTypes.bool.isRequired, | ||
seriesId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired, | ||
} as any; | ||
|
||
export { BarLabel }; |
Oops, something went wrong.