{
  "title": "Linear Regression",
  "description": "Scatterplot with fitted linear regression trend line.",
  "name": "tanstack-charts-linear-regression",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/line/linear-regression.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot, linearRegressionY } 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 } from \"d3-scale\";\nimport { TrendingDown } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface CarDatum {\n    name: string;\n    hp: number;\n    mpg: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): CarDatum[] {\n    const results: CarDatum[] = [];\n    for (let i = 0; i < 40; i++) {\n        const hp = 50 + i * 4 + Math.sin(i) * 10;\n        const mpg = 45 - hp * 0.15 + Math.cos(i * 1.2) * 4;\n        results.push({\n            name: `Model-${i + 1}`,\n            hp: Math.round(hp),\n            mpg: Math.max(10, Number(mpg.toFixed(1))),\n        });\n    }\n    return results;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const p = points[0];\n    const d = p.datum as any;\n    if (!d) return { title: \"\", rows: [] };\n    if (\"name\" in d) {\n        return {\n            title: d.name,\n            rows: [\n                { label: \"Power\", value: `${d.hp} hp`, color: \"var(--chart-1)\" },\n                { label: \"Fuel Economy\", value: `${d.mpg} mpg`, color: \"var(--foreground)\" },\n            ],\n        };\n    }\n    return {\n        title: \"Regression Line Trend\",\n        rows: [{ label: \"Predicted MPG\", value: `${Number(d.y).toFixed(1)} mpg`, color: \"var(--chart-2)\" }],\n    };\n}\n\nexport function LinearRegressionChart() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        dot(data, {\n                            id: \"car-dots\",\n                            x: \"hp\",\n                            y: \"mpg\",\n                            fill: \"var(--chart-1)\",\n                            r: 3.5,\n                        }),\n                        linearRegressionY(data, {\n                            id: \"regression-line\",\n                            x: \"hp\",\n                            y: \"mpg\",\n                            ci: 0,\n                            stroke: \"var(--chart-2)\",\n                            strokeWidth: 2.25,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleLinear().domain([40, 220]),\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"Engine Power (hp)\",\n                                ticks: { size: 0 },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([5, 50]),\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"Fuel Economy (mpg)\",\n                                ticks: { size: 0 },\n                            },\n                        },\n                    },\n                    margin: { top: 24, right: 24, bottom: 32, left: 45 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    focus: \"group-x\",\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: \"group-center\",\n                        placement: \"auto\",\n                        content: (points) => shadcnTooltipContent(points),\n                    },\n                },\n            ),\n        [data],\n    );\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Horsepower vs Fuel Economy</CardTitle>\n                <CardDescription>Scatterplot with fitted linear regression trend line</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=\"Horsepower vs Fuel Economy 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                    Strong negative correlation observed between horsepower and MPG{\" \"}\n                    <TrendingDown className=\"h-4 w-4 text-rose-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Red line denotes ordinary least squares linear fit estimate\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default LinearRegressionChart;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Scatterplot with fitted linear regression trend line. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "line"
    ]
  }
}