Skip to content
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

[docs] Document how to hide the legend #10951

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/data/charts/legend/HiddenLegend.js
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 FormControlLabel from '@mui/material/FormControlLabel';
import Checkbox from '@mui/material/Checkbox';
import { PieChart } from '@mui/x-charts/PieChart';

const series = [
{
data: [
{ id: 0, value: 10, label: 'series A' },
{ id: 1, value: 15, label: 'series B' },
{ id: 2, value: 20, label: 'series C' },
],
},
];

export default function HiddenLegend() {
const [isHidden, setIsHidden] = React.useState(false);

return (
<Stack>
<FormControlLabel
checked={isHidden}
control={
<Checkbox onChange={(event) => setIsHidden(event.target.checked)} />
}
label="hide the legend"
labelPlacement="end"
/>
<PieChart
series={series}
slotProps={{ legend: { hidden: isHidden } }}
width={400}
height={200}
/>
</Stack>
);
}
38 changes: 38 additions & 0 deletions docs/data/charts/legend/HiddenLegend.tsx
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 FormControlLabel from '@mui/material/FormControlLabel';
import Checkbox from '@mui/material/Checkbox';
import { PieChart } from '@mui/x-charts/PieChart';

const series = [
{
data: [
{ id: 0, value: 10, label: 'series A' },
{ id: 1, value: 15, label: 'series B' },
{ id: 2, value: 20, label: 'series C' },
],
},
];

export default function HiddenLegend() {
const [isHidden, setIsHidden] = React.useState(false);

return (
<Stack>
<FormControlLabel
checked={isHidden}
control={
<Checkbox onChange={(event) => setIsHidden(event.target.checked)} />
}
label="hide the legend"
labelPlacement="end"
/>
<PieChart
series={series}
slotProps={{ legend: { hidden: isHidden } }}
width={400}
height={200}
/>
</Stack>
);
}
14 changes: 14 additions & 0 deletions docs/data/charts/legend/HiddenLegend.tsx.preview
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<FormControlLabel
checked={isHidden}
control={
<Checkbox onChange={(event) => setIsHidden(event.target.checked)} />
}
label="hide the legend"
labelPlacement="end"
/>
<PieChart
series={series}
slotProps={{ legend: { hidden: isHidden } }}
width={400}
height={200}
/>
6 changes: 6 additions & 0 deletions docs/data/charts/legend/legend.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ By default, the legend is placed above the charts.

{{"demo": "LegendPositionNoSnap.js", "hideToolbar": true, "bg": "inline"}}

### Hidding

You can hide the legend with the property `slotProps.legend.hidden`.
michelengelen marked this conversation as resolved.
Show resolved Hide resolved

{{"demo": "HiddenLegend.js"}}

### Dimensions

Inside the legend, you can customize the pixel value of the width and height of the mark with the `itemMarkWidth` and `itemMarkHeight` props.
Expand Down
1 change: 1 addition & 0 deletions packages/x-charts/src/ChartsLegend/ChartsLegend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type ChartsLegendProps = {
classes?: Partial<ChartsLegendClasses>;
/**
* Set to true to hide the legend.
* @default false
*/
hidden?: boolean;
/**
Expand Down
Loading