"use client" import type { BoxProps } from "@chakra-ui/react" import { Box, ColorSwatch, Flex, HStack, Separator, Span, Stack, Text, defineStyle, } from "@chakra-ui/react" import { createContext, useContext, useMemo } from "react" import type { LegendProps, TooltipProps } from "recharts" import { ResponsiveContainer } from "recharts" import type { Payload } from "recharts/types/component/DefaultTooltipContent" import type { PolarViewBox, ViewBox } from "recharts/types/util/types" import { type ChartColor, type UseChartReturn, getProp } from "../use-chart" //////////////////////////////////////////////////////////////////////////////////// const ChartContext = createContext({} as UseChartReturn) const useChartContext = () => useContext(ChartContext) //////////////////////////////////////////////////////////////////////////////////// export interface ChartRootProps extends BoxProps { children: React.ReactElement chart: UseChartReturn } const baseCss = defineStyle({ width: "100%", [`& :where(${[ ".recharts-cartesian-axis-tick-value", ".recharts-polar-angle-axis-tick-value", ".recharts-polar-radius-axis-tick-value", ".recharts-pie-label-text", ].join(", ")})` as unknown as `&`]: { fill: "fg.muted", }, "& .recharts-cartesian-axis .recharts-label": { fill: "fg", fontWeight: "medium", }, "& *": { outline: "none", }, "& svg": { overflow: "visible", }, }) export function ChartRoot(props: ChartRootProps) { const { children, css, chart, ...rest } = props return ( {children} ) } //////////////////////////////////////////////////////////////////////////////////// export interface ChartGradientProps { id: string fillOpacity?: number stops: { color: ChartColor; offset: string | number; opacity?: number }[] } export function ChartGradient(props: ChartGradientProps) { const chart = useChartContext() const { id, fillOpacity, stops } = props return ( {stops.map((stop, index) => ( ))} ) } //////////////////////////////////////////////////////////////////////////////////// export interface ChartLegendProps extends LegendProps { title?: React.ReactNode nameKey?: string interaction?: "hover" | "click" } const hAlignMap = { left: "flex-start", center: "center", right: "flex-end", } export function ChartLegend(props: ChartLegendProps) { const { payload, verticalAlign = "bottom", align = "center", title, orientation, nameKey, spacing = "3", interaction = "hover", } = props const chart = useChartContext() const filteredPayload = payload?.filter( (item) => item.color !== "none" || item.type !== "none", ) if (!filteredPayload?.length) return null const spacingValue = typeof spacing === "number" ? `${spacing}px` : chart.spacing(spacing) return ( {title && {title}} {filteredPayload.map((item, index) => { const config = chart.getSeries(item) const seriesName = config?.name?.toString() const name = getProp(item.payload, nameKey) return ( { if (interaction === "click" && seriesName) { chart.setHighlightedSeries((prev) => prev === seriesName ? null : seriesName, ) } }} onMouseEnter={() => { if (interaction === "hover" && seriesName) { chart.setHighlightedSeries(seriesName) } }} onMouseLeave={() => { if (interaction === "hover" && seriesName) { chart.setHighlightedSeries(null) } }} > {config?.icon || ( )} {name || config?.label} ) })} ) } //////////////////////////////////////////////////////////////////////////////////// export interface ChartTooltipProps extends TooltipProps { hideLabel?: boolean hideIndicator?: boolean hideSeriesLabel?: boolean showTotal?: boolean fitContent?: boolean nameKey?: string indicator?: "line" | "dot" | "dashed" formatter?: ( value: any, name: any, ) => React.ReactNode | [React.ReactNode, React.ReactNode] render?: (item: Payload) => React.ReactNode } export function ChartTooltip(props: ChartTooltipProps) { const { payload: payloadProp, label, labelFormatter, hideLabel, hideIndicator, hideSeriesLabel, showTotal, fitContent, nameKey, formatter, render, } = props const chart = useChartContext() const payload = payloadProp?.filter( (item) => item.color !== "none" || item.type !== "none", ) const total = useMemo(() => chart.getPayloadTotal(payload), [payload, chart]) const tooltipLabel = useMemo(() => { const item = payload?.[0] const itemLabel = `${getProp(item?.payload, nameKey) || label || item?.dataKey || "value"}` return labelFormatter?.(itemLabel, payload ?? []) ?? itemLabel }, [payload, labelFormatter, label, nameKey]) if (!payload?.length) return null return ( {!hideLabel && {tooltipLabel}} {payload.map((item, index) => { const config = chart.getSeries(item) if (render) return render(item.payload) const formatted = formatter ? formatter(item.value, config?.label || item.name) : item.value?.toLocaleString() const [formattedValue, formattedName] = Array.isArray(formatted) ? formatted : [formatted, config?.label || item.name] return ( {config?.icon} {config?.color && !config?.icon && !hideIndicator && ( )} {!hideSeriesLabel && ( {formattedName} )} {item.value && ( {formattedValue} )} ) })} {showTotal && total != null && ( <> Total {(() => { if (!formatter) return total.toLocaleString() const formatted = formatter(total, "") return Array.isArray(formatted) ? formatted[0] : formatted })()} )} ) } //////////////////////////////////////////////////////////////////////////////////// export interface ChartRadialTextProps { viewBox: ViewBox | undefined title: React.ReactNode description: React.ReactNode gap?: number fontSize?: string } const isPolarViewBox = (viewBox: ViewBox): viewBox is PolarViewBox => "cx" in viewBox && "cy" in viewBox export function ChartRadialText(props: ChartRadialTextProps) { const { viewBox, title, description, gap = 24, fontSize = "2rem" } = props const chart = useChartContext() if (!viewBox || !isPolarViewBox(viewBox)) return null return ( {title} {description} ) }