components/tanstack-charts/bar/diverging-likert.tsx
import { type ChartPoint, barX, defineChart, ruleX, stack } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { scaleBand, scaleLinear } from "d3-scale";
import { MessageSquare } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface LikertDatum {
Question: string;
Response: string;
count: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const responseCategories = ["Disagree", "Neutral", "Agree"];
function generateDemoData(): LikertDatum[] {
const questions = ["Usability", "Performance", "Documentation", "Support"];
const results: LikertDatum[] = [];
questions.forEach((q, idx) => {
results.push({ Question: q, Response: "Disagree", count: 15 + idx * 5 });
results.push({ Question: q, Response: "Neutral", count: 25 + idx * 3 });
results.push({ Question: q, Response: "Agree", count: 60 - idx * 8 });
});
return results;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as LikertDatum;
if (!d) return { title: "", rows: [] };
return {
title: d.Question,
rows: [
{
label: d.Response,
value: `${d.count} responses`,
color: points[0].color || "var(--chart-1)",
},
],
};
}
export function DivergingLikert() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
barX(data, {
id: "likert-bars",
x: "count",
y: "Question",
z: "Response",
color: "Response",
layout: stack({
order: responseCategories,
anchor: { series: "Neutral", fraction: 0.5 },
}),
inset: 1,
radius: 3,
}),
ruleX([0], { stroke: "var(--border)" }),
],
scales: {
x: {
scale: scaleLinear,
grid: true,
axis: {
line: false,
label: "← Disagree · Neutral · Agree →",
ticks: {
size: 0,
format: (v: number) => `${Math.abs(v)}`,
},
},
},
y: {
scale: () => scaleBand<string>().paddingInner(0.2),
axis: {
line: false,
ticks: { size: 0 },
},
},
},
color: {
domain: responseCategories,
range: ["var(--chart-5)", "var(--chart-3)", "var(--chart-1)"],
},
margin: { top: 24, right: 24, bottom: 32, left: 90 },
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
focus: "group-y",
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "group-center",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[data],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>User Satisfaction Survey</CardTitle>
<CardDescription>Diverging Likert scale bars anchored around neutral survey sentiment</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="User Satisfaction Survey Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Usability receives highest positive agree ratio (60%){" "}
<MessageSquare className="h-4 w-4 text-blue-500" />
</div>
<div className="text-muted-foreground leading-none">
Positive sentiment extends to the right; negative sentiment diverges to the left
</div>
</CardFooter>
</Card>
);
}
export default DivergingLikert;