Skip to content
Charts

Area Chart

Recharts 3 area chart with gradient fills, a themed tooltip, and a tremor-style data API.

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 { AreaChart } from "kineto/area-chart";

Preview

Income vs Expense

tsx
"use client";

import * as React from "react";
import { useReducedMotion } from "motion/react";
import {
  Area,
  AreaChart as RechartsAreaChart,
  CartesianGrid,
  Legend,
  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 AreaChartProps 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;
}

export const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>(
  (
    {
      className,
      data,
      index,
      categories,
      colors = DEFAULT_COLORS,
      height = 240,
      valueFormat = (n) => n.toLocaleString(),
      showGrid = true,
      showLegend = false,
      showYAxis = false,
      ...props
    },
    ref
  ) => {
    const reduced = useReducedMotion();
    const uid = React.useId().replace(/:/g, "");

    return (
      <div ref={ref} className={cn("w-full", className)} style={{ height }} {...props}>
        <ResponsiveContainer width="100%" height="100%">
          <RechartsAreaChart data={data} margin={{ top: 8, right: 8, bottom: 0, left: 0 }}>
            <defs>
              {categories.map((cat, i) => {
                const c = color(colors[i % colors.length] ?? "--chart-1");
                return (
                  <linearGradient key={cat} id={`${uid}-${cat}`} x1="0" y1="0" x2="0" y2="1">
                    <stop offset="0%" stopColor={c} stopOpacity={0.3} />
                    <stop offset="100%" stopColor={c} stopOpacity={0} />
                  </linearGradient>
                );
              })}
            </defs>
            {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) => {
              const c = color(colors[i % colors.length] ?? "--chart-1");
              return (
                <Area
                  key={cat}
                  type="monotone"
                  dataKey={cat}
                  stroke={c}
                  strokeWidth={2.5}
                  fill={`url(#${uid}-${cat})`}
                  isAnimationActive={!reduced}
                  animationDuration={900}
                  dot={false}
                  activeDot={{ r: 4, strokeWidth: 2, stroke: "hsl(var(--card))" }}
                />
              );
            })}
          </RechartsAreaChart>
        </ResponsiveContainer>
      </div>
    );
  }
);
AreaChart.displayName = "AreaChart";

src/components/charts/area-chart.tsx

Props

PropTypeDefaultDescription
dataRecord<string, string | number>[]—Row objects.
indexstring—Key used for the x-axis.
categoriesstring[]—Keys to plot as areas.
colorsstring[]—CSS var names, e.g. ["--chart-1"].