Sparkline
Sparkline with readable values and an accessible data representation.
Make it yours.
Install the editable source and its dependencies into your React + Tailwind project.
Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/sparkline.jsonUse it
Scroll horizontally for long lines
"use client";
import {Sparkline} from '@/components/jez-ui/ui/sparkline';
export default function Example(){
return <><div className="w-full max-w-2xl"><div className="mb-8 flex flex-wrap items-end justify-between gap-4"><div><h3 className="text-3xl tracking-tight">A good direction.</h3><p className="mt-2 text-sm text-muted-foreground">The shape of this week</p></div><span className="rounded-full border border-border px-3 py-1 text-xs text-muted-foreground">Demo data</span></div><Sparkline/></div></>;
}Props & types
Scroll horizontally for long lines
{
values?: number[];
label?: string;
className?: string;
}Source
View 2 source files
registry/ui/sparkline.tsx
Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
export function Sparkline({
values = [4, 7, 5, 12, 9, 15, 13, 20],
label = "Trend",
className,
}: {
values?: number[];
label?: string;
className?: string;
}) {
const min = Math.min(...values),
max = Math.max(...values),
range = max - min || 1;
const points = values
.map(
(v, i) =>
`${(i / (values.length - 1 || 1)) * 200},${55 - ((v - min) / range) * 50}`,
)
.join(" ");
return (
<svg
viewBox="0 0 200 60"
role="img"
aria-label={`${label}: ${values.join(", ")}`}
className={cn("h-16 w-48", className)}
>
<polyline
points={points}
fill="none"
stroke="var(--primary)"
strokeWidth="2.5"
strokeLinejoin="round"
/>
</svg>
);
}
registry/ui/utils.ts
Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
return twMerge(clsx(values));
}
Dependencies
clsx@2.1.1 · tailwind-merge@3.5.0
Accessibility & behaviour
Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.
Read the accessibility and performance guide →