{"version":3,"sources":["../src/use-pie-chart.ts"],"sourcesContent":["import type {\n  CSSUIObject,\n  CSSUIProps,\n  PropGetter,\n  RequiredPropGetter,\n} from \"@yamada-ui/core\"\nimport type { Dict } from \"@yamada-ui/utils\"\nimport type { ComponentPropsWithoutRef } from \"react\"\nimport type * as Recharts from \"recharts\"\nimport type { CellProps, PieChartProps, PieProps } from \"./chart.types\"\nimport { getVar, useTheme } from \"@yamada-ui/core\"\nimport { cx } from \"@yamada-ui/utils\"\nimport { useCallback, useMemo, useState } from \"react\"\nimport { getClassName, getComponentProps } from \"./chart-utils\"\nimport { pieChartLabel, pieChartLabelLine } from \"./pie-chart-label\"\nimport { pieChartProperties, pieProperties } from \"./rechart-properties\"\n\nexport interface UsePieChartOptions {\n  /**\n   * Chart data.\n   */\n  data: CellProps[]\n  /**\n   * Controls angle at which chart ends.\n   *\n   * @default -270\n   */\n  endAngle?: number\n  /**\n   * Controls fill opacity of all pies.\n   *\n   * @default 1\n   */\n  fillOpacity?: [number, number] | number\n  /**\n   * Controls innerRadius of the chart segments.\n   * If it is a number, it is the width of the radius.\n   * For example, `60` means the radius is `60px` and the diameter is `120px`.\n   *\n   * @default '0%'\n   */\n  innerRadius?: number | string\n  /**\n   * Determines whether labels should be displayed as percentages.\n   *\n   * @default false\n   *\n   * @deprecated Use `percent` instead.\n   */\n  isPercent?: boolean\n  /**\n   * A function to format labels.\n   */\n  labelFormatter?: (value: number) => string\n  /**\n   * Distance between chart and label.\n   */\n  labelOffset?: number\n  /**\n   * Controls thickness of the chart segments. If it is a number, it is calculated as px.\n   * If it is a number, it is the width of the radius.\n   * For example, `60` means the radius is `60px` and the diameter is `120px`.\n   *\n   * @default '80%'\n   */\n  outerRadius?: number | string\n  /**\n   * Controls padding between segments.\n   *\n   * @default 0\n   */\n  paddingAngle?: number\n  /**\n   * Determines whether labels should be displayed as percentages.\n   *\n   * @default false\n   */\n  percent?: boolean\n  /**\n   * Controls angle at which chart starts.\n   *\n   * @default 90\n   */\n  startAngle?: number\n  /**\n   * Stroke width for the chart pies.\n   *\n   * @default 1\n   */\n  strokeWidth?: number\n  /**\n   * Determines whether segments labels should have lines that connect the segment with the label.\n   *\n   * @default false\n   */\n  withLabelLines?: boolean\n  /**\n   * Determines whether each segment should have associated label.\n   *\n   * @default false\n   */\n  withLabels?: boolean\n  /**\n   * Props for the cell.\n   */\n  cellProps?: Partial<CellProps>\n  /**\n   * Props passed down to recharts `PieChart` component.\n   */\n  chartProps?: PieChartProps\n  /**\n   * Props for the pie.\n   */\n  pieProps?: Partial<PieProps>\n}\n\ninterface UsePieChartProps extends UsePieChartOptions {\n  styles: Dict<CSSUIObject | undefined>\n}\n\nexport const usePieChart = ({\n  data,\n  endAngle = -270,\n  fillOpacity = 1,\n  innerRadius = \"0%\",\n  isPercent = false,\n  labelFormatter,\n  labelOffset,\n  withLabels = false,\n  outerRadius = withLabels ? \"80%\" : \"100%\",\n  paddingAngle = 0,\n  percent = isPercent,\n  startAngle = 90,\n  strokeWidth = 1,\n  styles,\n  withLabelLines = false,\n  ...rest\n}: UsePieChartProps) => {\n  const { theme } = useTheme()\n  const [highlightedArea, setHighlightedArea] = useState<null | string>(null)\n  const shouldHighlight = highlightedArea !== null\n  const { dimCell, ...computedCellProps } = rest.cellProps ?? {}\n  const {\n    activeShape = {},\n    inactiveShape = {},\n    label: labelProps,\n    labelLine: labelLineProps,\n    ...computedPieProps\n  } = rest.pieProps ?? {}\n\n  const cellColors: CSSUIProps[\"vars\"] = useMemo(\n    () =>\n      data.map(({ color }, index) => ({\n        name: `cell-${index}`,\n        token: \"colors\",\n        value: color ?? \"transparent\",\n      })),\n    [data],\n  )\n\n  const pieVars: CSSUIProps[\"vars\"] = useMemo(\n    () =>\n      [\n        ...cellColors,\n        { name: \"fill-opacity\", value: fillOpacity },\n      ] as Required<CSSUIProps>[\"vars\"],\n    [fillOpacity, cellColors],\n  )\n\n  const [chartProps, chartClassName] = useMemo(\n    () =>\n      getComponentProps<Dict, string>(\n        [rest.chartProps ?? {}, pieChartProperties],\n        styles.chart,\n      )(theme),\n    [rest.chartProps, styles.chart, theme],\n  )\n\n  const [pieProps, pieClassName] = useMemo(\n    () =>\n      getComponentProps<Dict, string>(\n        [computedPieProps, pieProperties],\n        styles.pie,\n      )(theme),\n    [computedPieProps, styles.pie, theme],\n  )\n\n  const cellClassName = useMemo(() => {\n    const resolvedCellProps = {\n      fillOpacity: \"$fill-opacity\",\n      ...styles.cell,\n      ...computedCellProps,\n    }\n\n    return getClassName(resolvedCellProps)(theme)\n  }, [computedCellProps, styles.cell, theme])\n\n  const dimCellClassName = useMemo(() => {\n    const resolvedDimCell = { fillOpacity: 0.3, strokeOpacity: 0, ...dimCell }\n\n    return getClassName(resolvedDimCell)(theme)\n  }, [dimCell, theme])\n\n  const activeShapeProps = useMemo(\n    () =>\n      getComponentProps<Dict, string>(\n        [{ _focus: { outline: \"none\" }, ...activeShape }, pieProperties],\n        styles.activeShape,\n      )(theme, true),\n    [activeShape, styles.activeShape, theme],\n  )\n\n  const inactiveShapeProps = useMemo(\n    () =>\n      getComponentProps<Dict, string>(\n        [inactiveShape, pieProperties],\n        styles.inactiveShape,\n      )(theme, true),\n    [inactiveShape, styles.inactiveShape, theme],\n  )\n\n  const label: Recharts.PieLabel = useCallback(\n    (props: any) =>\n      pieChartLabel({\n        calcPercent: percent,\n        labelFormatter,\n        labelOffset,\n        styles: styles.label,\n        labelProps,\n        ...props,\n      }),\n    [labelOffset, labelProps, styles.label, percent, labelFormatter],\n  )\n\n  const labelLine = useCallback(\n    (props: any) => {\n      return pieChartLabelLine({\n        labelOffset,\n        styles: styles.labelLine,\n        labelLineProps,\n        ...props,\n      })\n    },\n    [labelLineProps, labelOffset, styles.labelLine],\n  )\n\n  const cellPropList = useMemo(\n    () =>\n      data.map((props, index) => {\n        const { name, dimCell = {}, ...computedProps } = props\n        const color = getVar(`cell-${index}`)(theme)\n        const dimmed = shouldHighlight && highlightedArea !== name\n        const resolvedProps = {\n          ...computedProps,\n          ...(dimmed ? dimCell : {}),\n        }\n\n        const className = getClassName(\n          {\n            cellClassName,\n            ...resolvedProps,\n          },\n          dimmed ? dimCellClassName : undefined,\n        )(theme)\n\n        return {\n          className,\n          color,\n        }\n      }),\n    [\n      cellClassName,\n      data,\n      dimCellClassName,\n      highlightedArea,\n      shouldHighlight,\n      theme,\n    ],\n  )\n\n  const getPieChartProps: PropGetter<\n    ComponentPropsWithoutRef<typeof Recharts.PieChart>,\n    ComponentPropsWithoutRef<typeof Recharts.PieChart>\n  > = useCallback(\n    ({ className, ...props } = {}, ref = null) => ({\n      ref,\n      className: cx(className, chartClassName),\n      ...props,\n      ...chartProps,\n    }),\n    [chartProps, chartClassName],\n  )\n\n  const getPieProps: RequiredPropGetter<\n    Partial<Recharts.PieProps>,\n    Omit<Recharts.PieProps, \"ref\">\n  > = useCallback(\n    ({ className, ...props }, ref = null) => ({\n      ref,\n      className: cx(className, pieClassName),\n      activeShape: activeShapeProps,\n      data,\n      dataKey: \"value\",\n      endAngle,\n      inactiveShape: inactiveShapeProps,\n      innerRadius,\n      isAnimationActive: false,\n      label: withLabels ? label : false,\n      labelLine: withLabelLines ? labelLine : false,\n      outerRadius,\n      paddingAngle,\n      rootTabIndex: -1,\n      startAngle,\n      ...(props as Omit<Recharts.PieProps, \"dataKey\">),\n      ...pieProps,\n    }),\n    [\n      pieClassName,\n      data,\n      outerRadius,\n      innerRadius,\n      paddingAngle,\n      startAngle,\n      endAngle,\n      withLabels,\n      label,\n      withLabelLines,\n      labelLine,\n      activeShapeProps,\n      inactiveShapeProps,\n      pieProps,\n    ],\n  )\n\n  const getCellProps: RequiredPropGetter<\n    { index: number } & Omit<Recharts.CellProps, \"ref\">,\n    Recharts.CellProps\n  > = useCallback(\n    ({ className: classNameProp, index, ...props }, ref = null) => {\n      const { className, color } = cellPropList[index] ?? {}\n\n      return {\n        ref,\n        className: cx(classNameProp, className),\n        fill: color,\n        stroke: color,\n        strokeWidth,\n        ...(props as Recharts.CellProps),\n      }\n    },\n    [cellPropList, strokeWidth],\n  )\n\n  return {\n    pieVars,\n    setHighlightedArea,\n    getCellProps,\n    getPieChartProps,\n    getPieProps,\n  }\n}\n\nexport type UsePieChartReturn = ReturnType<typeof usePieChart>\n"],"mappings":";;;;;;;;;;;;;;;AAUA,SAAS,QAAQ,gBAAgB;AACjC,SAAS,UAAU;AACnB,SAAS,aAAa,SAAS,gBAAgB;AA4GxC,IAAM,cAAc,CAAC;AAAA,EAC1B;AAAA,EACA,WAAW;AAAA,EACX,cAAc;AAAA,EACd,cAAc;AAAA,EACd,YAAY;AAAA,EACZ;AAAA,EACA;AAAA,EACA,aAAa;AAAA,EACb,cAAc,aAAa,QAAQ;AAAA,EACnC,eAAe;AAAA,EACf,UAAU;AAAA,EACV,aAAa;AAAA,EACb,cAAc;AAAA,EACd;AAAA,EACA,iBAAiB;AAAA,EACjB,GAAG;AACL,MAAwB;AAzIxB;AA0IE,QAAM,EAAE,MAAM,IAAI,SAAS;AAC3B,QAAM,CAAC,iBAAiB,kBAAkB,IAAI,SAAwB,IAAI;AAC1E,QAAM,kBAAkB,oBAAoB;AAC5C,QAAM,EAAE,SAAS,GAAG,kBAAkB,KAAI,UAAK,cAAL,YAAkB,CAAC;AAC7D,QAAM;AAAA,IACJ,cAAc,CAAC;AAAA,IACf,gBAAgB,CAAC;AAAA,IACjB,OAAO;AAAA,IACP,WAAW;AAAA,IACX,GAAG;AAAA,EACL,KAAI,UAAK,aAAL,YAAiB,CAAC;AAEtB,QAAM,aAAiC;AAAA,IACrC,MACE,KAAK,IAAI,CAAC,EAAE,MAAM,GAAG,WAAW;AAAA,MAC9B,MAAM,QAAQ,KAAK;AAAA,MACnB,OAAO;AAAA,MACP,OAAO,wBAAS;AAAA,IAClB,EAAE;AAAA,IACJ,CAAC,IAAI;AAAA,EACP;AAEA,QAAM,UAA8B;AAAA,IAClC,MACE;AAAA,MACE,GAAG;AAAA,MACH,EAAE,MAAM,gBAAgB,OAAO,YAAY;AAAA,IAC7C;AA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