Chart Legend
Toggleable legend shared by Chart, Donut and Radar — clickable entries hide or restore series, with line and square swatches
ChartLegend is the legend used by Chart, Donut and Radar, shipped as a standalone component so custom charts can share it. Each entry is a button: clicking hides or restores the series at that index, and hovering highlights it. Entries for hidden series dim to 40% opacity.
Swatches come in two shapes — "line" for series-style charts (stroke segments) and "square" for categorical charts (pie/donut slices).
Installation
- $npx litefy@latest add chart-legend
- $pnpm dlx litefy@latest add chart-legend
- $yarn dlx litefy@latest add chart-legend
- $bun --bun litefy@latest add chart-legend
Usage
Hidden: none
"use client";
import { useState } from "react";
import { ChartLegend } from "@/ui";
const series = [
{ label: "Revenue", color: "var(--color-primary)" },
{ label: "Cost", color: "var(--color-info)" },
{ label: "Margin", color: "var(--color-success)" },
];
export default function Demo() {
const [hidden, setHidden] = useState<ReadonlySet<number>>(new Set());
const toggle = (index: number) =>
setHidden((prev) => {
const next = new Set(prev);
if (next.has(index)) next.delete(index);
else next.add(index);
return next;
});
const hiddenLabels = series.filter((_, i) => hidden.has(i)).map((s) => s.label);
return (
<div className="flex flex-col gap-6">
<ChartLegend
swatch="line"
items={series}
hidden={hidden}
onToggle={toggle}
className="justify-center"
/>
<ChartLegend
swatch="square"
items={series}
hidden={hidden}
onToggle={toggle}
className="justify-center"
/>
<p className="text-center text-sm text-muted-foreground">
Hidden: {hiddenLabels.length > 0 ? hiddenLabels.join(", ") : "none"}
</p>
</div>
);
}
Colors are plain CSS color strings — pass semantic variables such as var(--color-primary) so entries follow the theme, or the same resolved colors your chart uses (ChartSeriesConfig.stroke falls back to the useChartPalette palette by index). The component is purely presentational: state lives with you, which is why hidden is controlled and there is no built-in layout above or below your chart.
API Reference
ChartLegend
| Prop | Type | Default | Description |
|---|---|---|---|
items | { label: string; color: string }[] | - (required) | One entry per series, in the same order as the chart's series |
hidden | ReadonlySet<number> | - | Indices currently hidden; matching entries render dimmed |
swatch | "line" | "square" | "line" | Swatch shape — line segments for series, squares for categorical data |
onToggle | (index: number) => void | - | Called when an entry is clicked |
onHover | (index: number | null) => void | - | Called on entry mouse enter with the index, and mouse leave with null |
className | ClassNameValue | - | Merged onto the container; add justify-center to center the row |
style | React.CSSProperties | - | Inline style on the container |
Chart
A self-built canvas time-series chart engine sharing the chart-kit math layer — automatic canvas theming, responsive width, interactive legend and hover tooltip
Chat Input
A chat composer with pasted-screenshot thumbnails, a capability-toggle slot and an icon-only send button — mobile-ready out of the box.