Skip to content
Components

Live Line Chart

Live Line Chart with readable values and an accessible data representation.

Open ↗

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/live-line-chart.json

Use it

Scroll horizontally for long lines
"use client";
import {LiveLineChart} from '@/components/jez-ui/ui/live-line-chart';

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">In the moment.</h3><p className="mt-2 text-sm text-muted-foreground">A simulated feed, updating live</p></div><span className="rounded-full border border-border px-3 py-1 text-xs text-muted-foreground">Demo data</span></div><LiveLineChart/></div></>;
}

Props & types

Scroll horizontally for long lines
{ className?: string }

Source

View 5 source files

registry/ui/live-line-chart.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
import { LineChart } from "./line-chart";
import { sampleChartData } from "./chart-frame";
import { Button } from "./button";
export function LiveLineChart({ className }: { className?: string }) {
  const [data, setData] = React.useState(sampleChartData);
  const [running, setRunning] = React.useState(false);
  const tick = React.useRef(0);
  React.useEffect(() => {
    if (!running) return;
    const t = setInterval(() => {
      if (document.hidden) return;
      tick.current++;
      setData((d) => [
        ...d.slice(-11),
        {
          name: String(tick.current),
          value: 50 + Math.round(Math.sin(tick.current * 0.8) * 25),
        },
      ]);
    }, 1000);
    return () => clearInterval(t);
  }, [running]);
  return (
    <div className={cn("grid gap-3", className)}>
      <LineChart data={data} label="Live signal · simulated" />
      <Button variant="outline" onClick={() => setRunning((v) => !v)}>
        {running ? "Pause" : "Start"} stream
      </Button>
    </div>
  );
}

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));
}

registry/ui/line-chart.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
import {
  ResponsiveContainer,
  LineChart as Plot,
  Line,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
} from "recharts";
import { ChartFrame, sampleChartData, type ChartProps } from "./chart-frame";
export function LineChart({
  data = sampleChartData,
  label = "Line chart",
  className,
  color = "var(--primary)",
}: ChartProps) {
  return (
    <ChartFrame data={data} label={label} className={cn("", className)}>
      <ResponsiveContainer
        initialDimension={{ width: 600, height: 240 }}
        width="100%"
        height="100%"
      >
        <Plot
          data={data}
          margin={{ top: 8, right: 12, left: -24, bottom: 0 }}
          accessibilityLayer
        >
          <CartesianGrid vertical={false} stroke="var(--border)" />
          <XAxis
            dataKey="name"
            tickLine={false}
            axisLine={false}
            tick={{ fill: "var(--muted-foreground)", fontSize: 11 }}
          />
          <YAxis
            tickLine={false}
            axisLine={false}
            tick={{ fill: "var(--muted-foreground)", fontSize: 11 }}
          />
          <Tooltip
            contentStyle={{
              background: "var(--background)",
              border: "1px solid var(--border)",
              borderRadius: 8,
              color: "var(--foreground)",
            }}
          />
          <Line
            dataKey="value"
            fill={color}
            stroke={color}
            type="monotone"
            strokeWidth={2.5}
            dot={false}
            isAnimationActive={false}
          />
        </Plot>
      </ResponsiveContainer>
    </ChartFrame>
  );
}

registry/ui/chart-frame.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
export type ChartDatum = { name: string; value: number; previous?: number };
export const sampleChartData: ChartDatum[] = [
  { name: "Mon", value: 32, previous: 24 },
  { name: "Tue", value: 48, previous: 31 },
  { name: "Wed", value: 39, previous: 36 },
  { name: "Thu", value: 65, previous: 42 },
  { name: "Fri", value: 57, previous: 38 },
  { name: "Sat", value: 82, previous: 49 },
  { name: "Sun", value: 73, previous: 51 },
];
export type ChartProps = {
  data?: ChartDatum[];
  label?: string;
  className?: string;
  color?: string;
};
export function ChartFrame({
  children,
  data,
  label,
  className,
}: {
  children: React.ReactNode;
  data: ChartDatum[];
  label: string;
  className?: string;
}) {
  return (
    <figure className={cn("m-0 w-full min-w-0", className)}>
      <figcaption className="mb-4 text-sm font-medium">{label}</figcaption>
      <div className="h-60 w-full min-w-0 overflow-hidden">{children}</div>
      <details className="mt-3 text-xs text-muted-foreground">
        <summary>View data table</summary>
        <table className="mt-3 w-full text-left">
          <caption className="sr-only">{label}</caption>
          <thead>
            <tr>
              <th>Period</th>
              <th>Value</th>
              <th>Previous</th>
            </tr>
          </thead>
          <tbody>
            {data.map((d, i) => (
              <tr key={i}>
                <th className="py-1 font-normal">{d.name}</th>
                <td>{d.value}</td>
                <td>{d.previous ?? "—"}</td>
              </tr>
            ))}
          </tbody>
        </table>
      </details>
    </figure>
  );
}

registry/ui/button.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { Slot } from "radix-ui";
import { LoaderCircle } from "lucide-react";
import { cn } from "./utils";
export type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> & {
  variant?: "primary" | "secondary" | "outline" | "ghost" | "danger";
  size?: "sm" | "md" | "lg";
  loading?: boolean;
  asChild?: boolean;
};
export function Button({
  variant = "primary",
  size = "md",
  loading,
  asChild,
  className,
  children,
  disabled,
  ...props
}: ButtonProps) {
  const Comp = asChild ? Slot.Root : "button";
  return (
    <Comp
      type={asChild ? undefined : "button"}
      disabled={asChild ? undefined : disabled || loading}
      aria-disabled={disabled || loading || undefined}
      aria-busy={loading || undefined}
      className={cn(
        "relative inline-flex shrink-0 items-center justify-center gap-2 whitespace-nowrap rounded-lg border border-transparent text-sm font-medium leading-none transition-[background-color,border-color,box-shadow,transform] duration-150 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary active:scale-[.98] disabled:pointer-events-none disabled:opacity-45 aria-disabled:pointer-events-none [&_svg]:shrink-0",
        {
          "bg-primary text-primary-foreground shadow-[inset_0_1px_0_#ffffff26,0_1px_2px_#00000012] hover:bg-primary/90":
            variant === "primary",
          "border-border/60 bg-muted text-foreground hover:bg-muted/70":
            variant === "secondary",
          "border-border bg-background text-foreground shadow-sm hover:bg-muted/60":
            variant === "outline",
          "text-muted-foreground hover:bg-muted hover:text-foreground":
            variant === "ghost",
          "bg-danger text-danger-foreground hover:bg-danger/90":
            variant === "danger",
        },
        {
          "h-8 px-3 text-xs": size === "sm",
          "h-10 px-4": size === "md",
          "h-12 px-6": size === "lg",
        },
        className,
      )}
      {...props}
    >
      {asChild ? (
        children
      ) : (
        <>
          <span
            className={cn(
              "inline-flex min-w-0 flex-1 items-center justify-center gap-2",
              loading && "invisible",
            )}
          >
            {children}
          </span>
          {loading && (
            <LoaderCircle
              aria-hidden="true"
              className="absolute size-4 animate-spin motion-reduce:animate-none"
            />
          )}
        </>
      )}
    </Comp>
  );
}

Dependencies

clsx@2.1.1 · tailwind-merge@3.5.0 · recharts@3.8.0 · radix-ui@1.6.7 · lucide-react@0.577.0

Accessibility & behaviour

Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.

Read the accessibility and performance guide →