Chart Stat Flow
bklit-ui/chart-stat-flowFreeComponent
Animated stat value and label for chart overlays
Install it
npx shadcn@latest add https://bklit.com/r/chart-stat-flow.jsonSource
1"use client";23import NumberFlow from "@number-flow/react";4import { type ReactNode, useEffect, useMemo, useState } from "react";5import { cn } from "@/lib/utils";67/** Subset of `Intl.NumberFormatOptions` supported by NumberFlow */8export interface ChartStatFlowFormat {9 notation?: "standard" | "compact";10 compactDisplay?: "short" | "long";11 minimumFractionDigits?: number;12 maximumFractionDigits?: number;13 minimumIntegerDigits?: number;14 minimumSignificantDigits?: number;15 maximumSignificantDigits?: number;16 style?: "decimal" | "percent" | "currency";17 currency?: string;18 currencyDisplay?: "symbol" | "narrowSymbol" | "code" | "name";19 unit?: string;20 unitDisplay?: "short" | "long" | "narrow";21}2223export const defaultChartStatFlowFormat: ChartStatFlowFormat = {24 notation: "standard",25 maximumFractionDigits: 0,26};2728function formatStatValue(29 value: number,30 formatOptions: ChartStatFlowFormat,31 prefix?: string,32 suffix?: string33): string {34 const formatted = new Intl.NumberFormat(undefined, formatOptions).format(35 value36 );37 return `${prefix ?? ""}${formatted}${suffix ?? ""}`;38}3940function useNumberFlowElementReady(): boolean {41 const [ready, setReady] = useState(42 () =>43 typeof customElements !== "undefined" &&44 Boolean(customElements.get("number-flow-react"))45 );4647 useEffect(() => {48 if (ready) {49 return;50 }51 let cancelled = false;52 customElements.whenDefined("number-flow-react").then(() => {53 if (!cancelled) {54 setReady(true);55 }56 });57 return () => {58 cancelled = true;59 };60 }, [ready]);6162 return ready;63}6465export interface ChartStatFlowProps {66 value: number;67 label: string;68 formatOptions?: ChartStatFlowFormat;69 prefix?: string;70 suffix?: string;71 valueClassName?: string;72 labelClassName?: string;73 icon?: ReactNode;74}7576/**77 * Shared value + label stack using NumberFlow (same layout as pie / ring centers).78 * Parent should provide flex alignment and sizing when needed.79 */80export function ChartStatFlow({81 value,82 label,83 formatOptions = defaultChartStatFlowFormat,84 prefix,85 suffix,86 valueClassName = "text-2xl font-bold",87 labelClassName = "text-xs",88 icon,89}: ChartStatFlowProps) {90 const numberFlowReady = useNumberFlowElementReady();91 const staticValue = useMemo(92 () => formatStatValue(value, formatOptions, prefix, suffix),93 [value, formatOptions, prefix, suffix]94 );9596 return (97 <>98 {icon ? (99// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from bklit-ui
All 56 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Area Chartarea-chart | bklit-ui | Components | Free | 4 deps · 14 files | |
| Chart Backgroundbackground | bklit-ui | Components | Free | 2 deps · 3 files | |
| Bar Chartbar-chart | bklit-ui | Components | Free | 4 deps · 13 files | |
| Bar Depthbar-depth | bklit-ui | Components | Free | 1 dep | |
| Candlestick Chartcandlestick-chart | bklit-ui | Components | Free | 4 deps · 4 files | |
| Chart Animationchart-animation | bklit-ui | Components | Free | 1 dep · 7 files | |
| Chart Contextchart-context | bklit-ui | Components | Free | 5 deps · 12 files | |
| Chart Series Layerchart-series | bklit-ui | Components | Free | 3 deps · 15 files |
