Veil UI
Components

Chart Legend

A standalone legend of labeled color swatches.

Installation

$ npx veil-ui@latest add chart-legend

Usage

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

<ChartLegend items={[{ label: "Revenue", color: "#6366f1" }, { label: "Costs" }]} />;

Examples

Basic

  • Revenue
  • Costs
  • Profit

Computing

  • Organic
  • Paid
  • Referral

API reference

The package exports chart-legend along with its composable parts. It forwards standard attributes to its root element; see the examples above for the component-specific props.

Accessibility

  • Charts render inside a container with an explicit height so they stay responsive without layout shift.
  • Color is never the only encoding; pair charts with labels, summaries or a data table for critical information.
  • Tooltips and legends are rendered as HTML, so their text is available to assistive technology.

Vue and plain HTML

This component also ships for Vue (Reka UI) and plain HTML. Install it with npx veil-ui add chart-legend --framework vue or switch the framework in components.json.

<script setup lang="ts">
import { cn } from "@/lib/utils";

import { chartColors } from "@/components/ui/chart";

interface ChartLegendItem {
  label: string;
  color?: string;
}

const props = withDefaults(
  defineProps<{
    items: ChartLegendItem[];
    label?: string;
    class?: string;
  }>(),
  { label: "Legend" },
);
</script>

<template>
  <ul
    data-slot="chart-legend"
    :aria-label="label"
    :class="cn('flex flex-wrap items-center gap-x-4 gap-y-1 text-xs', props.class)"
  >
    <li
      v-for="(item, index) in items"
      :key="item.label"
      class="flex items-center gap-1.5"
    >
      <span
        class="size-2 rounded-[2px]"
        :style="{ backgroundColor: item.color ?? chartColors[index % chartColors.length] }"
        aria-hidden="true"
      />
      <span class="text-muted-foreground">{{ item.label }}</span>
    </li>
  </ul>
</template>

On this page