Charts and graphs · @jengacore/sparkline
Sparkline
Inline SVG trend line, no chart library: optional area and first-value baseline; decorative unless given a label.
- Transfers44
- Failures4
- Active agents128
Install
terminal
npx shadcn@latest add @jengacore/sparklineWithout the namespace configured: npx shadcn@latest add https://ui.jengacore.com/r/<new-york-v4|base-nova>/sparkline.json
Registry dependencies
Source
components/jc/sparkline.tsx
import * as React from "react"
import { cn } from "@/lib/utils"
/**
* An inline SVG sparkline: recent values, oldest first. No axes, no chart library. Decorative by default (the value
* next to it carries the meaning); pass `label` to give it a name of its own, such as "Last 12 weeks: 31 to 44".
*/
export function Sparkline({
values,
color = "var(--chart-2)",
area = false,
baseline = false,
label,
width = 120,
height = 32,
className,
}: {
values: readonly number[]
/** Any CSS colour; defaults to the first colour-blind-safe series colour. */
color?: string
/** Fill under the line at 15%. */
area?: boolean
/** Dashed line at the first value, to read the change at a glance. */
baseline?: boolean
label?: string
width?: number
height?: number
className?: string
}) {
const points = values.filter((v) => Number.isFinite(v))
if (points.length < 2) return null
const pad = 2
const min = Math.min(...points)
const max = Math.max(...points)
const y = (v: number) => (max === min ? height / 2 : height - pad - ((v - min) / (max - min)) * (height - pad * 2))
const step = width / (points.length - 1)
const coords = points.map((v, i) => [i * step, y(v)] as const)
const line = coords.map(([cx, cy], i) => `${i ? "L" : "M"}${cx.toFixed(1)} ${cy.toFixed(1)}`).join(" ")
const last = coords[coords.length - 1]
const first = points[0] ?? min
return (
<svg
data-slot="sparkline"
viewBox={`0 0 ${width} ${height}`}
preserveAspectRatio="none"
className={cn("block h-8 w-full overflow-visible", className)}
{...(label ? { role: "img", "aria-label": label } : { "aria-hidden": true })}
>
{baseline ? (
<path d={`M0 ${y(first).toFixed(1)} H${width}`} stroke="var(--border-strong, var(--border))" strokeWidth={1} strokeDasharray="2 3" fill="none" vectorEffect="non-scaling-stroke" />
) : null}
{area ? <path d={`${line} L${width} ${height} L0 ${height} Z`} fill={color} fillOpacity={0.15} stroke="none" /> : null}
<path d={line} stroke={color} strokeWidth={1.5} fill="none" strokeLinejoin="round" strokeLinecap="round" vectorEffect="non-scaling-stroke" />
{last ? <circle cx={last[0]} cy={last[1]} r={2.25} fill={color} /> : null}
</svg>
)
}