{"version":3,"file":"ChartLegend.cjs","names":["getSeriesLabels","ColorSwatch","Box","classes"],"sources":["../../src/ChartLegend/ChartLegend.tsx"],"sourcesContent":["import {\n  Box,\n  BoxProps,\n  ColorSwatch,\n  ElementProps,\n  factory,\n  Factory,\n  StylesApiProps,\n  useProps,\n  useStyles,\n} from '@mantine/core';\nimport { ChartSeries } from '../types';\nimport { getSeriesLabels } from '../utils';\nimport classes from './ChartLegend.module.css';\n\nfunction updateChartLegendPayload(payload: Record<string, any>[]): Record<string, any>[] {\n  return payload.map((item) => {\n    const newDataKey = item.dataKey?.split('.').pop();\n    return {\n      ...item,\n      dataKey: newDataKey,\n      payload: {\n        ...item.payload,\n        name: newDataKey,\n        dataKey: newDataKey,\n      },\n    };\n  });\n}\n\nexport function getFilteredChartLegendPayload(payload: readonly Record<string, any>[]) {\n  return updateChartLegendPayload(payload.filter((item) => item.color !== 'none'));\n}\n\nexport type ChartLegendStylesNames = 'legendItem' | 'legendItemColor' | 'legendItemName' | 'legend';\n\nexport interface ChartLegendProps\n  extends BoxProps, StylesApiProps<ChartLegendFactory>, ElementProps<'div'> {\n  /** Chart data provided by recharts */\n  payload: readonly Record<string, any>[] | undefined;\n\n  /** Function called when mouse enters/leaves one of the legend items */\n  onHighlight: (area: string | null) => void;\n\n  /** Position of the legend relative to the chart, used to apply margin on the corresponding side */\n  legendPosition: 'top' | 'bottom' | 'middle';\n\n  /** Data used for labels, only applicable for area charts: AreaChart, LineChart, BarChart */\n  series?: ChartSeries[];\n\n  /** Determines whether color swatch should be shown next to the label @default true */\n  showColor?: boolean;\n\n  /** Determines whether the legend should be centered @default false */\n  centered?: boolean;\n}\n\nexport type ChartLegendFactory = Factory<{\n  props: ChartLegendProps;\n  ref: HTMLDivElement;\n  stylesNames: ChartLegendStylesNames;\n}>;\n\nexport const ChartLegend = factory<ChartLegendFactory>((_props) => {\n  const props = useProps('ChartLegend', null, _props);\n  const {\n    classNames,\n    className,\n    style,\n    styles,\n    unstyled,\n    vars,\n    payload,\n    onHighlight,\n    legendPosition,\n    mod,\n    series,\n    showColor,\n    centered,\n    attributes,\n    ...others\n  } = props;\n\n  const getStyles = useStyles<ChartLegendFactory>({\n    name: 'ChartLegend',\n    classes,\n    props,\n    className,\n    style,\n    classNames,\n    styles,\n    unstyled,\n    attributes,\n    rootSelector: 'legend',\n  });\n\n  if (!payload) {\n    return null;\n  }\n\n  const filteredPayload = getFilteredChartLegendPayload(payload);\n  const labels = getSeriesLabels(series);\n\n  const items = filteredPayload.map((item, index) => (\n    <div\n      key={index}\n      {...getStyles('legendItem')}\n      onMouseEnter={() => onHighlight(item.dataKey)}\n      onMouseLeave={() => onHighlight(null)}\n      data-without-color={showColor === false || undefined}\n    >\n      <ColorSwatch\n        color={item.color}\n        size={12}\n        {...getStyles('legendItemColor')}\n        withShadow={false}\n      />\n      <p {...getStyles('legendItemName')}>{labels[item.dataKey] || item.dataKey}</p>\n    </div>\n  ));\n\n  return (\n    <Box mod={[{ position: legendPosition, centered }, mod]} {...getStyles('legend')} {...others}>\n      {items}\n    </Box>\n  );\n});\n\nChartLegend.displayName = '@mantine/charts/ChartLegend';\nChartLegend.classes = classes;\n"],"mappings":";;;;;;;AAeA,SAAS,yBAAyB,SAAuD;AACvF,QAAO,QAAQ,KAAK,SAAS;EAC3B,MAAM,aAAa,KAAK,SAAS,MAAM,IAAI,CAAC,KAAK;AACjD,SAAO;GACL,GAAG;GACH,SAAS;GACT,SAAS;IACP,GAAG,KAAK;IACR,MAAM;IACN,SAAS;IACV;GACF;GACD;;AAGJ,SAAgB,8BAA8B,SAAyC;AACrF,QAAO,yBAAyB,QAAQ,QAAQ,SAAS,KAAK,UAAU,OAAO,CAAC;;AAgClF,MAAa,eAAA,GAAA,cAAA,UAA2C,WAAW;CACjE,MAAM,SAAA,GAAA,cAAA,UAAiB,eAAe,MAAM,OAAO;CACnD,MAAM,EACJ,YACA,WACA,OACA,QACA,UACA,MACA,SACA,aACA,gBACA,KACA,QACA,WACA,UACA,YACA,G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