import React, { memo, ReactNode } from 'react'; import Grid from '@mui/material/Grid'; import Typography from '@mui/material/Typography'; import FormControlLabel from '@mui/material/FormControlLabel'; import Checkbox from '@mui/material/Checkbox'; import { useTheme } from '@mui/material/styles'; import clsx from 'clsx'; import type { FormControlLabelProps } from '@mui/material/FormControlLabel'; import createClasses from './styles'; export interface ChartLegendProps { chartConfig?: { name: string; id: string; color?: string; show: boolean; invisible?: boolean; }[]; handleChange?: (id: string) => FormControlLabelProps['onChange']; active?: boolean; vertical?: boolean; data?: { name: string; value: number; color?: string }[]; legendUnitPrefix?: string; legendUnitPostfix?: string; classRoot?: string; formatter?: (value: number) => ReactNode; } const ChartLegend = (props: ChartLegendProps) => { const theme = useTheme(); const classes = createClasses(); const { chartConfig = [], active = false, handleChange, vertical = false, data = [], legendUnitPrefix, legendUnitPostfix, classRoot, formatter } = props; if (vertical) { if (chartConfig.length > 0) { return ( {chartConfig.map((item, index) => { if (!item.invisible) { if (active) { return ( } checkedIcon={ } /> } classes={{ label: classes.name }} label={item.name} /> ); } return ( {item.name} ); } return null; })} ); } return ( {data.map((item, index) => ( {item.name} {formatter ? formatter(item.value) : `${legendUnitPrefix}${item.value}${legendUnitPostfix}`} ))} ); } return ( {chartConfig.map((item, index) => { if (!item.invisible) { if (active) { return ( } checkedIcon={ } /> } classes={{ label: classes.name }} label={item.name} /> ); } return ( {item.name} ); } return null; })} ); }; ChartLegend.defaultProps = { chartConfig: [], active: false, vertical: false, legendUnitPrefix: '', legendUnitPostfix: '' }; const m = memo(ChartLegend); export { m as ChartLegend };