{
  "title": "Active Bar Visitor Dashboard",
  "description": "Interactive tabbed bar dashboard switching desktop and mobile traffic.",
  "name": "tanstack-charts-active-bar-dashboard",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card",
    "chart"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/bar/active-bar-dashboard.tsx",
      "type": "registry:block",
      "content": "import { barY, crosshair, defineChart } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo, useState } from \"react\";\n\nimport { scaleBand, scaleLinear } from \"d3-scale\";\n\nimport { Card, CardContent, CardDescription, CardHeader, CardTitle } from \"@/components/ui/card\";\nimport type { ChartConfig } from \"@/components/ui/chart\";\n\ninterface DashboardRow {\n    id: string;\n    day: string;\n    desktop: number;\n    mobile: number;\n}\n\ntype MetricType = \"desktop\" | \"mobile\";\n\nconst chartConfig = {\n    desktop: {\n        label: \"Desktop\",\n        color: \"var(--chart-1)\",\n    },\n    mobile: {\n        label: \"Mobile\",\n        color: \"var(--chart-2)\",\n    },\n} satisfies ChartConfig;\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): DashboardRow[] {\n    const results: DashboardRow[] = [];\n    for (let i = 1; i <= 30; i++) {\n        const id = i < 10 ? `0${i}` : `${i}`;\n        results.push({\n            id,\n            day: `Jun ${i}`,\n            desktop: Math.floor(120 + Math.sin(i * 0.4) * 80 + Math.random() * 40),\n            mobile: Math.floor(90 + Math.cos(i * 0.3) * 60 + Math.random() * 30),\n        });\n    }\n    return results;\n}\n\nexport function ActiveBarDashboard() {\n    const [activeTab, setActiveTab] = useState<MetricType>(\"desktop\");\n    const data = useMemo(() => generateDemoData(), []);\n\n    const totals = useMemo(() => {\n        const totalDesktop = data.reduce((acc, curr) => acc + curr.desktop, 0);\n        const totalMobile = data.reduce((acc, curr) => acc + curr.mobile, 0);\n        return {\n            desktop: totalDesktop,\n            mobile: totalMobile,\n        };\n    }, [data]);\n\n    const yMax = useMemo(() => {\n        const max = Math.max(...data.map((d) => d[activeTab]));\n        return Math.ceil((max * 1.15) / 50) * 50;\n    }, [data, activeTab]);\n\n    const tickIds = [\"01\", \"07\", \"14\", \"21\", \"28\"];\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    motion: {\n                        transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n                    },\n                    marks: [\n                        barY(data, {\n                            id: \"daily-visitors\",\n                            x: \"id\",\n                            y: activeTab,\n                            key: \"id\",\n                            fill: \"url(#visitor-bars-gradient)\",\n                            radius: 4,\n                        }),\n                        crosshair<string, number>({\n                            id: \"active-bar-ring\",\n                            x: {\n                                band: {\n                                    fill: \"transparent\",\n                                    stroke: chartConfig[activeTab].color,\n                                    strokeOpacity: 0.9,\n                                    strokeWidth: 1.5,\n                                    radius: 5,\n                                },\n                            },\n                            y: false,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleBand<string>()\n                                .domain(data.map((d) => d.id))\n                                .paddingInner(0.2),\n                            axis: {\n                                line: false,\n                                ticks: {\n                                    values: tickIds,\n                                    format: (v: string) => data.find((d) => d.id === v)?.day ?? \"\",\n                                },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([0, yMax]).nice(),\n                            grid: true,\n                            axis: false,\n                        },\n                    },\n                    gradients: [\n                        {\n                            id: \"visitor-bars-gradient\",\n                            x1: 0,\n                            y1: 1,\n                            x2: 0,\n                            y2: 0,\n                            stops: [\n                                { offset: 0, color: chartConfig[activeTab].color, opacity: 0.35 },\n                                { offset: 1, color: chartConfig[activeTab].color, opacity: 0.95 },\n                            ],\n                        },\n                    ],\n                    margin: { top: 20, right: 12, bottom: 32, left: 12 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    keyboard: true,\n                    focus: \"group-x\",\n                    tooltip: {\n                        use: tooltip,\n                        motion: { type: \"spring\", stiffness: 170, damping: 18 },\n                        className: \"font-sans! shadow-none! capitalize!\",\n                        content: (points) => {\n                            const datum = points[0]?.datum as DashboardRow;\n                            if (!datum) return { rows: [] };\n                            return {\n                                title: datum.day,\n                                rows: [\n                                    {\n                                        label: chartConfig[activeTab].label,\n                                        value: `${datum[activeTab].toLocaleString(\"en-US\")} visitors`,\n                                        color: chartConfig[activeTab].color,\n                                    },\n                                ],\n                            };\n                        },\n                    },\n                },\n            ),\n        [data, activeTab, yMax],\n    );\n\n    return (\n        <Card className=\"gap-0 py-4 sm:py-0\">\n            <CardHeader className=\"flex flex-col items-stretch border-b p-0! sm:flex-row\">\n                {/* Title Context */}\n                <div className=\"flex flex-1 flex-col justify-center gap-1 px-6 pt-4 pb-3 sm:pt-6 sm:pb-6\">\n                    <CardTitle className=\"text-lg\">Active Bar Visitor Dashboard</CardTitle>\n                    <CardDescription>\n                        Interactive daily visitor traffic with focus ring cursor (Last 30 Days)\n                    </CardDescription>\n                </div>\n\n                {/* Tab Controls */}\n                <div className=\"flex\">\n                    {([\"desktop\", \"mobile\"] as const).map((key) => {\n                        return (\n                            <button\n                                key={key}\n                                data-active={activeTab === key}\n                                className=\"data-[active=true]:bg-muted/50 hover:bg-muted/30 flex flex-1 flex-col justify-center gap-1 border-t px-6 py-4 text-left transition-colors even:border-l sm:border-t-0 sm:border-l sm:px-8 sm:py-6\"\n                                onClick={() => setActiveTab(key)}>\n                                <span className=\"text-muted-foreground text-xs tracking-wider uppercase\">\n                                    {chartConfig[key].label}\n                                </span>\n                                <span className=\"text-lg leading-none font-bold sm:text-2xl\">\n                                    {totals[key].toLocaleString(\"en-US\")}\n                                </span>\n                            </button>\n                        );\n                    })}\n                </div>\n            </CardHeader>\n\n            <CardContent className=\"px-2 pt-4 sm:p-6 sm:pt-8\">\n                {/* Dynamic Legend Indicator */}\n                <div className=\"text-muted-foreground mb-4 flex items-center justify-end gap-2 text-xs font-medium\">\n                    <span\n                        className=\"h-2.5 w-2.5 rounded-full transition-colors duration-300\"\n                        style={{ backgroundColor: chartConfig[activeTab].color }}\n                    />\n                    <span>{chartConfig[activeTab].label} Visitors</span>\n                </div>\n\n                <div className=\"h-[280px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={280}\n                        ariaLabel={`Active bar visitor dashboard chart displaying ${activeTab} data`}\n                    />\n                </div>\n            </CardContent>\n        </Card>\n    );\n}\n\nexport default ActiveBarDashboard;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Interactive tabbed bar dashboard switching desktop and mobile traffic. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "bar"
    ]
  }
}