{"version":3,"file":"ScatterChart.cjs","names":["ReferenceLine","Scatter","LabelList","Box","ResponsiveContainer","ReChartsScatterChart","CartesianGrid","XAxis","Label","YAxis","Tooltip","ChartTooltip","Legend","ChartLegend","classes"],"sources":["../../src/ScatterChart/ScatterChart.tsx"],"sourcesContent":["import { useState } from 'react';\nimport {\n  CartesianGrid,\n  Label,\n  LabelList,\n  Legend,\n  ScatterChart as ReChartsScatterChart,\n  ReferenceLine,\n  ResponsiveContainer,\n  Scatter,\n  ScatterProps,\n  Tooltip,\n  XAxis,\n  YAxis,\n} from 'recharts';\nimport {\n  Box,\n  BoxProps,\n  createVarsResolver,\n  ElementProps,\n  factory,\n  Factory,\n  getThemeColor,\n  MantineColor,\n  StylesApiProps,\n  useMantineTheme,\n  useProps,\n  useResolvedStylesApi,\n  useStyles,\n} from '@mantine/core';\nimport { ChartLegend, ChartLegendStylesNames } from '../ChartLegend';\nimport { ChartTooltip, ChartTooltipStylesNames } from '../ChartTooltip';\nimport { BaseChartStylesNames, GridChartBaseProps } from '../types';\nimport classes from '../grid-chart.module.css';\n\nexport interface ScatterChartSeries {\n  color: MantineColor;\n  name: string;\n  data: Record<string, number>[];\n}\n\nexport type ScatterChartStylesNames =\n  | 'scatter'\n  | BaseChartStylesNames\n  | ChartLegendStylesNames\n  | ChartTooltipStylesNames;\n\nexport type ScatterChartCssVariables = {\n  root: '--chart-text-color' | '--chart-grid-color';\n};\n\nexport interface ScatterChartProps\n  extends\n    Omit<GridChartBaseProps, 'dataKey' | 'data' | 'unit' | 'valueFormatter'>,\n    BoxProps,\n    StylesApiProps<ScatterChartFactory>,\n    ElementProps<'div'> {\n  /** Keys that should be used to retrieve data from the data array on x and y axis */\n  dataKey: { x: string; y: string };\n\n  /** Data that is used to build the chart */\n  data: ScatterChartSeries[];\n\n  /** Units displayed after value on axis and inside the tooltip */\n  unit?: { x?: string; y?: string };\n\n  /** Labels that should be used instead of keys names in the tooltip */\n  labels?: { x?: string; y?: string };\n\n  /** A function to format values on x/y axis and in the tooltip */\n  valueFormatter?:\n    | GridChartBaseProps['valueFormatter']\n    | { x?: GridChartBaseProps['valueFormatter']; y?: GridChartBaseProps['valueFormatter'] };\n\n  /** Props passed down to recharts `ScatterChart` component */\n  scatterChartProps?: React.ComponentProps<typeof ReChartsScatterChart>;\n\n  /** Props passed down to recharts `Scatter` component */\n  scatterProps?: Partial<Omit<ScatterProps, 'ref'>>;\n\n  /** If set, displays labels next to points for the given axis */\n  pointLabels?: 'x' | 'y';\n}\n\nfunction getAxis(key: string, dataKey: { x: string; y: string }) {\n  return key === dataKey.x ? 'x' : 'y';\n}\n\nexport type ScatterChartFactory = Factory<{\n  props: ScatterChartProps;\n  ref: HTMLDivElement;\n  stylesNames: ScatterChartStylesNames;\n  vars: ScatterChartCssVariables;\n}>;\n\nconst defaultProps = {\n  withXAxis: true,\n  withYAxis: true,\n  withTooltip: true,\n  tooltipAnimationDuration: 0,\n  tickLine: 'y',\n  strokeDasharray: '5 5',\n  gridAxis: 'x',\n} satisfies Partial<ScatterChartProps>;\n\nconst varsResolver = createVarsResolver<ScatterChartFactory>((theme, { textColor, gridColor }) => ({\n  root: {\n    '--chart-text-color': textColor ? getThemeColor(textColor, theme) : undefined,\n    '--chart-grid-color': gridColor ? getThemeColor(gridColor, theme) : undefined,\n  },\n}));\n\nexport const ScatterChart = factory<ScatterChartFactory>((_props) => {\n  const props = useProps('ScatterChart', defaultProps, _props);\n  const {\n    classNames,\n    className,\n    style,\n    styles,\n    unstyled,\n    vars,\n    referenceLines,\n    dir,\n    withLegend,\n    withTooltip,\n    withXAxis,\n    withYAxis,\n    xAxisProps,\n    yAxisProps,\n    orientation,\n    scatterChartProps,\n    legendProps,\n    data,\n    gridAxis,\n    tickLine,\n    strokeDasharray,\n    gridProps,\n    tooltipAnimationDuration,\n    tooltipProps,\n    children,\n    onMouseLeave,\n    dataKey,\n    textColor,\n    gridColor,\n    xAxisLabel,\n    yAxisLabel,\n    unit,\n    labels,\n    valueFormatter,\n    scatterProps,\n    pointLabels,\n    attributes,\n    ...others\n  } = props;\n\n  const getFormatter = (axis: 'x' | 'y') =>\n    typeof valueFormatter === 'function' ? valueFormatter : valueFormatter?.[axis];\n  const xFormatter = getFormatter('x');\n  const yFormatter = getFormatter('y');\n\n  const theme = useMantineTheme();\n\n  const mappedData = data.map((item) => ({\n    ...item,\n    data: item.data.map((point) => ({ ...point, name: item.name })),\n  }));\n\n  const { resolvedClassNames, resolvedStyles } = useResolvedStylesApi<ScatterChartFactory>({\n    classNames,\n    styles,\n    props,\n  });\n\n  const getStyles = useStyles<ScatterChartFactory>({\n    name: 'ScatterChart',\n    classes,\n    props,\n    className,\n    style,\n    classNames,\n    styles,\n    unstyled,\n    attributes,\n    vars,\n    varsResolver,\n  });\n\n  const withXTickLine = gridAxis !== 'none' && (tickLine === 'x' || tickLine === 'xy');\n  const withYTickLine = gridAxis !== 'none' && (tickLine === 'y' || tickLine === 'xy');\n  const [highlightedArea, setHighlightedArea] = useState<string | null>(null);\n  const shouldHighlight = highlightedArea !== null;\n  const handleMouseLeave = (event: React.MouseEvent<HTMLDivElement>) => {\n    setHighlightedArea(null);\n    onMouseLeave?.(event);\n  };\n\n  const referenceLinesItems = referenceLines?.map((line, index) => {\n    const color = getThemeColor(line.color, theme);\n    return (\n      <ReferenceLine\n        key={index}\n        stroke={line.color ? color : 'var(--chart-grid-color)'}\n        strokeWidth={1}\n        {...line}\n        label={{\n          fill: line.color ? color : 'currentColor',\n          fontSize: 12,\n          position: line.labelPosition ?? 'insideBottomLeft',\n          ...(typeof line.label === 'object' ? line.label : { value: line.label }),\n        }}\n        {...getStyles('referenceLine')}\n      />\n    );\n  });\n\n  const scatters = mappedData.map((item, index) => {\n    const dimmed = shouldHighlight && highlightedArea !== item.name;\n    return (\n      <Scatter\n        data={item.data}\n        fill={getThemeColor(item.color, theme)}\n        key={index}\n        isAnimationActive={false}\n        fillOpacity={dimmed ? 0.1 : 1}\n        {...scatterProps}\n      >\n        {pointLabels && <LabelList dataKey={dataKey[pointLabels]} fontSize={8} dy={10} />}\n        {scatterProps?.children}\n      </Scatter>\n    );\n  });\n\n  return (\n    <Box {...getStyles('root')} onMouseLeave={handleMouseLeave} dir={dir || 'ltr'} {...others}>\n      <ResponsiveContainer {...getStyles('container')}>\n        <ReChartsScatterChart\n          margin={{\n            bottom: xAxisLabel ? 30 : undefined,\n            left: yAxisLabel ? 10 : undefined,\n            right: yAxisLabel ? 5 : undefined,\n          }}\n          {...scatterChartProps}\n        >\n          <CartesianGrid\n            strokeDasharray={strokeDasharray as string}\n            vertical={gridAxis === 'y' || gridAxis === 'xy'}\n            horizontal={gridAxis === 'x' || gridAxis === 'xy'}\n            {...getStyles('grid')}\n            {...gridProps}\n          />\n          <XAxis\n            type=\"number\"\n            hide={!withXAxis}\n            dataKey={dataKey.x}\n            tick={{ transform: 'translate(0, 10)', fontSize: 12, fill: 'currentColor' }}\n            stroke=\"\"\n            interval=\"preserveStartEnd\"\n            tickLine={withXTickLine ? { stroke: 'currentColor' } : false}\n            minTickGap={5}\n            unit={unit?.x}\n            tickFormatter={xFormatter}\n            {...getStyles('axis')}\n            {...xAxisProps}\n          >\n            {xAxisLabel && (\n              <Label position=\"insideBottom\" offset={-20} fontSize={12} {...getStyles('axisLabel')}>\n                {xAxisLabel}\n              </Label>\n            )}\n            {xAxisProps?.children}\n          </XAxis>\n          <YAxis\n            type=\"number\"\n            hide={!withYAxis}\n            axisLine={false}\n            dataKey={dataKey.y}\n            tickLine={withYTickLine ? { stroke: 'currentColor' } : false}\n            tick={{ transform: 'translate(-10, 0)', fontSize: 12, fill: 'currentColor' }}\n            allowDecimals\n            unit={unit?.y}\n            tickFormatter={yFormatter}\n            {...getStyles('axis')}\n            {...yAxisProps}\n          >\n            {yAxisLabel && (\n              <Label\n                position=\"insideLeft\"\n                angle={-90}\n                textAnchor=\"middle\"\n                fontSize={12}\n                offset={-5}\n                {...getStyles('axisLabel')}\n              >\n                {yAxisLabel}\n              </Label>\n            )}\n            {yAxisProps?.children}\n          </YAxis>\n\n          {withTooltip && (\n            <Tooltip\n              animationDuration={tooltipAnimationDuration}\n              isAnimationActive={tooltipAnimationDuration !== 0}\n              position={{ y: 0 }}\n              cursor={{\n                stroke: 'var(--chart-grid-color)',\n                strokeWidth: 1,\n                strokeDasharray,\n              }}\n              content={({ label, payload, labelFormatter }) => (\n                <ChartTooltip\n                  type=\"scatter\"\n                  label={labelFormatter && payload ? labelFormatter(label, payload) : label}\n                  payload={\n                    labels\n                      ? payload?.map((item) => ({\n                          ...item,\n                          name: labels[getAxis(item.name as string, dataKey)] || item.name,\n                          value:\n                            getFormatter(getAxis(item.name as string, dataKey))?.(\n                              item.value as number\n                            ) ?? item.value,\n                        }))\n                      : payload?.map((item) => ({\n                          ...item,\n                          value:\n                            getFormatter(getAxis(item.name as string, dataKey))?.(\n                              item.value as number\n                            ) ?? item.value,\n                        }))\n                  }\n                  classNames={resolvedClassNames}\n                  styles={resolvedStyles}\n                  series={data}\n                  attributes={attributes}\n                />\n              )}\n              {...tooltipProps}\n            />\n          )}\n\n          {withLegend && (\n            <Legend\n              verticalAlign=\"top\"\n              content={(payload) => (\n                <ChartLegend\n                  payload={payload.payload?.map((item, index) => ({\n                    ...item,\n                    dataKey: data[index].name,\n                  }))}\n                  onHighlight={setHighlightedArea}\n                  legendPosition={legendProps?.verticalAlign || 'top'}\n                  classNames={resolvedClassNames}\n                  styles={resolvedStyles}\n                  series={data}\n                  attributes={attributes}\n                />\n              )}\n              height={44}\n              {...legendProps}\n            />\n          )}\n\n          {referenceLinesItems}\n          {scatters}\n        </ReChartsScatterChart>\n      </ResponsiveContainer>\n    </Box>\n  );\n});\n\nScatterChart.displayName = '@mantine/charts/ScatterChart';\nScatterChart.classes = classes;\nScatterChart.varsResolver = varsResolver;\n\nexport namespace ScatterChart {\n  export type Props = ScatterChartProps;\n  export type StylesNames = ScatterChartStylesNames;\n  export type CssVariables = ScatterChartCssVariables;\n  export type Factory = ScatterChartFactory;\n  export type Series = ScatterCh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