Veil UI
Components

Sparkline

A compact inline trend line with an optional area fill.

Installation

$ npx veil-ui@latest add sparkline

Usage

import { Sparkline } from "@/components/ui/sparkline";

<Sparkline data={[12, 18, 14, 22, 20, 27]} label="Weekly trend" />;

Examples

Basic

Area

API reference

The package exports sparkline 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 sparkline --framework vue or switch the framework in components.json.

<script setup lang="ts">
import { computed } from "vue";

import { cn } from "@/lib/utils";

const props = withDefaults(
  defineProps<{
    data: number[];
    width?: number;
    height?: number;
    color?: string;
    label?: string;
    class?: string;
    showArea?: boolean;
  }>(),
  {
    width: 120,
    height: 32,
    color: "currentColor",
    label: "Trend sparkline",
    showArea: false,
  },
);

const isEmpty = computed(() => props.data.length === 0);

const chart = computed(() => {
  if (props.data.length === 0) {
    return { line: "", area: "" };
  }

  const min = Math.min(...props.data);
  const max = Math.max(...props.data);
  const range = max - min || 1;
  const step = props.data.length > 1 ? props.width / (props.data.length - 1) : 0;

  const points = props.data.map((value, index) => {
    const x = index * step;
    const y = props.height - 2 - ((value - min) / range) * (props.height - 4);
    return `${x},${y}`;
  });

  const line = `M ${points.join(" L ")}`;
  const area = `${line} L ${props.width},${props.height} L 0,${props.height} Z`;

  return { line, area };
});
</script>

<template>
  <span v-if="isEmpty" :class="cn('text-xs text-muted-foreground', props.class)">
    No data
  </span>
  <svg
    v-else
    role="img"
    :aria-label="label"
    :viewBox="`0 0 ${width} ${height}`"
    :class="cn('h-8 w-auto', props.class)"
    :style="{ color }"
  >
    <path
      v-if="showArea"
      :d="chart.area"
      fill="currentColor"
      fill-opacity="0.15"
      stroke="none"
    />
    <path
      :d="chart.line"
      fill="none"
      stroke="currentColor"
      :stroke-width="1.5"
      stroke-linecap="round"
      stroke-linejoin="round"
    />
  </svg>
</template>

On this page