glasscn
ShowcaseDocsComponentsStudioThemingNative
5Get started
Guides
IntroductionInstallationThemingFonts & typeStudioReact Native
Surfaces
AuroraBackdropGlassLiquid glassCardTheme scopeBackground extensionScroll areaAccordionCollapsibleAlertItemEmpty
Typography
TypographyProse
Controls
ButtonBadgeInputTextareaLabelSwitchCheckboxSegmented controlTabsGlass groupGlass clusterGlass morphSliderProgressKbdSeparatorAvatarGlass preferencesComboboxCalendarDate pickerSpinnerSkeletonSelectRadio groupFieldInput groupButton groupToggleToggle groupStepper
Overlays
DialogAlert dialogSheetAction sheetPopoverTooltipDropdown menuToasterCommandDrawerContext menuConfirmation popover
Navigation
DockToolbarScroll edgeNavigation barSidebarSearch fieldGrouped list
Data
Activity ringsProgress ringSparklineStatKPIWidgetRange chartBar listGaugeTrackerCategory barHeatmapChartArea chartBar chartLine chartDonut chartTableData table
Chat
ChatChat markdownChat parts
Blocks
More Apple glass examplesLiquid Glass examplesDashboardAnalyticsSettingsSign inChat
glasscn — a shadcn registry. Copy it, own it, change it.DocsThemingregistry.json
Data@tanstack/charts

Area chart

Volume over time: gradient areas under a line per series — stacked (diverging for negatives) or 100% — with scrubbing, selection and a grouped glass tooltip.

Installation

pnpm dlx shadcn@latest add https://glasscn.app/r/area-chart.json

React Native (iOS, Android and web through react-native-web) — same parts and props:

pnpm dlx shadcn@latest add https://glasscn.app/r/native-area-chart.json

Or with the namespace set up: shadcn add @glasscn-app/area-chart

Props

PropTypeDefault
data / x / series / ariaLabelas LineChart

Your rows, an x accessor, and one area per series.

—
stackedboolean | "expand"

Pile the layers into a total (negatives stack down from zero); "expand" shows each layer's share. The tooltip lists layers top-down with their own values.

false
latestPointboolean

A dot on each series' last value.

false
curve"smooth" | "linear" | "step""smooth"
onScrub(row: Row | null) => void

As LineChart.

—
axis"leading" | "trailing"

The value axis side — trailing, as Apple's charts (flips in RTL).

"trailing"
selectedX / onSelectx value | null / (row: Row | null) => void

Controlled selection: a click, tap or Enter on a point reports its row; selectedX marks it.

—
state / onRetry / stateLabels"ready" | "loading" | "empty" | "error"

A skeleton, "No data for this range" or an error with a retry, the plot's height.

"ready" ("empty" when data is empty)
accessibilityTableboolean | "visible"

A ChartSummary sentence and ChartDataTable for screen readers (sr-only), shown with "visible", omitted with false.

true
valueFormat / xFormat(v) => string

Axis ticks and tooltip.

—
height / legend / gridnumber / boolean / boolean240 / series > 1 / true

Source

components/glass/area-chart.tsx
"use client"

import * as React from "react"
import { cn } from "@/lib/glass-cn"
import { areaY, defineChart, lineY, ruleX } from "@tanstack/charts"
import { crosshair } from "@tanstack/charts/crosshair"
import { decorative } from "@tanstack/charts/mark/decorative"
import { tooltip } from "@tanstack/charts/tooltip"

import {
  ChartAlternative,
  ChartContainer,
  ChartLegend,
  categoryAxisStyle,
  chartColor,
  seriesTooltip,
  useAxisSide,
  useChartAccessors,
  valueAxis,
  xScaleFor,
  type ChartAccessibility,
  type ChartSeries,
  type ChartStateLabels,
  type ChartStateValue,
} from "@/components/glass/chart"
import { monotoneCurve, sanitizeId, stackEdges, stepCurve } from "@/lib/glass-charts"

type XValue = string | number | Date

/**
 * Volume over an ordered x: each series a soft gradient that fades to the
 * baseline under a line. `stacked` piles the layers into a total — the tooltip
 * still reads each layer's own value from your row, top layer first. `stacked="expand"`
 * shows each layer's share (0–1) of the total. Mixed signs stack apart: positives
 * up from zero, negatives down.
 */
function AreaChart<Row>({
  data,
  x: xProp,
  series: seriesProp,
  ariaLabel,
  height = 240,
  stacked = false,
  valueFormat: valueFormatProp,
  xFormat: xFormatProp,
  legend = seriesProp.length > 1,
  grid = true,
  curve = "smooth",
  axis = "trailing",
  latestPoint = false,
  selectedX,
  onSelect,
  onScrub,
  state,
  onRetry,
  stateLabels,
  accessibilityTable = true,
  className,
  ...props
}: Omit<React.ComponentProps<"div">, "children" | "onSelect"> & {
  data: readonly Row[]
  x: (d: Row) => XValue
  series: readonly ChartSeries<Row>[]
  ariaLabel: string
  height?: number
  stacked?: boolean | "expand"
  valueFormat?: (value: number) => string
  xFormat?: (value: XValue) => string
  legend?: boolean
  grid?: boolean
  curve?: "smooth" | "linear" | "step"
  /** Which side the value axis sits on. Apple's charts put it trailing. */
  axis?: "leading" | "trailing"
  /** A dot on each series' last value. */
  latestPoint?: boolean
  /** Marks this x with a rule — controlled selection. */
  selectedX?: XValue | null
  /** A click, tap or Enter on a point selects its row; null clears. */
  onSelect?: (row: Row | null) => void
  /** The row under the pointer or keyboard focus while scrubbing; null when it leaves. */
  onScrub?: (row: Row | null) => void
  state?: ChartStateValue
  onRetry?: () => void
  stateLabels?: ChartStateLabels
  /** Exact values for assistive tech: true (sr-only, default), "visible", or false. */
  accessibilityTable?: ChartAccessibility
}) {
  const ref = React.useRef<HTMLDivElement>(null)
  const side = useAxisSide(ref, axis)
  const { x, series, valueFormat, xFormat, key } = useChartAccessors({ x: xProp, series: seriesProp, valueFormat: valueFormatProp, xFormat: xFormatProp })
  const selectedKey = selectedX == null ? null : selectedX instanceof Date ? selectedX.getTime() : selectedX
  const selectedIsDate = selectedX instanceof Date
  // unique per chart, so two area charts on a page never share gradients
  const uid = sanitizeId(React.useId())
  const definition = React.useMemo(() => {
    const colors = series.map((s, i) => s.color ?? chartColor(i))
    const path = curve === "smooth" ? monotoneCurve : curve === "step" ? stepCurve : undefined
    // the bottom and top of layer i at row d: 0 → value, or the diverging running totals when stacked
    const edges = (d: Row, i: number) => {
      const values = series.map((s) => s.value(d))
      if (stacked === "expand") {
        const total = values.reduce<number>((sum, v) => sum + Math.abs(v ?? 0), 0) || 1
        return stackEdges(values.map((v) => (v == null ? v : Math.abs(v) / total)), i)
      }
      return stackEdges(values, i)
    }
    const bottom = (i: number) => (d: Row) => (stacked ? edges(d, i)[0] : 0)
    const top = (i: number) => (d: Row) => {
      const v = series[i].value(d)
      return v == null ? null : stacked ? edges(d, i)[1] : v
    }
    const lastRow = data[data.length - 1]
    const selected = selectedKey == null ? [] : [selectedIsDate ? new Date(selectedKey) : selectedKey]
    return defineChart({
      marks: [
        ...series.map((s, i) =>
          areaY(data, {
            id: s.id,
            x,
            y1: bottom(i),
            y2: top(i),
            z: () => s.id,
            color: () => s.id,
            fill: `url(#area-${uid}-${i})`,
            fillOpacity: 1,
            curve: path,
          })
        ),
        // the line rides the top edge; the area owns focus so the tooltip reads one point per series
        ...series.map((s, i) => decorative(lineY(data, { id: `${s.id}-line`, x, y: top(i), z: () => s.id, color: () => s.id, strokeWidth: 2, curve: path }))),
        ...(latestPoint && lastRow
          ? series.map((s, i) => decorative(lineY([lastRow], { id: `${s.id}-latest`, x, y: top(i), z: () => s.id, color: () => s.id, points: true, strokeWidth: 0 })))
          : []),
        ...(selected.length ? [ruleX(selected, { id: "selected", stroke: "var(--foreground)", strokeOpacity: 0.4, strokeWidth: 1 })] : []),
        crosshair({ x: true, y: false }),
      ],
      scales: {
        x: { scale: xScaleFor(data.map(x), "point"), axis: categoryAxisStyle(xFormat) },
        y: valueAxis({ grid, valueFormat: stacked === "expand" ? (valueFormat ?? ((v: number) => `${Math.round(v * 100)}%`)) : valueFormat, side }),
      },
      color: { domain: series.map((s) => s.id), range: colors },
      gradients: colors.map((color, i) => ({
        id: `area-${uid}-${i}`,
        x1: 0,
        y1: 0,
        x2: 0,
        y2: 1,
        stops: [
          { offset: 0, color, opacity: stacked ? 0.5 : 0.34 },
          { offset: 1, color, opacity: stacked ? 0.12 : 0.02 },
        ],
      })),
      focus: "group-x",
      maxFocusDistance: Number.POSITIVE_INFINITY,
      tooltip: {
        use: tooltip,
        anchor: "group-center",
        sort: stacked ? undefined : "color-domain",
        placement: ["top", "right", "left", "bottom"],
        // a stack reads top-down, as it's drawn
        content: seriesTooltip(series, x, { xFormat, valueFormat, reverse: Boolean(stacked) }),
      },
    })
    // `key` stands for the accessors' structure; they call your latest functions
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [uid, data, key, stacked, grid, curve, side, latestPoint, selectedKey, selectedIsDate])

  return (
    <div ref={ref} data-slot="area-chart" className={cn("grid w-full gap-3", className)} {...props}>
      <ChartContainer
        definition={definition}
        ariaLabel={ariaLabel}
        height={height}
        idPrefix={`area-chart-${uid}`}
        state={state ?? (data.length ? "ready" : "empty")}
        onRetry={onRetry}
        stateLabels={stateLabels}
        onSelect={onSelect ? (p) => onSelect(p ? (p.datum as Row) : null) : undefined}
        onFocusChange={onScrub ? (p) => onScrub(p ? (p.datum as Row) : null) : undefined}
      />
      {legend ? <ChartLegend items={series.map((s, i) => ({ label: s.label, color: s.color ?? chartColor(i) }))} /> : null}
      <ChartAlternative mode={accessibilityTable} data={data} x={x} series={series} xFormat={xFormat} valueFormat={valueFormat} label={ariaLabel} />
    </div>
  )
}

export { AreaChart }
ChartBar chart
Sleep stages
Hours per night, last 10 nights
Sep 29Sep 30Oct 1Oct 2Oct 3Oct 4Oct 5Oct 6Oct 7Oct 80.0h2.0h4.0h6.0h8.0h
  • Deep
  • REM
  • Light

Deep: 10 values, low 0.9h, high 1.6h, latest 1.5h, trending up. REM: 10 values, low 1.2h, high 2.1h, latest 2.0h, trending up. Light: 10 values, low 3.4h, high 4.4h, latest 4.1h, trending up.

Hours of deep, REM and light sleep per night over the last 10 nights, stacked
DeepREMLight
Sep 291.2h1.6h3.9h
Sep 301.0h1.4h3.6h
Oct 11.4h1.8h4.1h
Oct 21.3h1.9h4.3h
Oct 30.9h1.2h3.4h
Oct 41.5h2.0h4.4h
Oct 51.6h2.1h4.2h
Oct 61.3h1.7h3.8h
Oct 71.4h1.9h4.0h
Oct 81.5h2.0h4.1h