File size: 7,510 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
"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<BarSegmentData>
}
const ChartContext = React.createContext({} as UseChartReturn<BarSegmentData>)
////////////////////////////////////////////////////////////////////////////////////
export interface BarSegmentRootProps extends StackProps, ChartProps {
barSize?: Tokens["sizes"]
}
export function BarSegmentRoot(props: BarSegmentRootProps) {
const { chart, barSize = "10", children, ...rest } = props
return (
<Stack gap="4" {...rest} css={{ "--bar-size": chart.size(barSize) }}>
<ChartContext.Provider value={chart}>{children}</ChartContext.Provider>
</Stack>
)
}
////////////////////////////////////////////////////////////////////////////////////
export const BarSegmentContent = React.forwardRef<HTMLDivElement, StackProps>(
function BarSegmentContent(props, ref) {
const chart = React.useContext(ChartContext)
return (
<Stack
w="full"
gap="1"
ref={ref}
onMouseLeave={() => 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 (
<HStack {...props}>
{chart.data.map((item) => (
<HStack
flexShrink="0"
key={item.name}
textStyle="sm"
fontWeight="medium"
lineClamp={1}
style={{
["--bar-percent" as string]: `${getPercent(item.value)}%`,
}}
flex="var(--bar-percent)"
>
{item.name}
</HStack>
))}
</HStack>
)
}
////////////////////////////////////////////////////////////////////////////////////
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 (
<HStack pos="relative" gap="1" {...rest}>
{chart.data.map((item) => (
<Box
pos="relative"
key={item.name}
flexShrink="0"
flex="var(--bar-percent)"
height="var(--bar-size)"
bg={item.color}
rounded="l1"
onMouseMove={() => {
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 && (
<BarSegmentTooltip payload={item} />
)}
</Box>
))}
{children}
</HStack>
)
}
////////////////////////////////////////////////////////////////////////////////////
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 (
<Box
h={label ? "150%" : "100%"}
pos="absolute"
insetStart={`var(--bar-percent)`}
bottom="0"
style={{ ["--bar-percent" as string]: `${getPercent(value)}%` }}
{...rest}
>
<Flex gap="2" h="full">
<Span
w="2px"
h="100%"
bg="bg.inverted"
outline="2px solid {colors.bg}"
/>
{label && (
<Span fontWeight="medium" textStyle="xs" pos="relative" top="-4px">
{label}
</Span>
)}
</Flex>
</Box>
)
}
////////////////////////////////////////////////////////////////////////////////////
export interface BarSegmentValueProps extends StackProps {}
export function BarSegmentValue(props: BarSegmentValueProps) {
const chart = React.useContext(ChartContext)
const getPercent = (value: number) => chart.getValuePercent("value", value)
return (
<HStack {...props}>
{chart.data.map((item) => (
<HStack
key={item.name}
textStyle="sm"
fontWeight="medium"
flexShrink="0"
style={{
["--bar-percent" as string]: `${getPercent(item.value)}%`,
}}
flex="var(--bar-percent)"
>
<FormatNumber
value={item.value}
notation="compact"
maximumFractionDigits={2}
/>
</HStack>
))}
</HStack>
)
}
////////////////////////////////////////////////////////////////////////////////////
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 (
<HStack wrap="wrap" gap="4" textStyle="sm" {...rest}>
{chart.data.map((item) => (
<HStack key={item.name}>
<ColorSwatch
value={chart.color(item.color)}
boxSize="0.82em"
rounded="full"
/>
<HStack gap="1.5">
<Text>{item.name}</Text>
{showValue && (
<Span fontWeight="medium">{formatter(item.value)}</Span>
)}
{showPercent && (
<Span color="fg.muted">
{chart.getValuePercent("value", item.value).toFixed(0)}%
</Span>
)}
</HStack>
</HStack>
))}
</HStack>
)
}
////////////////////////////////////////////////////////////////////////////////////
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 (
<HStack
pos="absolute"
top="-4"
right="4"
bg="bg.panel"
textStyle="xs"
zIndex="1"
px="2.5"
py="1"
gap="1.5"
rounded="l2"
shadow="md"
{...rest}
>
<ColorSwatch
value={chart.color(payload.color)}
boxSize="0.82em"
rounded="full"
/>
<Span>{payload.name}</Span>
<Span fontFamily="mono" fontWeight="medium">
{formatter(payload.value)}
</Span>
</HStack>
)
}
|