Chart Legend

Charts

Interactive chart legend with color toggles.

Preview

Revenue
Expenses
Profit

Usage

example.jsx
import { ChartLegend } from "@/components/ui/chart-legend";

export default function Example() {
  return <ChartLegend />;
}

Source Code

Copy this file into components/ui/chart-legend.jsx in your project.

chart-legend.jsx
import { forwardRef } from "react";
import { cn } from "@/lib/utils";

const ChartLegend = forwardRef(({ className, items = [], direction = "horizontal", ...props }, ref) => (
  <div ref={ref} className={cn("flex flex-wrap gap-4 text-sm", direction === "vertical" && "flex-col gap-2", className)} {...props}>
    {items.map((item, i) => (
      <div key={i} className="flex items-center gap-2">
        <span className="h-3 w-3 rounded-sm" style={{ backgroundColor: item.color }} />
        <span className="text-muted-foreground">{item.label}</span>
      </div>
    ))}
  </div>
));
ChartLegend.displayName = "ChartLegend";

export { ChartLegend };

Quick Install

Make sure you have the cn() utility set up. It requires clsx and tailwind-merge.

npm install clsx tailwind-merge