{
  "title": "Diverging Likert",
  "description": "Diverging Likert scale bars anchored around neutral survey sentiment.",
  "name": "tanstack-charts-diverging-likert",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/bar/diverging-likert.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, barX, defineChart, ruleX, stack } 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 { scaleBand, scaleLinear } from \"d3-scale\";\nimport { MessageSquare } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface LikertDatum {\n    Question: string;\n    Response: string;\n    count: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nconst responseCategories = [\"Disagree\", \"Neutral\", \"Agree\"];\n\nfunction generateDemoData(): LikertDatum[] {\n    const questions = [\"Usability\", \"Performance\", \"Documentation\", \"Support\"];\n    const results: LikertDatum[] = [];\n\n    questions.forEach((q, idx) => {\n        results.push({ Question: q, Response: \"Disagree\", count: 15 + idx * 5 });\n        results.push({ Question: q, Response: \"Neutral\", count: 25 + idx * 3 });\n        results.push({ Question: q, Response: \"Agree\", count: 60 - idx * 8 });\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 LikertDatum;\n    if (!d) return { title: \"\", rows: [] };\n    return {\n        title: d.Question,\n        rows: [\n            {\n                label: d.Response,\n                value: `${d.count} responses`,\n                color: points[0].color || \"var(--chart-1)\",\n            },\n        ],\n    };\n}\n\nexport function DivergingLikert() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        barX(data, {\n                            id: \"likert-bars\",\n                            x: \"count\",\n                            y: \"Question\",\n                            z: \"Response\",\n                            color: \"Response\",\n                            layout: stack({\n                                order: responseCategories,\n                                anchor: { series: \"Neutral\", fraction: 0.5 },\n                            }),\n                            inset: 1,\n                            radius: 3,\n                        }),\n                        ruleX([0], { stroke: \"var(--border)\" }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleLinear,\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"← Disagree · Neutral · Agree →\",\n                                ticks: {\n                                    size: 0,\n                                    format: (v: number) => `${Math.abs(v)}`,\n                                },\n                            },\n                        },\n                        y: {\n                            scale: () => scaleBand<string>().paddingInner(0.2),\n                            axis: {\n                                line: false,\n                                ticks: { size: 0 },\n                            },\n                        },\n                    },\n                    color: {\n                        domain: responseCategories,\n                        range: [\"var(--chart-5)\", \"var(--chart-3)\", \"var(--chart-1)\"],\n                    },\n                    margin: { top: 24, right: 24, bottom: 32, left: 90 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    focus: \"group-y\",\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>User Satisfaction Survey</CardTitle>\n                <CardDescription>Diverging Likert scale bars anchored around neutral survey sentiment</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=\"User Satisfaction Survey 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                    Usability receives highest positive agree ratio (60%){\" \"}\n                    <MessageSquare className=\"h-4 w-4 text-blue-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Positive sentiment extends to the right; negative sentiment diverges to the left\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default DivergingLikert;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Diverging Likert scale bars anchored around neutral survey sentiment. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack"
    ]
  }
}