{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "stat-group-02",
  "description": "A row of metrics, each with its change and a 14-day sparkline, under a sentence saying how many improved.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/kpi.json",
    "https://www.dashboardblocks.com/r/radix/stat-group.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/stat-group/stat-group-02.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { getKPIChange } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  Stat,\n  StatChange,\n  StatGroup,\n  StatLabel,\n  type StatMetric,\n  StatSparkline,\n  StatValue,\n  formatStatValue,\n  getStatPlaceholders,\n} from '@/registry/components/dashboardblocks/stat-group'\nimport {\n  formatCurrency,\n  formatNumber,\n  formatPercent,\n} from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface SparklineMetric extends StatMetric {\n  /** One value per day, oldest first. */\n  history: number[]\n}\n\ninterface StatGroup2Props extends BlockStateProps {\n  /** Follows each previous value, such as `the 14 days before`. @default 'the previous period' */\n  comparison?: string\n  /** Defaults to a sentence saying how many metrics improved. */\n  description?: string\n  /** Leave out, or pass none, to show the empty state. */\n  metrics?: SparklineMetric[]\n  /** How many metrics to sketch while loading. @default 4 */\n  placeholders?: number\n  title: string\n}\n\nconst exampleHistory = (end: number, slope: number, wobble: number, phase = 0) =>\n  Array.from({ length: 14 }, (_, day) =>\n    Number((end - (13 - day) * slope + Math.sin(day / 1.3 + phase) * wobble).toFixed(2)),\n  )\n\nconst exampleProps: StatGroup2Props = {\n  comparison: 'the 14 days before',\n  metrics: [\n    {\n      history: exampleHistory(1_910, 22, 140),\n      key: 'visitors',\n      label: 'Visitors',\n      previous: 21_640,\n      value: 24_318,\n    },\n    {\n      changeType: 'points',\n      formatter: (value) => formatPercent(value / 100),\n      history: exampleHistory(3.4, 0.03, 0.12, 2),\n      key: 'conversion',\n      label: 'Conversion rate',\n      previous: 3.1,\n      value: 3.4,\n    },\n    {\n      formatter: (value) => formatCurrency(value, { precision: 'exact' }),\n      history: exampleHistory(37.6, 0.18, 0.7, 4),\n      key: 'aov',\n      label: 'Avg order value',\n      previous: 35.45,\n      value: 37.55,\n    },\n    {\n      changeType: 'points',\n      formatter: (value) => formatPercent(value / 100),\n      goodDirection: 'down',\n      history: exampleHistory(40.5, -0.25, 1, 1),\n      key: 'bounce',\n      label: 'Bounce rate',\n      previous: 44,\n      value: 41,\n    },\n  ],\n  title: 'Store performance',\n}\n\nconst describeHistory = (metric: SparklineMetric) => {\n  const format = metric.formatter ?? ((value: number) => formatNumber(value))\n  const first = metric.history[0]\n  const last = metric.history[metric.history.length - 1]\n  const direction = last > first ? 'Up' : last < first ? 'Down' : 'Flat'\n  return `${metric.label} by day: ${direction} from ${format(first)} to ${format(last)}`\n}\n\nconst daysAgo = (count: number) =>\n  Array.from({ length: count }, (_, index) => {\n    const ago = count - 1 - index\n    return ago === 0 ? 'Today' : ago === 1 ? 'Yesterday' : `${ago} days ago`\n  })\n\nfunction summarize(metrics: SparklineMetric[], comparison: string) {\n  const improved = metrics.filter((metric) => {\n    const change = getKPIChange(metric) ?? 0\n    return metric.goodDirection === 'down' ? change < 0 : change > 0\n  }).length\n  if (improved === metrics.length)\n    return `All ${improved} metrics improved on ${comparison}.`\n  return `${improved} of ${metrics.length} metrics improved on ${comparison}.`\n}\n\nconst StatGroup2 = (props: StatGroup2Props) => {\n  const {\n    comparison = 'the previous period',\n    metrics = [],\n    placeholders = 4,\n    errorMessage,\n    status,\n    title,\n  } = props\n  const { description = summarize(metrics, comparison) } = props\n  const shown: SparklineMetric[] =\n    status === 'loading'\n      ? getStatPlaceholders(placeholders).map((metric) => ({\n          ...metric,\n          history: [],\n        }))\n      : metrics\n\n  return (\n    <BlockStateProvider\n      errorMessage={errorMessage}\n      state={getBlockState(status, metrics)}\n    >\n      <Card>\n        <CardContent>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex flex-col gap-0.5'>\n              <h2 className='font-semibold tracking-tight'>{title}</h2>\n              <BlockReady className='text-sm'>\n                <p className='text-muted-foreground text-sm'>{description}</p>\n              </BlockReady>\n            </header>\n            <StatGroup className='grid-cols-1 @md:grid-cols-2 @2xl:grid-cols-4'>\n              {shown.map((metric) => (\n                <Stat\n                  key={metric.key}\n                  className='grid grid-cols-[minmax(0,1fr)_6rem] content-start gap-x-4 gap-y-1 @md:grid-cols-1'\n                >\n                  <StatLabel>{metric.label}</StatLabel>\n                  <StatValue className='text-3xl @4xl:text-4xl'>\n                    {formatStatValue(metric)}\n                  </StatValue>\n                  <StatChange comparison={comparison} metric={metric} variant='short' />\n                  <dd className='col-start-2 row-span-3 row-start-1 self-center @md:col-start-1 @md:row-span-1 @md:row-start-4 @md:mt-3'>\n                    <StatSparkline\n                      data={metric.history}\n                      formatter={metric.formatter}\n                      label={describeHistory(metric)}\n                      labels={daysAgo(metric.history.length)}\n                    />\n                  </dd>\n                </Stat>\n              ))}\n            </StatGroup>\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { StatGroup2, exampleProps as statGroup2ExampleProps, type StatGroup2Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/stat-group/stat-group-02.tsx"
    }
  ],
  "meta": {
    "shows": "A row of metrics, each with its change and a 14-day sparkline, under a sentence saying how many improved.",
    "size": "full",
    "usedWith": [
      "chart-panel-01",
      "breakdown-01",
      "data-table-02"
    ],
    "component": "StatGroup2",
    "props": "{ comparison?: string; description?: string; metrics?: { history: number[]; changeType?: \"percent\" | \"points\" | \"count\"; detail?: ReactNode; formatter?: (value: number) => string; goodDirection?: \"up\" | \"down\"; key: string; label: string; previous?: number; tone?: \"warning\" | \"bad\"; value: number }[]; placeholders?: number; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
