{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "stat-group-03",
  "description": "A row of metrics where picking one charts it by day against the previous period.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "tabs",
    "https://www.dashboardblocks.com/r/radix/chart-panel.json",
    "https://www.dashboardblocks.com/r/radix/stat-group.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/stat-group/stat-group-03.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  ChartTooltipContent,\n  chartActiveDot,\n  chartAxisProps,\n  chartGridProps,\n  chartLastDot,\n  chartLineCursor,\n  chartValueAxisWidth,\n  useChartId,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  ChartPanelFigure,\n  ChartPanelLegend,\n  ChartPanelTable,\n} from '@/registry/components/dashboardblocks/chart-panel'\nimport {\n  Stat,\n  StatChange,\n  StatChangeContent,\n  StatGroup,\n  StatLabel,\n  type StatMetric,\n  StatValue,\n  formatStatValue,\n  getStatPlaceholders,\n} from '@/registry/components/dashboardblocks/stat-group'\nimport {\n  formatCompact,\n  formatCurrency,\n  formatNumber,\n  formatPercent,\n} from '@/registry/lib/dashboardblocks/format'\nimport { useState } from 'react'\nimport {\n  Area,\n  CartesianGrid,\n  ComposedChart,\n  Line,\n  ResponsiveContainer,\n  Tooltip,\n  XAxis,\n  YAxis,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\nimport { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'\n\ninterface DailyValue {\n  current: number\n  label: string\n  previous: number\n}\n\ninterface ChartMetric extends StatMetric {\n  daily: DailyValue[]\n}\n\ninterface StatGroup3Props extends BlockStateProps {\n  currentLabel?: string\n  /** Leave out, or pass none, to show the empty state. */\n  metrics?: ChartMetric[]\n  /** How many metrics to sketch while loading. @default 4 */\n  placeholders?: number\n  previousLabel?: string\n  title: string\n}\n\nconst exampleDaily = (\n  base: number,\n  slope: number,\n  wobble: number,\n  previousRatio: number,\n  digits = 0,\n): DailyValue[] =>\n  Array.from({ length: 30 }, (_, day) => {\n    const weekend = day % 7 > 4 ? 0.85 : 1\n    const current = (base + day * slope + Math.sin(day / 2.2) * wobble) * weekend\n    const previous =\n      (base * previousRatio + day * slope * 0.5 + Math.sin(day / 2.8 + 1) * wobble) *\n      weekend\n    return {\n      current: Number(current.toFixed(digits)),\n      label: `Sep ${day + 1}`,\n      previous: Number(previous.toFixed(digits)),\n    }\n  })\n\nconst exampleTotals = (daily: DailyValue[], mode: 'mean' | 'sum', digits = 0) => {\n  const total = (key: 'current' | 'previous') => {\n    const sum = daily.reduce((acc, day) => acc + day[key], 0)\n    return Number((mode === 'sum' ? sum : sum / daily.length).toFixed(digits))\n  }\n  return { daily, previous: total('previous'), value: total('current') }\n}\n\nconst exampleProps: StatGroup3Props = {\n  currentLabel: 'Sep 1 – 30',\n  metrics: [\n    {\n      ...exampleTotals(exampleDaily(1_420, 12, 160, 0.92), 'sum'),\n      formatter: (value) => formatCurrency(value),\n      key: 'revenue',\n      label: 'Revenue',\n    },\n    {\n      ...exampleTotals(exampleDaily(40, 0.2, 5, 0.95), 'sum'),\n      key: 'orders',\n      label: 'Orders',\n    },\n    {\n      ...exampleTotals(exampleDaily(36.2, 0.08, 1.4, 0.96, 2), 'mean', 2),\n      formatter: (value) => formatCurrency(value, { precision: 'exact' }),\n      key: 'aov',\n      label: 'Avg order value',\n    },\n    {\n      ...exampleTotals(exampleDaily(2.1, -0.02, 0.25, 1.1, 1), 'mean', 1),\n      changeType: 'points',\n      formatter: (value) => formatPercent(value / 100),\n      goodDirection: 'down',\n      key: 'refundRate',\n      label: 'Refund rate',\n    },\n  ],\n  previousLabel: 'Aug 2 – 31',\n  title: 'Sales',\n}\n\nconst CURRENT_COLOR = 'var(--color-chart-2)'\nconst PREVIOUS_COLOR = 'var(--color-muted-foreground)'\n\nconst StatGroup3 = (props: StatGroup3Props) => {\n  const {\n    currentLabel = 'This period',\n    metrics = [],\n    placeholders = 4,\n    previousLabel = 'Previous period',\n    errorMessage,\n    status,\n    title,\n  } = props\n  const gradient = useChartId()\n  const [active, setActive] = useState<string>()\n  const state = getBlockState(status, metrics)\n\n  const header = (\n    <header className='flex flex-col gap-0.5'>\n      <h2 className='font-semibold tracking-tight'>{title}</h2>\n      <p className='text-muted-foreground text-sm'>\n        {currentLabel} against {previousLabel}. Pick a metric to chart it by day.\n      </p>\n    </header>\n  )\n\n  if (state) {\n    return (\n      <BlockStateProvider errorMessage={errorMessage} state={state}>\n        <Card>\n          <CardContent>\n            <section className='@container flex flex-col gap-4'>\n              {header}\n              <StatGroup className='grid-cols-2 gap-x-6 gap-y-3 @2xl:grid-cols-4'>\n                {getStatPlaceholders(placeholders).map((metric) => (\n                  <Stat key={metric.key}>\n                    <StatLabel />\n                    <StatValue className='text-3xl @4xl:text-4xl' />\n                    <StatChange className='text-xs' metric={metric} variant='short' />\n                  </Stat>\n                ))}\n              </StatGroup>\n              {state === 'loading' && <ChartPlaceholder className='mt-1' height={224} />}\n            </section>\n          </CardContent>\n        </Card>\n      </BlockStateProvider>\n    )\n  }\n\n  return (\n    <Card>\n      <CardContent>\n        <section className='@container flex flex-col gap-4'>\n          {header}\n          <Tabs\n            value={active ?? metrics[0]?.key}\n            onValueChange={(value) => setActive(String(value))}\n            className='gap-0'\n          >\n            <TabsList\n              aria-label={`${title} metrics`}\n              variant='line'\n              className='grid h-auto w-full grid-cols-2 items-stretch gap-x-6 gap-y-3 rounded-none border-t p-0 group-data-horizontal/tabs:h-auto @2xl:grid-cols-4'\n            >\n              {metrics.map((metric) => (\n                <TabsTrigger\n                  key={metric.key}\n                  value={metric.key}\n                  className='h-auto min-w-0 flex-col items-start justify-start gap-1 rounded-none px-0 pt-3 pb-3 text-left font-normal whitespace-normal'\n                >\n                  <span className='text-muted-foreground text-sm'>{metric.label}</span>\n                  <span className='text-3xl font-light tracking-tight whitespace-nowrap tabular-nums @4xl:text-4xl'>\n                    {formatStatValue(metric)}\n                  </span>\n                  <StatChangeContent\n                    className='text-xs'\n                    comparison={previousLabel}\n                    metric={metric}\n                    variant='short'\n                  />\n                </TabsTrigger>\n              ))}\n            </TabsList>\n            {metrics.map((metric) => {\n              const format = metric.formatter ?? ((value: number) => formatNumber(value))\n              return (\n                <TabsContent\n                  key={metric.key}\n                  value={metric.key}\n                  className='mt-1 flex flex-col gap-3 border-t pt-4'\n                >\n                  <ChartPanelLegend\n                    items={[\n                      { color: CURRENT_COLOR, label: currentLabel, shape: 'line' },\n                      {\n                        color: PREVIOUS_COLOR,\n                        label: previousLabel,\n                        shape: 'line',\n                      },\n                    ]}\n                  />\n                  <ChartPanelFigure className='h-56'>\n                    <ResponsiveContainer width='100%' height='100%'>\n                      <ComposedChart\n                        aria-label={`${metric.label} by day, ${currentLabel} against ${previousLabel}`}\n                        data={metric.daily}\n                        margin={{ top: 8, right: 8, bottom: 0, left: 0 }}\n                      >\n                        <defs>\n                          <linearGradient id={gradient} x1='0' y1='0' x2='0' y2='1'>\n                            <stop\n                              offset='0%'\n                              stopColor={CURRENT_COLOR}\n                              stopOpacity={0.3}\n                            />\n                            <stop\n                              offset='100%'\n                              stopColor={CURRENT_COLOR}\n                              stopOpacity={0}\n                            />\n                          </linearGradient>\n                        </defs>\n                        <CartesianGrid {...chartGridProps} />\n                        <XAxis\n                          {...chartAxisProps}\n                          dataKey='label'\n                          interval='preserveStartEnd'\n                          minTickGap={40}\n                        />\n                        <YAxis\n                          {...chartAxisProps}\n                          domain={['auto', 'auto']}\n                          tickFormatter={(value: number) =>\n                            metric.changeType === 'points'\n                              ? format(value)\n                              : formatCompact(value)\n                          }\n                          width={chartValueAxisWidth}\n                        />\n                        <Tooltip\n                          content={(tooltipProps) => (\n                            <ChartTooltipContent\n                              {...tooltipProps}\n                              valueFormatter={format}\n                            />\n                          )}\n                          cursor={chartLineCursor}\n                          itemSorter={(item) => (item.dataKey === 'current' ? 0 : 1)}\n                        />\n                        <Line\n                          activeDot={chartActiveDot}\n                          dataKey='previous'\n                          dot={false}\n                          name={previousLabel}\n                          stroke={PREVIOUS_COLOR}\n                          strokeDasharray='3 3'\n                          strokeWidth={1.5}\n                          type='monotone'\n                        />\n                        <Area\n                          activeDot={chartActiveDot}\n                          dataKey='current'\n                          dot={chartLastDot(metric.daily.length, CURRENT_COLOR)}\n                          fill={`url(#${gradient})`}\n                          name={currentLabel}\n                          stroke={CURRENT_COLOR}\n                          strokeLinecap='round'\n                          strokeLinejoin='round'\n                          strokeWidth={2}\n                          type='monotone'\n                        />\n                      </ComposedChart>\n                    </ResponsiveContainer>\n                  </ChartPanelFigure>\n                  <ChartPanelTable\n                    caption={`${metric.label} by day, ${currentLabel} vs ${previousLabel}`}\n                    columns={[\n                      { key: 'label', label: 'Day' },\n                      { format, key: 'current', label: currentLabel },\n                      { format, key: 'previous', label: previousLabel },\n                    ]}\n                    rows={metric.daily}\n                  />\n                </TabsContent>\n              )\n            })}\n          </Tabs>\n        </section>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { StatGroup3, exampleProps as statGroup3ExampleProps, type StatGroup3Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/stat-group/stat-group-03.tsx"
    }
  ],
  "meta": {
    "shows": "A row of metrics where picking one charts it by day against the previous period.",
    "size": "full",
    "usedWith": [
      "heatmap-01",
      "geo-04",
      "billing-01"
    ],
    "component": "StatGroup3",
    "props": "{ currentLabel?: string; metrics?: { daily: { current: number; label: string; previous: 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; previousLabel?: string; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
