{"version":3,"file":"Treemap.mjs","names":["Treemap","RechartsTreemap","Tooltip","classes"],"sources":["../../src/Treemap/Treemap.tsx"],"sourcesContent":["import React from 'react';\nimport {\n  Treemap as RechartsTreemap,\n  TreemapProps as RechartsTreemapProps,\n  ResponsiveContainer,\n  Tooltip,\n  TooltipProps,\n} from 'recharts';\nimport {\n  Box,\n  BoxProps,\n  createVarsResolver,\n  ElementProps,\n  factory,\n  Factory,\n  getAutoContrastValue,\n  getThemeColor,\n  isLightColor,\n  MantineColor,\n  MantineTheme,\n  parseThemeColor,\n  rem,\n  StylesApiProps,\n  useMantineTheme,\n  useProps,\n  useResolvedStylesApi,\n  useStyles,\n} from '@mantine/core';\nimport { ChartTooltip, ChartTooltipStylesNames } from '../ChartTooltip/ChartTooltip';\nimport classes from './Treemap.module.css';\n\nexport interface TreemapData {\n  name: string;\n  value?: number;\n  color?: MantineColor;\n  children?: TreemapData[];\n  [key: string]: unknown;\n}\n\nexport type TreemapStylesNames = 'root' | ChartTooltipStylesNames;\nexport type TreemapCssVariables = {\n  root: '--chart-stroke-color' | '--chart-height';\n};\n\nexport interface TreemapProps\n  extends BoxProps, StylesApiProps<TreemapFactory>, ElementProps<'div'> {\n  /** Data used to render chart */\n  data: TreemapData[];\n\n  /** Key in data object for the value @default 'value' */\n  dataKey?: string;\n\n  /** The treemap will try to keep every single rectangle's aspect ratio near the aspectRatio given @default 1.618033988749895 */\n  aspectRatio?: number;\n\n  /** Determines whether the tooltip should be displayed when a node is hovered @default true */\n  withTooltip?: boolean;\n\n  /** Tooltip animation duration in ms @default 0 */\n  tooltipAnimationDuration?: number;\n\n  /** Props passed down to `Tooltip` recharts component */\n  tooltipProps?: Omit<TooltipProps<any, any>, 'ref'>;\n\n  /** Props passed down to recharts `Treemap` component */\n  treemapProps?: Partial<Omit<RechartsTreemapProps, 'ref' | 'data' | 'dataKey'>>;\n\n  /** Controls color of the node stroke, by default depends on color scheme */\n  strokeColor?: MantineColor;\n\n  /** Controls text color of labels @default dimmed */\n  textColor?: MantineColor;\n\n  /** Controls chart height @default 300 */\n  height?: number;\n\n  /** Controls width of node stroke @default 1 */\n  strokeWidth?: number;\n\n  /** A function to format values inside the tooltip */\n  valueFormatter?: (value: number) => string;\n\n  /** Determines whether text color should be adjusted based on background color to improve contrast */\n  autoContrast?: boolean;\n\n  /** Additional elements rendered inside `Treemap` component */\n  children?: React.ReactNode;\n}\n\nexport type TreemapFactory = Factory<{\n  props: TreemapProps;\n  ref: HTMLDivElement;\n  stylesNames: TreemapStylesNames;\n  vars: TreemapCssVariables;\n}>;\n\nconst defaultProps = {\n  dataKey: 'value',\n  withTooltip: true,\n  tooltipAnimationDuration: 0,\n  height: 300,\n  strokeWidth: 1,\n  aspectRatio: 0.5 * (1 + Math.sqrt(5)),\n} satisfies Partial<TreemapProps>;\n\nconst varsResolver = createVarsResolver<TreemapFactory>((theme, { strokeColor, height }) => ({\n  root: {\n    '--chart-stroke-color': strokeColor ? getThemeColor(strokeColor, theme) : undefined,\n    '--chart-height': rem(height),\n  },\n}));\n\ninterface TreemapContentProps {\n  x: number;\n  y: number;\n  width: number;\n  height: number;\n  depth: number;\n  name: string;\n  resolvedColors: Record<string, string>;\n  resolvedLabelColors: Record<string, string>;\n  index: number;\n  strokeWidth: number;\n}\n\nfunction TreemapContent({\n  x,\n  y,\n  width,\n  height,\n  depth,\n  name,\n  resolvedColors,\n  resolvedLabelColors,\n  index,\n  strokeWidth,\n}: TreemapContentProps) {\n  const fill = resolvedColors[name] || `var(--mantine-color-blue-${(index % 9) + 1})`;\n  const labelColor = resolvedLabelColors[name] || 'var(--mantine-color-white)';\n  const showLabel = width > 30 && height > 20;\n\n  return (\n    <g>\n      <rect\n        x={x}\n        y={y}\n        width={width}\n        height={height}\n        fill={depth >= 1 ? fill : 'transparent'}\n        stroke=\"var(--chart-stroke-color, var(--mantine-color-body))\"\n        strokeWidth={depth >= 1 ? strokeWidth : 0}\n      />\n      {depth >= 1 && showLabel && (\n        <text\n          x={x + width / 2}\n          y={y + height / 2}\n          textAnchor=\"middle\"\n          dominantBaseline=\"central\"\n          fill={labelColor}\n          fontSize={12}\n          fontFamily=\"var(--mantine-font-family)\"\n        >\n          {name}\n        </text>\n      )}\n    </g>\n  );\n}\n\nfunction resolveColors(\n  data: TreemapData[],\n  theme: MantineTheme,\n  parentColor?: string\n): Record<string, string> {\n  const result: Record<string, string> = {};\n  for (const item of data) {\n    const color = item.color || parentColor;\n    if (color) {\n      result[item.name] = parseThemeColor({ color, theme }).value;\n    }\n    if (item.children) {\n      Object.assign(result, resolveColors(item.children, theme, color));\n    }\n  }\n  return result;\n}\n\nfunction resolveLabelColors(\n  resolvedColors: Record<string, string>,\n  autoContrast: boolean,\n  luminanceThreshold: number,\n  textColor?: string\n): Record<string, string> {\n  const result: Record<string, string> = {};\n  for (const [name, color] of Object.entries(resolvedColors)) {\n    if (textColor) {\n      result[name] = textColor;\n    } else if (autoContrast) {\n      result[name] = isLightColor(color, luminanceThreshold)\n        ? 'var(--mantine-color-black)'\n        : 'var(--mantine-color-white)';\n    }\n  }\n  return result;\n}\n\nexport const Treemap = factory<TreemapFactory>((_props) => {\n  const props = useProps('Treemap', defaultProps, _props);\n  const {\n    classNames,\n    className,\n    style,\n    styles,\n    unstyled,\n    vars,\n    data,\n    dataKey,\n    aspectRatio,\n    withTooltip,\n    tooltipAnimationDuration,\n    tooltipProps,\n    treemapProps,\n    strokeColor,\n    textColor,\n    height: chartHeight,\n    strokeWidth,\n    valueFormatter,\n    autoContrast,\n    children,\n    attributes,\n    ...others\n  } = props;\n\n  const theme = useMantineTheme();\n  const resolvedColors = resolveColors(data, theme);\n  const _autoContrast = getAutoContrastValue(autoContrast, theme);\n  const _textColor = textColor ? getThemeColor(textColor, theme) : undefined;\n  const resolvedLabelColors = resolveLabelColors(\n    resolvedColors,\n    _autoContrast,\n    theme.luminanceThreshold,\n    _textColor\n  );\n\n  const getStyles = useStyles<TreemapFactory>({\n    name: 'Treemap',\n    classes,\n    props,\n    className,\n    style,\n    classNames,\n    styles,\n    unstyled,\n    attributes,\n    vars,\n    varsResolver,\n  });\n\n  const { resolvedClassNames, resolvedStyles } = useResolvedStylesApi<TreemapFactory>({\n    classNames,\n    styles,\n    props,\n  });\n\n  return (\n    <Box {...getStyles('root')} {...others}>\n      <ResponsiveContainer height={chartHeight}>\n        <RechartsTreemap\n          data={data as any}\n          dataKey={dataKey as any}\n          aspectRatio={aspectRatio}\n          isAnimationActive={false}\n          content={(nodeProps: any) => (\n            <TreemapContent\n              {...nodeProps}\n              resolvedColors={resolvedColors}\n              resolvedLabelColors={resolvedLabelColors}\n              strokeWidth={strokeWidth!}\n            />\n          )}\n          {...treemapProps}\n        >\n          {withTooltip && (\n            <Tooltip\n              animationDuration={tooltipAnimationDuration}\n              isAnimationActive={false}\n              content={({ payload }) => (\n                <ChartTooltip\n                  payload={\n                    payload?.map((item) => ({\n                      name: item.name as string,\n                      value: item.value as number,\n                      color: resolvedColors[item.name as string] || 'var(--mantine-color-blue-6)',\n                    })) || []\n                  }\n                  classNames={resolvedClassNames}\n                  styles={resolvedStyles}\n                  type=\"radial\"\n                  valueFormatter={valueFormatter}\n                  attributes={attributes}\n                />\n              )}\n              {...tooltipProps}\n            />\n          )}\n\n          {children}\n        </RechartsTreemap>\n      </ResponsiveContainer>\n    </Box>\n  );\n});\n\nTreemap.displayName = '@mantine/charts/Treemap';\nTreemap.classes = classes;\nTreemap.varsResolver = varsResolver;\n"],"mappings":";;;;;;;;AAgGA,MAAM,eAAe;CACnB,SAAS;CACT,aAAa;CACb,0BAA0B;CAC1B,QAAQ;CACR,aAAa;CACb,aAAa,MAAO,IAAI,KAAK,KAAK,CAAC;AACrC;AAEA,MAAM,eAAe,oBAAoC,OAAO,EAAE,aAAa,cAAc,EAC3F,MAAM;CACJ,wBAAwB,cAAc,cAAc,aAAa,KAAK,IAAI,KAAA;CAC1E,kBAAkB,IAAI,MAAM;AAC9B,EACF,EAAE;AAeF,SAAS,eAAe,EACtB,GACA,GACA,OACA,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