Skip to content
caliper ui docs

Components

Line chart

One or more series over time, with hover readout and an optional band.

Free
pnpm dlx shadcn@latest add @caliper/line-chart

Halden revenue

Revenue against costs, thousands USD

Revenue and costs
Revenue and costs
PointRevenueCostsEvent
Aug 34836
Aug 105238
Aug 174737
Aug 245840
Aug 316141
Sep 75542
Sep 146643
Sep 216345
Sep 287144
Oct 56847
Oct 127748
Oct 198249
Installs components/ui/line-chart.tsxDepends on d3-shapeMotion none in component source

When to use

Use it for

  • Trends of a few series.
  • Comparing a metric against a target range.

Skip it when

  • More than five series; the lines blur together.

Installation

terminal
pnpm dlx shadcn@latest add @caliper/line-chart

Adds its 3 files to components/ui/, each with its own stylesheet, and installs their packages. No account needed. The first part also adds Caliper UI’s tokens; import them once as in Installation.

Usage

This is the code behind the preview above.

example.tsx
import { useState, type ReactNode } from "react"
import { LineChart } from "@/components/ui/line-chart"
import { SegmentedControl } from "@/components/ui/segmented-control"
import { cn } from "@/components/ui/lib/cn"

function Panel({ title, context, action, className, children }: { title: string; context: string; action?: ReactNode; className?: string; children: ReactNode }) {
  return (
    <section className={cn("grid w-full max-w-md min-w-0 gap-5 rounded-overlay border border-border bg-background p-6 shadow-resting", className)}>
      <div className="flex flex-wrap items-start justify-between gap-3">
        <div className="grid min-w-0 gap-1">
          <p className="font-medium">{title}</p>
          <p className="text-sm text-muted-foreground">{context}</p>
        </div>
        {action}
      </div>
      {children}
    </section>
  );
}

const periods = [
  { value: "week", label: "Week" },
  { value: "month", label: "Month" },
  { value: "year", label: "Year" },
];

type Period = "week" | "month" | "year";

function ChartHead({ title, period, onPeriod }: { title?: string; period: Period; onPeriod: (period: Period) => void }) {
  const control = <SegmentedControl label="Period" value={period} onValueChange={(value) => onPeriod(value as Period)} options={periods} />;
  // Beside the chart's own title where there is room; above it on phones, so the two never overlap.
  if (!title) return <div className="mb-3 flex justify-end sm:absolute sm:-top-2 sm:right-0 sm:z-10 sm:mb-0">{control}</div>;
  return (
    <div className="mb-4 flex items-center justify-between gap-3">
      <p className="text-sm font-medium">{title}</p>
      {control}
    </div>
  );
}

const lineLabels: Record<Period, string[]> = {
  week: ["Aug 3", "Aug 10", "Aug 17", "Aug 24", "Aug 31", "Sep 7", "Sep 14", "Sep 21", "Sep 28", "Oct 5", "Oct 12", "Oct 19"],
  month: ["Nov", "Dec", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct"],
  year: ["2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023", "2024", "2025", "2026"],
};

const lineValues: Record<Period, { revenue: number[]; costs: number[] }> = {
  week: { revenue: [48, 52, 47, 58, 61, 55, 66, 63, 71, 68, 77, 82], costs: [36, 38, 37, 40, 41, 42, 43, 45, 44, 47, 48, 49] },
  month: { revenue: [210, 248, 226, 241, 262, 255, 284, 279, 302, 296, 318, 334], costs: [168, 176, 182, 179, 188, 194, 191, 203, 207, 212, 216, 221] },
  year: { revenue: [420, 610, 780, 1040, 1210, 1180, 1690, 2240, 2610, 2980, 3420, 3760], costs: [510, 640, 720, 860, 980, 1060, 1240, 1520, 1760, 1940, 2210, 2430] },
};

const lineFor = (period: Period) =>
  lineLabels[period].map((label, index) => ({
    id: label,
    label,
    values: { revenue: lineValues[period].revenue[index]!, costs: lineValues[period].costs[index]! },
  }));

const lineSeries = [
  { id: "revenue", label: "Revenue" },
  { id: "costs", label: "Costs" },
];

function RevenueLines() {
  const [period, setPeriod] = useState<Period>("week");
  return (
    <div className="relative w-full max-w-xl">
      <ChartHead period={period} onPeriod={setPeriod} />
      <LineChart label="Revenue and costs" data={lineFor(period)} series={lineSeries} unit="k" />
    </div>
  );
}

export function RevenueLinesCard() {
  return (
    <Panel title="Halden revenue" context="Revenue against costs, thousands USD" className="max-w-xl">
      <RevenueLines />
    </Panel>
  );
}

API reference

LineChart

component
PropTypeDefault
label *string—
data *readonly ChartDatum[]—
series *readonly ChartSeries[]—
unitstring""
loadingboolean—
errorstring—
classNamestring—
band{ lower: string; upper: string; }—

Accessibility

It exposes slider semantics, with aria-label, aria-busy, aria-hidden, aria-valuemin, aria-valuemax and aria-valuenow kept in step with state.

No custom key handling; Tab and the native activation keys do the work.

Motion

It declares no animation in its component source. Any transition comes from its stylesheet, which uses the duration tokens that collapse to instant under prefers-reduced-motion.

All Core charts examples