
A bar chart displaying positive and negative data values built with TanStack Charts.
components/tanstack-charts/bar/bar-chart-negative.tsx
import { type ChartPoint, barY, defineChart, text } 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 { TrendingUp } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface MonthDatum {
month: string;
desktop: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): MonthDatum[] {
return [
{ month: "January", desktop: 186 },
{ month: "February", desktop: 205 },
{ month: "March", desktop: -207 },
{ month: "April", desktop: 173 },
{ month: "May", desktop: -209 },
{ month: "June", desktop: 214 },
];
}
function titleCase(value: string) {
return value.charAt(0).toUpperCase() + value.slice(1);
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
return {
title: String(points[0]?.xValue ?? ""),
rows: points.map((point) => ({
label: titleCase(
String(point.group ?? point.markId.replace(/-?(bars|lines|areas|slices|values|radar)$/u, "")),
),
value: Number(point.yValue ?? point.xValue ?? 0).toLocaleString("en-US"),
color: point.color,
})),
};
}
export function BarChartNegative() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
motion: {
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
},
marks: [
barY(data, {
id: "visitor-bars",
x: "month",
y: "desktop",
fill: (row: MonthDatum) => (row.desktop < 0 ? "var(--chart-2)" : "var(--chart-1)"),
radius: 0,
}),
text(data, {
id: "visitor-labels",
x: "month",
y: "desktop",
text: (row: MonthDatum) => row.month,
dy: (row: MonthDatum) => (row.desktop < 0 ? 14 : -10),
fill: "var(--chart-1)",
fontSize: 12,
}),
],
scales: {
x: {
scale: () => scaleBand<string>().paddingInner(0.2).paddingOuter(0.1),
axis: false,
},
y: { scale: scaleLinear, grid: true, axis: false },
},
margin: { top: 24, right: 5, bottom: 24, left: 5 },
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
focus: "group-x",
tooltip: {
use: tooltip,
motion: { type: "spring", stiffness: 170, damping: 18 },
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "group-center",
placement: "auto",
sort: "color-domain",
content: (points) => shadcnTooltipContent(points),
},
},
),
[data],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Net Cash Flow Balance</CardTitle>
<CardDescription>Monthly revenue inflows vs operational expenses</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Net Cash Flow Balance Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Positive operating balance up by 5.2% this month <TrendingUp className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">Showing total visitors for the last 6 months</div>
</CardFooter>
</Card>
);
}
export default BarChartNegative;