부품 데이터·관리 화면

지표 카드 stat-card

관리 화면 맨 위에 늘어놓는 숫자 카드. 지표 이름, 큰 숫자, 이전 기간 대비 증감(오름·내림 색), 기간 안의 값을 그린 작은 선 그래프.

  • 나온 곳 대시보드
  • KPI
  • 지표
  • 통계
  • 스파크라인
  • 대시보드

설치

shadcn/ui 를 쓰는 Next.js 프로젝트에서 한 줄로 설치됩니다. 파일은 components/bpublish/stat-card.tsx 로 들어오고, 공통 토큰(base)도 같이 설치됩니다. 테마는 테마에서 하나 고릅니다.

npx shadcn@latest add https://www.bpublish.com/r/stat-card.json

레지스트리를 등록했다면 npx shadcn@latest add @bpublish/stat-card · 설치 안내

AI에게 맡기기

Claude Code, Cursor 같은 AI 코딩 도구에 붙여 넣으세요. MCP 서버를 연결해 두면 이름만 말해도 됩니다.

bpublish UI의 지표 카드(stat-card) 부품으로 만들어 줘. 설치: npx shadcn@latest add https://www.bpublish.com/r/stat-card.json · 문서: https://www.bpublish.com/designs/components/stat-card

사용 예시

app/page.tsx
import { StatCard } from '@/components/bpublish/stat-card'

/* 10월 22일 – 11월 20일, 하루 단위 */
const revenue = [434, 401, 469, 493, 425, 477, 443, 452, 454, 539, 476, 480, 456, 481, 456, 478, 498, 553, 459, 464, 454, 471, 466, 580, 534, 484, 467, 478, 489, 533]
const orders = [85, 78, 91, 97, 82, 90, 87, 85, 89, 101, 94, 91, 89, 95, 88, 90, 97, 107, 90, 90, 89, 90, 88, 109, 104, 91, 92, 91, 95, 102]
const visitors = [2389, 2335, 2590, 2733, 2450, 2590, 2501, 2491, 2497, 2891, 2604, 2534, 2520, 2742, 2538, 2495, 2741, 2974, 2469, 2608, 2532, 2574, 2574, 3033, 2838, 2687, 2617, 2703, 2723, 2866]
const conversion = [3.56, 3.34, 3.51, 3.55, 3.35, 3.47, 3.48, 3.41, 3.56, 3.49, 3.61, 3.59, 3.53, 3.46, 3.47, 3.61, 3.54, 3.6, 3.65, 3.45, 3.52, 3.5, 3.42, 3.59, 3.66, 3.39, 3.52, 3.37, 3.49, 3.56]

export default function Example() {
  return (
    <div className="grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-4">
      <StatCard label="매출" value="1억 4,343만원" change={13.3} caption="이전 기간 1억 2,662만원" trend={revenue} />
      <StatCard label="주문" value="2,767" change={13.3} caption="이전 기간 2,442" trend={orders} />
      <StatCard label="방문자" value="78,839" change={8.1} caption="이전 기간 72,934" trend={visitors} />
      <StatCard label="구매 전환율" value="3.51%" change={4.8} caption="이전 기간 3.35%" trend={conversion} />
    </div>
  )
}

속성

이름형식기본값설명
label필수string—지표 이름
value필수string—보일 값. 단위까지 붙여 넘긴다
changenumber—이전 기간 대비 증감률(%). 음수면 내림
captionstring—숫자 아래 보조 줄(이전 기간 값 등)
trendnumber[]—기간 안의 값들. 주면 작은 선 그래프를 그린다
good'up' | 'down''up'줄어드는 것이 좋은 지표(환불률 등)면 'down'. 증감 색이 뒤집힌다

원본 코드

설치 명령 없이 그대로 복사해 넣어도 됩니다. React 외의 패키지는 쓰지 않습니다.

components/bpublish/stat-card.tsx
/**
 * bpublish UI — 지표 카드. https://www.bpublish.com/designs/components/stat-card
 * 관리 화면 맨 위에 네 장쯤 늘어놓는 숫자 카드. 이름, 큰 숫자, 이전 기간 대비 증감, 작은 추이 선.
 */
export type StatCardProps = {
  /** 지표 이름: "매출", "주문" */
  label: string
  /** 화면에 보일 값. 단위까지 붙여 넘긴다: "1억 4,343만원" */
  value: string
  /** 이전 기간 대비 증감률(%). 12.4 는 12.4% 증가 */
  change?: number
  /** 숫자 아래 보조 줄: "이전 기간 1억 2,662만원" */
  caption?: string
  /** 기간 안의 값들. 카드 아래 작은 선 그래프로 그린다 */
  trend?: number[]
  /** 줄어드는 것이 좋은 지표(환불률, 응답 시간)면 'down'. 증감 색이 뒤집힌다 */
  good?: 'up' | 'down'
  className?: string
}

const cx = (...v: Array<string | false | null | undefined>) => v.filter(Boolean).join(' ')

/* 성공 색(--success)이 없는 테마에서는 오름을 글자색으로 차분하게 표시한다. 강조색이 빨강인 테마에서 오름이 내림처럼 읽히지 않게 */
const UP = 'bg-[color-mix(in_oklab,var(--success,var(--foreground))_12%,transparent)] text-[color-mix(in_oklab,var(--success,var(--foreground))_85%,var(--foreground))]'
const DOWN = 'bg-destructive/10 text-destructive'

export function StatCard({ label, value, change, caption, trend, good = 'up', className }: StatCardProps) {
  return (
    <section className={cx('flex flex-col rounded-lg border border-border bg-card p-5 text-card-foreground', className)}>
      <div className="flex items-center justify-between gap-3">
        <h3 className="text-[13px] font-medium text-muted-foreground">{label}</h3>
        {change !== undefined ? <StatDelta value={change} good={good} /> : null}
      </div>
      <p className="mt-2 text-[26px] leading-tight font-semibold tracking-tight tabular-nums">{value}</p>
      {caption ? <p className="mt-1 text-xs text-muted-foreground tabular-nums">{caption}</p> : null}
      {trend && trend.length > 1 ? <Sparkline values={trend} className="mt-4" /> : null}
    </section>
  )
}

function StatDelta({ value, good }: { value: number; good: 'up' | 'down' }) {
  const up = value >= 0
  const better = good === 'up' ? up : !up
  return (
    <span className={cx('inline-flex h-5 shrink-0 items-center gap-0.5 rounded-md px-1.5 text-xs font-semibold tabular-nums', better ? UP : DOWN)}>
      <svg aria-hidden viewBox="0 0 12 12" className={cx('size-3', !up && 'rotate-180')}>
        <path d="M6 2.5 9.5 7h-7L6 2.5Z" fill="currentColor" />
      </svg>
      {Math.abs(value).toFixed(1)}%<span className="sr-only">{up ? ' 증가' : ' 감소'}</span>
    </span>
  )
}

/** 작은 선 그래프. 면은 단색을 옅게 깐다 */
function Sparkline({ values, className }: { values: number[]; className?: string }) {
  const W = 120
  const H = 36
  const min = Math.min(...values)
  const span = Math.max(...values) - min || 1
  const pts = values.map((v, i) => [(i / (values.length - 1)) * W, H - 3 - ((v - min) / span) * (H - 8)] as const)
  const line = monotone(pts)
  return (
    <svg aria-hidden viewBox={`0 0 ${W} ${H}`} preserveAspectRatio="none" className={cx('h-9 w-full overflow-visible', className)}>
      <path d={`${line}L${W},${H}L0,${H}Z`} fill="var(--chart-1)" fillOpacity={0.08} />
      <path d={line} fill="none" stroke="var(--chart-1)" strokeWidth={1.5} vectorEffect="non-scaling-stroke" strokeLinejoin="round" />
    </svg>
  )
}

/** 값의 오르내림을 넘어서지 않는 부드러운 곡선 (Fritsch–Carlson) */
function monotone(pts: ReadonlyArray<readonly [number, number]>) {
  const n = pts.length
  if (n < 2) return n ? `M${pts[0][0]},${pts[0][1]}` : ''
  const dx: number[] = []
  const m: number[] = []
  for (let i = 0; i < n - 1; i++) {
    dx[i] = pts[i + 1][0] - pts[i][0]
    m[i] = (pts[i + 1][1] - pts[i][1]) / (dx[i] || 1)
  }
  const t: number[] = [m[0]]
  for (let i = 1; i < n - 1; i++) {
    t[i] = m[i - 1] * m[i] <= 0 ? 0 : (3 * (dx[i - 1] + dx[i])) / ((2 * dx[i] + dx[i - 1]) / m[i - 1] + (dx[i] + 2 * dx[i - 1]) / m[i])
  }
  t[n - 1] = m[n - 2]
  let d = `M${pts[0][0]},${pts[0][1]}`
  for (let i = 0; i < n - 1; i++) {
    const h = dx[i] / 3
    d += `C${pts[i][0] + h},${pts[i][1] + t[i] * h} ${pts[i + 1][0] - h},${pts[i + 1][1] - t[i + 1] * h} ${pts[i + 1][0]},${pts[i + 1][1]}`
  }
  return d
}