{"version":3,"file":"MatrixChart.mjs","names":["classes"],"sources":["../../src/MatrixChart/MatrixChart.tsx"],"sourcesContent":["import { useMemo, useState } from 'react';\nimport {\n  Box,\n  BoxProps,\n  ElementProps,\n  factory,\n  Factory,\n  getThemeColor,\n  MantineColor,\n  StylesApiProps,\n  Tooltip,\n  TooltipFloatingProps,\n  useMantineTheme,\n  useProps,\n  useStyles,\n} from '@mantine/core';\nimport { getHeatColor } from '../Heatmap/get-heat-color/get-heat-color';\nimport { buildCellMap, getCellKey } from './build-cell-map/build-cell-map';\nimport { getDomain } from './get-domain/get-domain';\nimport { getLegendWidth } from './get-legend-width/get-legend-width';\nimport { getUniqueValues } from './get-unique-values/get-unique-values';\nimport classes from './MatrixChart.module.css';\n\nexport interface MatrixChartCell {\n  x: string;\n  y: string;\n  value: number | null;\n}\n\nexport type MatrixChartData = MatrixChartCell[];\n\nexport type MatrixChartStylesNames =\n  | 'root'\n  | 'cell'\n  | 'xLabel'\n  | 'yLabel'\n  | 'legend'\n  | 'legendLabel'\n  | 'legendRect';\n\nexport interface MatrixChartProps\n  extends\n    BoxProps,\n    StylesApiProps<MatrixChartFactory>,\n    ElementProps<'svg', 'display' | 'opacity' | 'viewBox' | 'width' | 'height'> {\n  /** Array of cell objects with x, y, and value */\n  data: MatrixChartData;\n\n  /** Labels for x-axis (columns), displayed along top or bottom */\n  xLabels?: string[];\n\n  /** Labels for y-axis (rows), displayed along left side */\n  yLabels?: string[];\n\n  /** Value domain [min, max] for color interpolation, auto-calculated if not set */\n  domain?: [number, number];\n\n  /** Array of colors for value interpolation, by default 4 shades of green colors are used */\n  colors?: MantineColor[];\n\n  /** Cell size in px, @default 20 */\n  cellSize?: number;\n\n  /** Gap between cells in px, @default 1 */\n  gap?: number;\n\n  /** Cell border radius, @default 2 */\n  cellRadius?: number;\n\n  /** If set, tooltip is displayed on cell hover, requires `getTooltipLabel` to be set @default false */\n  withTooltip?: boolean;\n\n  /** A function to generate tooltip label based on the hovered cell, required for the tooltip to be visible */\n  getTooltipLabel?: (cell: MatrixChartCell) => React.ReactNode;\n\n  /** Props passed down to the `Tooltip.Floating` component */\n  tooltipProps?: Partial<TooltipFloatingProps>;\n\n  /** Callback to add custom props to cells */\n  getCellProps?: (cell: MatrixChartCell) => React.ComponentProps<'rect'>;\n\n  /** Width reserved for y-axis labels, @default 60 */\n  yLabelsWidth?: number;\n\n  /** Height reserved for x-axis labels, @default 60 */\n  xLabelsHeight?: number;\n\n  /** X-axis label position, @default 'bottom' */\n  xLabelsPosition?: 'top' | 'bottom';\n\n  /** Font size for axis labels, @default 12 */\n  fontSize?: number;\n\n  /** Color for empty/null cells */\n  emptyColor?: MantineColor;\n\n  /** Whether to show legend, @default false */\n  withLegend?: boolean;\n\n  /** Legend labels, @default ['Less', 'More'] */\n  legendLabels?: [string, string];\n\n  /** If set, x-axis labels are displayed, @default false */\n  withXLabels?: boolean;\n\n  /** If set, y-axis labels are displayed, @default false */\n  withYLabels?: boolean;\n\n  /** Rotation angle for x-axis labels in degrees, @default -90 */\n  xLabelsRotation?: number;\n}\n\nexport type MatrixChartFactory = Factory<{\n  props: MatrixChartProps;\n  ref: SVGSVGElement;\n  stylesNames: MatrixChartStylesNames;\n}>;\n\nconst defaultProps = {\n  cellSize: 20,\n  gap: 1,\n  cellRadius: 2,\n  yLabelsWidth: 60,\n  xLabelsHeight: 60,\n  xLabelsPosition: 'bottom',\n  fontSize: 12,\n  xLabelsRotation: -90,\n  colors: [\n    'var(--matrix-level-1)',\n    'var(--matrix-level-2)',\n    'var(--matrix-level-3)',\n    'var(--matrix-level-4)',\n  ],\n  legendLabels: ['Less', 'More'] as [string, string],\n} satisfies Partial<MatrixChartProps>;\n\nconst LEGEND_PADDING = 10;\nconst TEXT_GAP = 6;\n\nexport const MatrixChart = factory<MatrixChartFactory>((_props) => {\n  const props = useProps('MatrixChart', defaultProps, _props);\n  const {\n    classNames,\n    className,\n    style,\n    styles,\n    unstyled,\n    vars,\n    data,\n    xLabels: xLabelsProp,\n    yLabels: yLabelsProp,\n    domain,\n    colors,\n    cellSize,\n    gap,\n    cellRadius,\n    withTooltip,\n    getTooltipLabel,\n    tooltipProps,\n    getCellProps,\n    yLabelsWidth,\n    xLabelsHeight,\n    xLabelsPosition,\n    fontSize,\n    emptyColor,\n    withLegend,\n    legendLabels,\n    withXLabels,\n    withYLabels,\n    xLabelsRotation,\n    attributes,\n    ...others\n  } = props;\n\n  const getStyles = useStyles<MatrixChartFactory>({\n    name: 'MatrixChart',\n    classes,\n    props,\n    className,\n    style,\n    classNames,\n    styles,\n    unstyled,\n    attributes,\n    vars,\n  });\n\n  const [hoveredCell, setHoveredCell] = useState<MatrixChartCell | null>(null);\n\n  const xValues = useMemo(() => getUniqueValues(data, 'x', xLabelsProp), [data, xLabelsProp]);\n\n  const yValues = useMemo(() => getUniqueValues(data, 'y', yLabelsProp), [data, yLabelsProp]);\n\n  const cellMap = useMemo(() => buildCellMap(data), [data]);\n  const [min, max] = useMemo(() => getDomain(data, domain), [data, domain]);\n\n  const theme = useMantineTheme();\n\n  const resolvedColors = useMemo(() => {\n    const palette = colors && colors.length > 0 ? colors : defaultProps.colors;\n    return palette.map((color) => getThemeColor(color, theme));\n  }, [colors, theme]);\n\n  const resolvedEmptyColor = emptyColor ? getThemeColor(emptyColor, theme) : undefined;\n\n  const resolvedCellSize = cellSize!;\n  const resolvedGap = gap!;\n  const cellSizeWithGap = resolvedCellSize + resolvedGap;\n\n  const hasXLabels = withXLabels === true && xValues.length > 0;\n  const hasYLabels = withYLabels === true && yValues.length > 0;\n  const isRotated = xLabelsRotation !== 0 && xLabelsRotation !== undefined;\n\n  const yOffset = hasYLabels ? yLabelsWidth! : 0;\n  const xLabelAtTop = hasXLabels && xLabelsPosition === 'top';\n  const xLabelAtBottom = hasXLabels && xLabelsPosition === 'bottom';\n  const xTopOffset = xLabelAtTop ? xLabelsHeight! : 0;\n  const xBottomHeight = xLabelAtBottom ? xLabelsHeight! : 0;\n\n  const legendHeight = withLegend ? LEGEND_PADDING + resolvedCellSize : 0;\n\n  const gridWidth = Math.max(0, xValues.length * cellSizeWithGap - resolvedGap);\n  const gridHeight = Math.max(0, yValues.length * cellSizeWithGap - resolvedGap);\n\n  const legendWidth = withLegend\n    ? getLegendWidth({\n        legendLabels: legendLabels!,\n        fontSize: fontSize!,\n        colorsCount: resolvedColors.length,\n        cellSize: resolvedCellSize,\n        gap: resolvedGap,\n      })\n    : 0;\n\n  const svgWidth = Math.max(yOffset + gridWidth, legendWidth);\n  const svgHeight = xTopOffset + gridHeight + xBottomHeight + legendHeight;\n\n  const cellNodes = yValues.map((yVal, rowIndex) =>\n    xValues.map((xVal, colIndex) => {\n      const cell = cellMap.get(getCellKey(xVal, yVal));\n      const value = cell?.value ?? null;\n      // Non-finite values are excluded from the domain, so they cannot be interpolated\n      // to a color either – they are treated the same way as missing cells.\n      const isEmpty = value === null || !Number.isFinite(value);\n\n      const fill = isEmpty\n        ? resolvedEmptyColor\n        : getHeatColor({ value, min, max, colors: resolvedColors });\n\n      const extraProps = getCellProps && cell ? getCellProps(cell) : {};\n\n      return (\n        <rect\n          key={`${colIndex}-${rowIndex}`}\n          x={yOffset + colIndex * cellSizeWithGap}\n          y={xTopOffset + rowIndex * cellSizeWithGap}\n          width={resolvedCellSize}\n          height={resolvedCellSize}\n          rx={cellRadius}\n          fill={fill}\n          data-empty={(isEmpty && !emptyColor) || undefined}\n          onPointerEnter={\n            withTooltip\n              ? () => setHoveredCell(cell || { x: xVal, y: yVal, value: null })\n              : undefined\n          }\n          {...getStyles('cell')}\n          {...extraProps}\n        />\n      );\n    })\n  );\n\n  const yLabelNodes = hasYLabels\n    ? yValues.map((label, index) => (\n        <text\n          key={index}\n          x={yOffset - 8}\n          y={xTopOffset + index * cellSizeWithGap + resolvedCellSize / 2}\n          fontSize={fontSize}\n          textAnchor=\"end\"\n          dominantBaseline=\"central\"\n          {...getStyles('yLabel')}\n        >\n          {label}\n        </text>\n      ))\n    : null;\n\n  const xLabelNodes = hasXLabels\n    ? xValues.map((label, index) => {\n        const x = yOffset + index * cellSizeWithGap + resolvedCellSize / 2;\n        const y = xLabelAtTop ? xLabelsHeight! - 8 : xTopOffset + gridHeight + 8;\n\n        return (\n          <text\n            key={index}\n            x={x}\n            y={y}\n            fontSize={fontSize}\n            textAnchor={isRotated ? 'end' : 'middle'}\n            dominantBaseline={isRotated ? 'central' : xLabelAtTop ? 'auto' : 'hanging'}\n            transform={isRotated ? `rotate(${xLabelsRotation}, ${x}, ${y})` : undefined}\n            {...getStyles('xLabel')}\n          >\n            {label}\n          </text>\n        );\n      })\n    : null;\n\n  const label = getTooltipLabel && hoveredCell && withTooltip ? getTooltipLabel(hoveredCell) : null;\n\n  const charWidth = fontSize! * 0.6;\n  const lessLabel = legendLabels![0];\n  const moreLabel = legendLabels![1];\n  const lessWidth = lessLabel.length * charWidth;\n  const allColors = [resolvedEmptyColor, ...resolvedColors];\n  const rectsWidth = allColors.length * resolvedCellSize + (allColors.length - 1) * resolvedGap;\n  const legendX = svgWidth - legendWidth;\n  const legendY = xTopOffset + gridHeight + xBottomHeight + LEGEND_PADDING;\n\n  const legendNode = withLegend ? (\n    <g transform={`translate(${legendX}, ${legendY})`} data-id=\"legend\" {...getStyles('legend')}>\n      <text\n        x={0}\n        y={resolvedCellSize / 2}\n        fontSize={fontSize}\n        dominantBaseline=\"central\"\n        {...getStyles('legendLabel')}\n      >\n        {lessLabel}\n      </text>\n      {allColors.map((color, i) => (\n        <rect\n          key={i}\n          x={lessWidth + TEXT_GAP + i * (resolvedCellSize + resolvedGap)}\n          y={0}\n          width={resolvedCellSize}\n          height={resolvedCellSize}\n          rx={cellRadius}\n          fill={color}\n          data-empty={color === undefined || undefined}\n          {...getStyles('legendRect')}\n        />\n      ))}\n      <text\n        x={lessWidth + TEXT_GAP + rectsWidth + TEXT_GAP}\n        y={resolvedCellSize / 2}\n        fontSize={fontSize}\n        dominantBaseline=\"central\"\n        {...getStyles('legendLabel')}\n      >\n        {moreLabel}\n      </text>\n    </g>\n  ) : null;\n\n  // Individual cells carry no accessible information, so the chart is announced as one image\n  // rather than letting assistive tech walk a grid of anonymous rectangles. Listing every\n  // cell would be unusable on a large matrix, so the generated summary describes its shape\n  // and value range instead – the same approach as `WaffleChart`, bounded for a 2D grid.\n  // A caller-supplied `aria-label` or `aria-labelledby` always wins.\n  const isLabelled = others['aria-label'] !== undefined || others['aria-labelledby'] !== undefined;\n  const gridLabel =\n    xValues.length > 0 && yValues.length > 0\n      ? `Heatmap, ${xValues.length} columns by ${yValues.length} rows, values from ${min} to ${max}`\n      : undefined;\n\n  return (\n    <Box\n      component=\"svg\"\n      width={svgWidth}\n      height={svgHeight}\n      role={isLabelled || gridLabel ? 'img' : undefined}\n      aria-label={isLabelled ? undefined : gridLabel}\n      {...getStyles('root')}\n      {...others}\n    >\n      <Tooltip.Floating\n        label={label}\n        disabled={!withTooltip || label == null}\n        position=\"top\"\n        {...tooltipProps}\n      >\n        <g\n          data-id=\"all-cells\"\n          onPointerLeave={withTooltip ? () => setHoveredCell(null) : undefined}\n        >\n          {withTooltip && (\n            <rect\n              fill=\"transparent\"\n              x={yOffset}\n              y={xTopOffset}\n              width={gridWidth}\n              height={gridHeight}\n            />\n          )}\n          {cellNodes}\n        </g>\n      </Tooltip.Floating>\n      {yLabelNodes}\n      {xLabelNodes}\n      {legendNode}\n    </Box>\n  );\n});\n\nMatrixChart.displayName = '@mantine/charts/MatrixChart';\nMatrixChart.classes = classes;\n"],"mappings":";;;;;;;;;;;AAsHA,MAAM,eAAe;CACnB,UAAU;CACV,KAAK;CACL,YAAY;CACZ,cAAc;CACd,eAAe;CACf,iBAAiB;CACjB,UAAU;CACV,iBAAiB;CACjB,QAAQ;EACN;EACA;EACA;EACA;CACF;CACA,cAAc,CAAC,QAAQ,MAAM;AAC/B;AAEA,MAAM,iBAAiB;AACvB,MAAM,WAAW;AAEjB,MAAa,cAAc,SAA6B,WAAW;CACjE,MAAM,QAAQ,SAAS,eAAe,cAAc,MAAM;CAC1D,MAAM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