{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bar-chart-kpi-02",
  "description": "Each day's value with its goal marked on the bar, the share of the goal reached and how many days met it.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/kpi/bar-chart-kpi-02.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  BlockStateText,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  ChartTooltipFrame,\n  ChartTooltipItem,\n  chartAxisProps,\n  chartBarCursor,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  KPI,\n  KPIChart,\n  KPIContent,\n  KPITitle,\n  type KPIFormat,\n  KPIValue,\n  getKPIFormatter,\n} from '@/registry/components/dashboardblocks/kpi'\nimport { formatPercent } from '@/registry/lib/dashboardblocks/format'\nimport { Bar, BarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\nimport { cn } from '@/lib/utils'\n\ninterface DailyGoal {\n  goal: number\n  label: string\n  value: number\n}\n\ninterface BarChartKPI2Props extends BlockStateProps {\n  /** Leave out, or pass no days, to show the empty state. */\n  data?: DailyGoal[]\n  format?: KPIFormat\n  /** @default 144 */\n  height?: number\n  title: string\n}\n\nconst exampleProps: BarChartKPI2Props = {\n  data: [\n    { goal: 3_800, label: 'Mon', value: 3_200 },\n    { goal: 4_200, label: 'Tue', value: 4_500 },\n    { goal: 3_400, label: 'Wed', value: 2_800 },\n    { goal: 4_800, label: 'Thu', value: 5_200 },\n    { goal: 6_400, label: 'Fri', value: 6_100 },\n    { goal: 5_200, label: 'Sat', value: 4_800 },\n    { goal: 6_000, label: 'Sun', value: 5_700 },\n  ],\n  format: 'currency',\n  title: 'Revenue vs goal',\n}\n\nconst MISSED_COLOR = 'color-mix(in oklab, var(--color-chart-2) 30%, transparent)'\n\nfunction BarWithGoal({\n  x = 0,\n  y = 0,\n  width = 0,\n  height = 0,\n  payload,\n}: {\n  x?: number\n  y?: number\n  width?: number\n  height?: number\n  payload?: DailyGoal\n}) {\n  if (!payload) return null\n  const met = payload.value >= payload.goal\n  const base = y + height\n  const goalY = payload.value > 0 ? base - (height * payload.goal) / payload.value : base\n  const radius = Math.min(4, width / 2, height)\n\n  return (\n    <g>\n      <path\n        d={`M${x},${base}V${y + radius}a${radius},${radius} 0 0 1 ${radius},${-radius}H${x + width - radius}a${radius},${radius} 0 0 1 ${radius},${radius}V${base}Z`}\n        fill={met ? 'var(--color-chart-2)' : MISSED_COLOR}\n      />\n      <rect\n        x={x - 3}\n        y={goalY - 1}\n        width={width + 6}\n        height={2}\n        rx={1}\n        fill='var(--color-foreground)'\n      />\n    </g>\n  )\n}\n\nconst BarChartKPI2 = (props: BarChartKPI2Props) => {\n  const { data = [], format, height = 144, errorMessage, status, title } = props\n  const state = getBlockState(status, data)\n  const formatter = getKPIFormatter(format)\n  const total = data.reduce((sum, point) => sum + point.value, 0)\n  const goal = data.reduce((sum, point) => sum + point.goal, 0)\n  const share = goal > 0 ? total / goal : 0\n  const daysMet = data.filter((point) => point.value >= point.goal).length\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      <Card>\n        <CardContent>\n          <KPI>\n            <KPIContent className='gap-3'>\n              <div className='flex flex-col gap-1'>\n                <KPITitle>{title}</KPITitle>\n                <KPIValue value={total} format={format} animated />\n                {state === 'loading' ? (\n                  <BlockStateText className='text-sm' />\n                ) : state ? null : (\n                  <p className='text-muted-foreground text-sm'>\n                    <span\n                      className={cn(\n                        'font-medium tabular-nums',\n                        share >= 1\n                          ? 'text-emerald-700 dark:text-emerald-400'\n                          : 'text-orange-700 dark:text-orange-400',\n                      )}\n                    >\n                      {formatPercent(share, { maximumFractionDigits: 0 })} of goal\n                    </span>{' '}\n                    of <span className='tabular-nums'>{formatter(goal)}</span>\n                  </p>\n                )}\n              </div>\n              <KPIChart\n                label={`${title} by day: ${data\n                  .map(\n                    (point) =>\n                      `${point.label} ${formatter(point.value)} of ${formatter(point.goal)}`,\n                  )\n                  .join(', ')}`}\n              >\n                {state ? (\n                  <ChartPlaceholder height={height} />\n                ) : (\n                  <div className='w-full' style={{ height }}>\n                    <ResponsiveContainer width='100%' height='100%'>\n                      <BarChart\n                        accessibilityLayer={false}\n                        data={data}\n                        barCategoryGap='28%'\n                        margin={{ top: 4, right: 4, bottom: 0, left: 4 }}\n                      >\n                        <XAxis\n                          {...chartAxisProps}\n                          dataKey='label'\n                          tickFormatter={(value: string) => String(value).charAt(0)}\n                          interval={0}\n                        />\n                        <Tooltip\n                          content={({ active, payload }) => {\n                            const point = payload?.[0]?.payload as DailyGoal | undefined\n                            if (!active || !point) return null\n                            return (\n                              <ChartTooltipFrame>\n                                <span className='font-medium'>{point.label}</span>\n                                <ChartTooltipItem\n                                  color={\n                                    point.value >= point.goal\n                                      ? 'var(--color-chart-2)'\n                                      : MISSED_COLOR\n                                  }\n                                  name='Actual'\n                                  value={formatter(point.value)}\n                                />\n                                <ChartTooltipItem\n                                  color='var(--color-foreground)'\n                                  name='Goal'\n                                  value={formatter(point.goal)}\n                                />\n                              </ChartTooltipFrame>\n                            )\n                          }}\n                          cursor={chartBarCursor}\n                        />\n                        <YAxis\n                          hide\n                          domain={[\n                            0,\n                            Math.max(\n                              ...data.map((point) => Math.max(point.value, point.goal)),\n                            ),\n                          ]}\n                        />\n                        <Bar dataKey='value' name='Actual' shape={BarWithGoal} />\n                      </BarChart>\n                    </ResponsiveContainer>\n                  </div>\n                )}\n              </KPIChart>\n              <BlockReady className='text-xs'>\n                <p className='text-muted-foreground text-xs'>\n                  Met the goal on {daysMet} of {data.length} days. Solid bars met it; the\n                  line on each bar is that day&apos;s goal.\n                </p>\n              </BlockReady>\n            </KPIContent>\n          </KPI>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  BarChartKPI2,\n  exampleProps as barChartKpi2ExampleProps,\n  type BarChartKPI2Props,\n  type DailyGoal,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/kpi/bar-chart-kpi-02.tsx"
    }
  ],
  "meta": {
    "shows": "Each day's value with its goal marked on the bar, the share of the goal reached and how many days met it.",
    "size": "compact",
    "usedWith": [
      "goals-02",
      "progress-kpi-01",
      "chart-panel-01"
    ],
    "component": "BarChartKPI2",
    "props": "{ data?: { goal: number; label: string; value: number }[]; format?: \"number\" | \"compact\" | \"currency\" | \"currency-compact\" | \"percent\"; height?: number; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
