Skip to content
← Components

Area Chart

Area 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/area-chart.json

Use it

Scroll horizontally for long lines
"use client";
import { AreaChart } from "@/components/jez-ui/ui/area-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">The bigger picture.</h3>
            <p className="mt-2 text-sm text-muted-foreground">
              Audience over the last seven days
            </p>
          </div>
          <span className="rounded-full border border-border px-3 py-1 text-xs text-muted-foreground">
            Demo data
          </span>
        </div>
        <AreaChart />
      </div>
    </>
  );
}

Props & types

Scroll horizontally for long lines
export type ChartProps<E extends React.ElementType = "figure"> =
  React.ComponentProps<E> & {
    data?: ChartDatum[];
    label?: string;
    className?: string;
    color?: string;
  };

ChartProps

Source

View 3 source files

registry/ui/area-chart.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { cn } from "./utils";
import {
  ResponsiveContainer,
  AreaChart as Plot,
  Area,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
} from "recharts";
import { ChartFrame, sampleChartData, type ChartProps } from "./chart-frame";
export function AreaChart({
  data = sampleChartData,
  label = "Area chart",
  className,
  color = "var(--primary)",
  children,
  ...rootProps
}: ChartProps) {
  return (
    <ChartFrame
      {...rootProps}
      data={data}
      label={label}
      className={cn("", className)}
    >
      {children !== undefined ? (
        children
      ) : (
        <>
          <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)",
                }}
              />
              <Area
                dataKey="value"
                fill={color}
                stroke={color}
                type="monotone"
                fillOpacity={0.14}
                strokeWidth={2}
                isAnimationActive={false}
              />
            </Plot>
          </ResponsiveContainer>
        </>
      )}
    </ChartFrame>
  );
}

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/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<E extends React.ElementType = "figure"> =
  React.ComponentProps<E> & {
    data?: ChartDatum[];
    label?: string;
    className?: string;
    color?: string;
  };
export function ChartFrame({
  children,
  data,
  label,
  className,
  contentProps,
  captionProps,
  ...rootProps
}: React.ComponentProps<"figure"> & {
  contentProps?: React.ComponentProps<"div">;
  captionProps?: React.ComponentProps<"figcaption">;
  children: React.ReactNode;
  data: ChartDatum[];
  label: string;
  className?: string;
}) {
  return (
    <figure {...rootProps} className={cn("m-0 w-full min-w-0", className)}>
      <ChartCaption {...captionProps}>{label}</ChartCaption>
      <ChartContent {...contentProps}>{children}</ChartContent>
      <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>
  );
}

export function ChartCaption({
  className,
  ...props
}: React.ComponentProps<"figcaption">) {
  return (
    <figcaption
      className={cn("mb-4 text-sm font-medium", className)}
      {...props}
    />
  );
}
export function ChartContent({
  className,
  ...props
}: React.ComponentProps<"div">) {
  return (
    <div
      className={cn("h-60 w-full min-w-0 overflow-hidden", className)}
      {...props}
    />
  );
}

Dependencies

recharts@3.8.0 · clsx@2.1.1 · tailwind-merge@3.5.0

Accessibility & behaviour

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

Read the accessibility and performance guide →