Skip to content
Charts

Line Chart

Recharts 3 line chart with smooth curves, active dots, and a themed tooltip.

MotionRecharts draw-in animation; disabled under reduced motion

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { LineChart } from "kineto/line-chart";

Preview

Monthly trend

tsx
"use client";

import * as React from "react";
import { useReducedMotion } from "motion/react";
import {
  CartesianGrid,
  Legend,
  Line,
  LineChart as RechartsLineChart,
  ResponsiveContainer,
  Tooltip,
  XAxis,
  YAxis,
} from "recharts";

import { cn } from "@/lib/cn";
import { ChartTooltip } from "@/components/charts/chart-tooltip";

const DEFAULT_COLORS = ["--chart-1", "--chart-2", "--chart-3", "--chart-4", "--chart-5"];
const color = (v: string) => `hsl(var(${v}))`;

export interface LineChartProps extends React.HTMLAttributes<HTMLDivElement> {
  data: Array<Record<string, string | number>>;
  index: string;
  categories: string[];
  colors?: string[];
  height?: number;
  valueFormat?: (value: number) => string;
  showGrid?: boolean;
  showLegend?: boolean;
  showYAxis?: boolean;
  curved?: boolean;
}

export const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>(
  (
    {
      className,
      data,
      index,
      categories,
      colors = DEFAULT_COLORS,
      height = 240,
      valueFormat = (n) => n.toLocaleString(),
      showGrid = true,
      showLegend = false,
      showYAxis = false,
      curved = true,
      ...props
    },
    ref
  ) => {
    const reduced = useReducedMotion();

    return (
      <div ref={ref} className={cn("w-full", className)} style={{ height }} {...props}>
        <ResponsiveContainer width="100%" height="100%">
          <RechartsLineChart data={data} margin={{ top: 8, right: 8, bottom: 0, left: 0 }}>
            {showGrid ? (
              <CartesianGrid vertical={false} stroke="hsl(var(--border))" strokeDasharray="3 6" />
            ) : null}
            <XAxis
              dataKey={index}
              tickLine={false}
              axisLine={false}
              tick={{ fill: "hsl(var(--muted-foreground))", fontSize: 12 }}
              dy={8}
            />
            {showYAxis ? (
              <YAxis
                tickLine={false}
                axisLine={false}
                width={40}
                tick={{ fill: "hsl(var(--muted-foreground))", fontSize: 12 }}
                tickFormatter={(v) => valueFormat(Number(v))}
              />
            ) : null}
            <Tooltip
              cursor={{ stroke: "hsl(var(--muted-foreground))", strokeDasharray: "3 4" }}
              content={<ChartTooltip valueFormat={valueFormat} />}
            />
            {showLegend ? <Legend iconType="circle" /> : null}
            {categories.map((cat, i) => (
              <Line
                key={cat}
                type={curved ? "monotone" : "linear"}
                dataKey={cat}
                stroke={color(colors[i % colors.length] ?? "--chart-1")}
                strokeWidth={2.5}
                dot={false}
                activeDot={{ r: 4, strokeWidth: 2, stroke: "hsl(var(--card))" }}
                isAnimationActive={!reduced}
                animationDuration={900}
              />
            ))}
          </RechartsLineChart>
        </ResponsiveContainer>
      </div>
    );
  }
);
LineChart.displayName = "LineChart";

src/components/charts/line-chart.tsx

Props

PropTypeDefaultDescription
dataRecord<string, string | number>[]—Row objects.
indexstring—Key used for the x-axis.
categoriesstring[]—Keys to plot as lines.
curvedbooleantrueMonotone vs. straight segments.