"use client" import type { BoxProps, StackProps, Tokens } from "@chakra-ui/react" import { Box, ColorSwatch, Flex, FormatNumber, HStack, Span, Stack, Text, } from "@chakra-ui/react" import * as React from "react" import type { UseChartReturn } from "../use-chart" export interface BarSegmentData { name: string value: number color: string } interface ChartProps { chart: UseChartReturn } const ChartContext = React.createContext({} as UseChartReturn) //////////////////////////////////////////////////////////////////////////////////// export interface BarSegmentRootProps extends StackProps, ChartProps { barSize?: Tokens["sizes"] } export function BarSegmentRoot(props: BarSegmentRootProps) { const { chart, barSize = "10", children, ...rest } = props return ( {children} ) } //////////////////////////////////////////////////////////////////////////////////// export const BarSegmentContent = React.forwardRef( function BarSegmentContent(props, ref) { const chart = React.useContext(ChartContext) return ( chart.setHighlightedSeries(null)} {...props} /> ) }, ) //////////////////////////////////////////////////////////////////////////////////// export interface BarSegmentLabelProps extends StackProps {} export function BarSegmentLabel(props: BarSegmentLabelProps) { const chart = React.useContext(ChartContext) const getPercent = (value: number) => chart.getValuePercent("value", value) return ( {chart.data.map((item) => ( {item.name} ))} ) } //////////////////////////////////////////////////////////////////////////////////// export interface BarSegmentBarProps extends StackProps { tooltip?: boolean | ((props: BarSegmentTooltipProps) => React.ReactNode) } export function BarSegmentBar(props: BarSegmentBarProps) { const { tooltip, children, ...rest } = props const chart = React.useContext(ChartContext) const getPercent = (value: number) => chart.getValuePercent("value", value) return ( {chart.data.map((item) => ( { if (!tooltip) return chart.setHighlightedSeries(item.name) }} style={{ ["--bar-percent" as string]: `${getPercent(item.value)}%`, }} > {typeof tooltip === "function" ? tooltip({ payload: item }) : null} {typeof tooltip === "boolean" && tooltip && ( )} ))} {children} ) } //////////////////////////////////////////////////////////////////////////////////// export interface BarSegmentReferenceProps extends BoxProps { value: number label?: React.ReactNode } export function BarSegmentReference(props: BarSegmentReferenceProps) { const { value, label, ...rest } = props const chart = React.useContext(ChartContext) const getPercent = (value: number) => chart.getValuePercent("value", value) return ( {label && ( {label} )} ) } //////////////////////////////////////////////////////////////////////////////////// export interface BarSegmentValueProps extends StackProps {} export function BarSegmentValue(props: BarSegmentValueProps) { const chart = React.useContext(ChartContext) const getPercent = (value: number) => chart.getValuePercent("value", value) return ( {chart.data.map((item) => ( ))} ) } //////////////////////////////////////////////////////////////////////////////////// export interface BarSegmentLegendProps extends StackProps { showPercent?: boolean showValue?: boolean valueFormatter?: (value: number) => string } export function BarSegmentLegend(props: BarSegmentLegendProps) { const { showPercent, showValue, valueFormatter, ...rest } = props const chart = React.useContext(ChartContext) const formatter = valueFormatter || chart.formatNumber({ maximumFractionDigits: 2 }) return ( {chart.data.map((item) => ( {item.name} {showValue && ( {formatter(item.value)} )} {showPercent && ( {chart.getValuePercent("value", item.value).toFixed(0)}% )} ))} ) } //////////////////////////////////////////////////////////////////////////////////// export interface BarSegmentTooltipProps extends StackProps { payload: BarSegmentData } export function BarSegmentTooltip(props: BarSegmentTooltipProps) { const { payload, ...rest } = props const chart = React.useContext(ChartContext) if (!payload || chart.highlightedSeries !== payload.name) return null const formatter = chart.formatNumber({ maximumFractionDigits: 2 }) return ( {payload.name} {formatter(payload.value)} ) }