{
  "title": "Bump Ranking",
  "description": "Curve-bump ranking lines tracking position changes over time.",
  "name": "tanstack-charts-bump-ranking",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale",
    "d3-shape"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/bump-ranking.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, d3Curve, defineChart, dot, lineY, rank, select, text } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo } from \"react\";\n\nimport { scaleLinear, scaleUtc } from \"d3-scale\";\nimport { curveBumpX } from \"d3-shape\";\nimport { Trophy } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface BumpDatum {\n    industry: string;\n    date: Date;\n    unemployed: number;\n    rank?: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nconst industriesList = [\"Trade\", \"Manufacturing\", \"Services\", \"Tech\", \"Construction\"];\n\nfunction generateDemoData(): BumpDatum[] {\n    const results: BumpDatum[] = [];\n    const years = [2020, 2021, 2022, 2023, 2024, 2025];\n\n    years.forEach((year) => {\n        industriesList.forEach((ind, idx) => {\n            const unemployed = 100 + Math.floor(Math.sin(year * 0.4 + idx) * 80 + Math.random() * 40);\n            results.push({\n                industry: ind,\n                date: new Date(Date.UTC(year, 0, 1)),\n                unemployed,\n            });\n        });\n    });\n\n    return results;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const d = points[0].datum as BumpDatum;\n    if (!d || !d.date) return { title: \"\", rows: [] };\n    const dateObj = d.date instanceof Date ? d.date : new Date(d.date);\n    const yearStr = isNaN(dateObj.getTime()) ? \"\" : ` (${dateObj.getUTCFullYear()})`;\n    return {\n        title: `${d.industry}${yearStr}`,\n        rows: [\n            { label: \"Rank\", value: `#${d.rank}`, color: \"var(--chart-1)\" },\n            { label: \"Unemployed Count\", value: `${d.unemployed}k`, color: \"var(--foreground)\" },\n        ],\n    };\n}\n\nexport function BumpRanking() {\n    const rawData = useMemo(() => generateDemoData(), []);\n\n    const rows = useMemo(() => {\n        return rank(rawData, {\n            by: \"date\",\n            value: \"unemployed\",\n            order: \"descending\",\n        });\n    }, [rawData]);\n\n    const labels = useMemo(() => {\n        return select(rows, {\n            by: \"industry\",\n            value: (datum: BumpDatum) =>\n                datum?.date ? (datum.date instanceof Date ? datum.date.getTime() : new Date(datum.date).getTime()) : 0,\n            select: \"max\",\n        });\n    }, [rows]);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        lineY(rows, {\n                            id: \"industry-ranks\",\n                            x: \"date\",\n                            y: \"rank\",\n                            color: \"industry\",\n                            curve: d3Curve(curveBumpX),\n                            strokeWidth: 2.5,\n                        }),\n                        dot(rows, {\n                            id: \"industry-rank-points\",\n                            x: \"date\",\n                            y: \"rank\",\n                            color: \"industry\",\n                            r: 4,\n                        }),\n                        text(labels, {\n                            id: \"industry-labels\",\n                            x: \"date\",\n                            y: \"rank\",\n                            text: \"industry\",\n                            color: \"industry\",\n                            anchor: \"start\",\n                            dx: 8,\n                            fontSize: 10,\n                            fontWeight: 600,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleUtc,\n                            axis: {\n                                ticks: {\n                                    count: 6,\n                                    format: (d: any) => {\n                                        const date = d instanceof Date ? d : new Date(d);\n                                        return isNaN(date.getTime()) ? \"\" : `${date.getUTCFullYear()}`;\n                                    },\n                                },\n                                label: \"Year\",\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([5.5, 0.5]),\n                            grid: true,\n                            axis: {\n                                ticks: { count: 5, format: (v: number) => `#${v}` },\n                                label: \"Ranking Position\",\n                            },\n                        },\n                    },\n                    color: {\n                        domain: industriesList,\n                        range: [\n                            \"var(--chart-1)\",\n                            \"var(--chart-2)\",\n                            \"var(--chart-3)\",\n                            \"var(--chart-4)\",\n                            \"var(--chart-5)\",\n                        ],\n                    },\n                    margin: { top: 24, right: 80, bottom: 32, left: 45 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    keyboard: true,\n                    tooltip: {\n                        use: tooltip,\n                        className:\n                            \"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs\",\n                        anchor: \"pointer\",\n                        placement: \"auto\",\n                        content: (points) => shadcnTooltipContent(points),\n                    },\n                },\n            ),\n        [rows, labels],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Industry Unemployment Bump Chart</CardTitle>\n                <CardDescription>\n                    Curve-bump ranking lines tracking position shifts across annual observations\n                </CardDescription>\n            </CardHeader>\n            <CardContent>\n                <div className=\"h-[250px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={250}\n                        ariaLabel=\"Industry Unemployment Bump Chart\"\n                    />\n                </div>\n            </CardContent>\n            <CardFooter className=\"flex-col items-start gap-2 text-sm\">\n                <div className=\"flex items-center gap-2 leading-none font-medium\">\n                    Trade maintains top rank across recent observation years{\" \"}\n                    <Trophy className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Smooth bump curves illustrate ordinal rank swaps over time\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default BumpRanking;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Curve-bump ranking lines tracking position changes over time. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}