diff --git a/docs/data/charts/legend/CustomLegend.js b/docs/data/charts/legend/CustomLegend.js new file mode 100644 index 0000000000000..62c74bb546743 --- /dev/null +++ b/docs/data/charts/legend/CustomLegend.js @@ -0,0 +1,111 @@ +import * as React from 'react'; +import Box from '@mui/material/Box'; +import Stack from '@mui/material/Stack'; +import Typography from '@mui/material/Typography'; +import { useTheme } from '@mui/material/styles'; +import { useLegend } from '@mui/x-charts/hooks'; +import { LineChart } from '@mui/x-charts/LineChart'; + +function LineWithMark({ color, size }) { + return ( + + + + + ); +} + +function DashedLine({ color, size }) { + return ( + + + + ); +} + +function MyCustomLegend() { + const { items } = useLegend(); + return ( + + {items.map((item) => { + const { label, id, color, seriesId } = item; + return ( + + {seriesId === 'avg' ? ( + + ) : ( + + )} + {`${label}`} + + ); + })} + + ); +} + +const dataset = [ + { month: 'Jan', '1991_2020_avg': 4.1, 2023: 3.9 }, + { month: 'Fev', '1991_2020_avg': 4.7, 2023: 8.9 }, + { month: 'Mar', '1991_2020_avg': 7.5, 2023: 9.5 }, + { month: 'Apr', '1991_2020_avg': 10.6, 2023: 11.5 }, + { month: 'May', '1991_2020_avg': 13.8, 2023: 15.5 }, + { month: 'Jun', '1991_2020_avg': 16.7, 2023: 16.4 }, + { month: 'Jul', '1991_2020_avg': 18.9, 2023: 19.5 }, + { month: 'Aug', '1991_2020_avg': 18.8, 2023: 20.5 }, + { month: 'Sep', '1991_2020_avg': 15.8, 2023: 16.4 }, + { month: 'Oct', '1991_2020_avg': 11.9, 2023: 13.2 }, + { month: 'Nov', '1991_2020_avg': 7.6, 2023: 8.1 }, + { month: 'Dec', '1991_2020_avg': 4.7, 2023: 6.1 }, +]; + +export default function CustomLegend() { + const theme = useTheme(); + + return ( + + + + ); +} diff --git a/docs/data/charts/legend/CustomLegend.tsx b/docs/data/charts/legend/CustomLegend.tsx new file mode 100644 index 0000000000000..9d84a0b9ee10e --- /dev/null +++ b/docs/data/charts/legend/CustomLegend.tsx @@ -0,0 +1,116 @@ +import * as React from 'react'; +import Box from '@mui/material/Box'; +import Stack from '@mui/material/Stack'; +import Typography from '@mui/material/Typography'; +import { useTheme } from '@mui/material/styles'; +import { useLegend } from '@mui/x-charts/hooks'; +import { LineChart } from '@mui/x-charts/LineChart'; + +type IconProps = { + color: string; + size: number; +}; + +function LineWithMark({ color, size }: IconProps) { + return ( + + + + + ); +} + +function DashedLine({ color, size }: IconProps) { + return ( + + + + ); +} + +function MyCustomLegend() { + const { items } = useLegend(); + return ( + + {items.map((item) => { + const { label, id, color, seriesId } = item; + return ( + + {seriesId === 'avg' ? ( + + ) : ( + + )} + {`${label}`} + + ); + })} + + ); +} + +const dataset = [ + { month: 'Jan', '1991_2020_avg': 4.1, 2023: 3.9 }, + { month: 'Fev', '1991_2020_avg': 4.7, 2023: 8.9 }, + { month: 'Mar', '1991_2020_avg': 7.5, 2023: 9.5 }, + { month: 'Apr', '1991_2020_avg': 10.6, 2023: 11.5 }, + { month: 'May', '1991_2020_avg': 13.8, 2023: 15.5 }, + { month: 'Jun', '1991_2020_avg': 16.7, 2023: 16.4 }, + { month: 'Jul', '1991_2020_avg': 18.9, 2023: 19.5 }, + { month: 'Aug', '1991_2020_avg': 18.8, 2023: 20.5 }, + { month: 'Sep', '1991_2020_avg': 15.8, 2023: 16.4 }, + { month: 'Oct', '1991_2020_avg': 11.9, 2023: 13.2 }, + { month: 'Nov', '1991_2020_avg': 7.6, 2023: 8.1 }, + { month: 'Dec', '1991_2020_avg': 4.7, 2023: 6.1 }, +]; + +export default function CustomLegend() { + const theme = useTheme(); + + return ( + + + + ); +} diff --git a/docs/data/charts/legend/legend.md b/docs/data/charts/legend/legend.md index ba5a44654ce6a..2d0da25629787 100644 --- a/docs/data/charts/legend/legend.md +++ b/docs/data/charts/legend/legend.md @@ -18,8 +18,6 @@ In chart components, the legend links series with `label` properties and their c This section explains how to customize the legend using classes and properties. -In order to fully customize the legend with custom components, see an example at the [HTML components docs](/x/react-charts/components/#html-components). - ### Dimensions Much of the customization can be done using CSS properties. @@ -80,6 +78,17 @@ Make sure that the legend container has a fixed height or width to enable scroll {{"demo": "ScrollableLegend.js"}} +### Custom component + +For advanced customization, you can create your own legend with `useLegend`. +This hook returns the items that the default legend would plot. +Allowing you to focus on the rendering. + +This demo show how to use it with slots. +Another demo in [HTML components docs](/x/react-charts/components/#html-components) shows how to use it with composition. + +{{"demo": "CustomLegend.js"}} + ## Color legend To display legend associated to a [colorMap](https://mui.com/x/react-charts/styling/#values-color), you can use: