# Line Chart (/ui/components/react/line-chart)

A line chart compares how 1 or more measures move over time. Like every chart here it is a recipe: [Recharts](https://recharts.github.io) draws it and the [chart layer](/ui/components/react/charts) themes it. Each example below is a complete file to paste and adapt.

## Installation [#installation]

Recharts is the only extra dependency, with `react-is` at the same version as `react`:

```bash
pnpm add recharts react-is   # or: npm install recharts react-is / yarn add recharts react-is / bun add recharts react-is
```

The examples import the chart layer from `@/components/chart`. If your project doesn't have it yet, copy it from the [Charts](/ui/components/react/charts#the-chart-layer) page first.

## Examples [#examples]

### Default [#default]

A 2 px line with no dots at rest, and Sparkline's indicator on the hovered point. The dates on the axis and in the tooltip go through `Intl.DateTimeFormat`, and `minTickGap` drops the ticks that would crowd.

```tsx
'use client'

import { CartesianGrid, Line, LineChart, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'

const data = [
  { date: '2026-03-02', orders: 212 },
  { date: '2026-03-03', orders: 238 },
  { date: '2026-03-04', orders: 225 },
  { date: '2026-03-05', orders: 261 },
  { date: '2026-03-06', orders: 304 },
  { date: '2026-03-07', orders: 352 },
  { date: '2026-03-08', orders: 331 },
  { date: '2026-03-09', orders: 246 },
  { date: '2026-03-10', orders: 268 },
  { date: '2026-03-11', orders: 259 },
  { date: '2026-03-12', orders: 297 },
  { date: '2026-03-13', orders: 343 },
  { date: '2026-03-14', orders: 389 },
  { date: '2026-03-15', orders: 372 },
]

const count = new Intl.NumberFormat('en-US')
const day = new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric', timeZone: 'UTC' })
const fullDay = new Intl.DateTimeFormat('en-US', { weekday: 'long', month: 'long', day: 'numeric', timeZone: 'UTC' })

export default function LineChartDefault() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart>
        <LineChart responsive data={data} aria-label="Orders per day over 2 weeks">
          <CartesianGrid vertical={false} />
          <XAxis
            dataKey="date"
            tickFormatter={(value) => day.format(new Date(value))}
            tickLine={false}
            axisLine={false}
            minTickGap={32}
            interval="equidistantPreserveEnd"
          />
          <YAxis
            tickFormatter={(value) => count.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
            niceTicks="snap125"
          />
          <Tooltip
            content={
              <ChartTooltip
                formatLabel={(label) => fullDay.format(new Date(label))}
                formatValue={(value) => count.format(value)}
              />
            }
          />
          <Line
            name="Orders"
            dataKey="orders"
            type="monotone"
            stroke={chartColors[0]}
            dot={false}
            activeDot={chartMarks.activeDot}
            {...motion.series}
          />
        </LineChart>
      </Chart>
    </div>
  )
}
```

### Multiple [#multiple]

Each series takes the next color of `chartColors` as its `stroke`, in order. With 2 or more lines the chart needs a `Legend`.

```tsx
'use client'

import { CartesianGrid, Legend, Line, LineChart, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartLegend, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'

const data = [
  { month: 'Jan', desktop: 18600, mobile: 12400, tablet: 4200 },
  { month: 'Feb', desktop: 19800, mobile: 14100, tablet: 4500 },
  { month: 'Mar', desktop: 18900, mobile: 15900, tablet: 4100 },
  { month: 'Apr', desktop: 21400, mobile: 17300, tablet: 4800 },
  { month: 'May', desktop: 22700, mobile: 19800, tablet: 5200 },
  { month: 'Jun', desktop: 21900, mobile: 21600, tablet: 4900 },
  { month: 'Jul', desktop: 23800, mobile: 24300, tablet: 5600 },
  { month: 'Aug', desktop: 23100, mobile: 26100, tablet: 5300 },
  { month: 'Sep', desktop: 25200, mobile: 27900, tablet: 6100 },
  { month: 'Oct', desktop: 26800, mobile: 30400, tablet: 6500 },
  { month: 'Nov', desktop: 26100, mobile: 32800, tablet: 6200 },
  { month: 'Dec', desktop: 28300, mobile: 36200, tablet: 7000 },
]

const compact = new Intl.NumberFormat('en-US', { notation: 'compact' })
const count = new Intl.NumberFormat('en-US')

export default function LineChartMultiple() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart className="h-72">
        <LineChart responsive data={data} aria-label="Sessions by device, by month">
          <CartesianGrid vertical={false} />
          <XAxis dataKey="month" tickLine={false} axisLine={false} minTickGap={24} interval="equidistantPreserveEnd" />
          <YAxis
            tickFormatter={(value) => compact.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
            niceTicks="snap125"
          />
          <Tooltip content={<ChartTooltip formatValue={(value) => count.format(value)} />} />
          <Legend itemSorter={null} content={<ChartLegend />} />
          <Line
            name="Desktop"
            dataKey="desktop"
            type="monotone"
            stroke={chartColors[0]}
            dot={false}
            activeDot={chartMarks.activeDot}
            {...motion.series}
          />
          <Line
            name="Mobile"
            dataKey="mobile"
            type="monotone"
            stroke={chartColors[1]}
            dot={false}
            activeDot={chartMarks.activeDot}
            {...motion.series}
          />
          <Line
            name="Tablet"
            dataKey="tablet"
            type="monotone"
            stroke={chartColors[2]}
            dot={false}
            activeDot={chartMarks.activeDot}
            {...motion.series}
          />
        </LineChart>
      </Chart>
    </div>
  )
}
```

### Dots [#dots]

For a few points far apart, such as quarters, show them: a `dot` filled with the surface color and straight segments (`type="linear"`), so nothing is implied between 2 measurements.

```tsx
'use client'

import { CartesianGrid, Line, LineChart, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'

const data = [
  { quarter: 'Q1 2025', score: 41 },
  { quarter: 'Q2 2025', score: 46 },
  { quarter: 'Q3 2025', score: 44 },
  { quarter: 'Q4 2025', score: 52 },
  { quarter: 'Q1 2026', score: 57 },
  { quarter: 'Q2 2026', score: 55 },
  { quarter: 'Q3 2026', score: 63 },
  { quarter: 'Q4 2026', score: 68 },
]

const count = new Intl.NumberFormat('en-US')

export default function LineChartDots() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart>
        <LineChart
          responsive
          data={data}
          margin={{ top: 8, right: 12, left: 12 }}
          aria-label="Net promoter score by quarter"
        >
          <CartesianGrid vertical={false} />
          <XAxis
            dataKey="quarter"
            tickLine={false}
            axisLine={false}
            minTickGap={24}
            interval="equidistantPreserveEnd"
          />
          <YAxis
            domain={[0, 100]}
            tickFormatter={(value) => count.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
            niceTicks="snap125"
          />
          <Tooltip content={<ChartTooltip formatValue={(value) => count.format(value)} />} />
          <Line
            name="Net promoter score"
            dataKey="score"
            type="linear"
            stroke={chartColors[0]}
            dot={{ r: 4, fill: 'var(--background)' }}
            activeDot={chartMarks.activeDot}
            {...motion.series}
          />
        </LineChart>
      </Chart>
    </div>
  )
}
```

### Reference line [#reference-line]

A target or a baseline is a `ReferenceLine`. The theme draws it in `--foreground-subtle`, the neutral color, and a dash sets it apart from the data.

```tsx
'use client'

import { CartesianGrid, Line, LineChart, ReferenceLine, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'

const data = [
  { month: 'Jan', signups: 640 },
  { month: 'Feb', signups: 710 },
  { month: 'Mar', signups: 690 },
  { month: 'Apr', signups: 820 },
  { month: 'May', signups: 910 },
  { month: 'Jun', signups: 880 },
  { month: 'Jul', signups: 1040 },
  { month: 'Aug', signups: 990 },
  { month: 'Sep', signups: 1120 },
  { month: 'Oct', signups: 1260 },
  { month: 'Nov', signups: 1190 },
  { month: 'Dec', signups: 1340 },
]

const target = 1000

const count = new Intl.NumberFormat('en-US')

export default function LineChartReferenceLine() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart>
        <LineChart responsive data={data} aria-label="Signups by month against a target of 1,000">
          <CartesianGrid vertical={false} />
          <XAxis dataKey="month" tickLine={false} axisLine={false} minTickGap={24} interval="equidistantPreserveEnd" />
          <YAxis
            tickFormatter={(value) => count.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
            niceTicks="snap125"
          />
          <Tooltip content={<ChartTooltip formatValue={(value) => count.format(value)} />} />
          <ReferenceLine
            y={target}
            strokeDasharray="4 4"
            label={{ value: `Target ${count.format(target)}`, position: 'insideBottomRight' }}
          />
          <Line
            name="Signups"
            dataKey="signups"
            type="monotone"
            stroke={chartColors[0]}
            dot={false}
            activeDot={chartMarks.activeDot}
            {...motion.series}
          />
        </LineChart>
      </Chart>
    </div>
  )
}
```

### Interactive [#interactive]

Give the `Legend` an `onClick` and `ChartLegend` renders each item as a toggle button. The state lives in your component: pass it to each line as `hide`, and Recharts rescales the axis to the lines that remain.

```tsx
'use client'

import { useState } from 'react'
import { CartesianGrid, Legend, Line, LineChart, Tooltip, XAxis, YAxis } from 'recharts'
import { Chart, ChartLegend, ChartTooltip, chartColors, chartMarks, useChartMotion } from '@/components/chart'

const data = [
  { month: 'Jan', email: 4200, search: 6800, social: 2100, referral: 1300 },
  { month: 'Feb', email: 4600, search: 7200, social: 2600, referral: 1500 },
  { month: 'Mar', email: 4400, search: 7900, social: 3400, referral: 1400 },
  { month: 'Apr', email: 5100, search: 8300, social: 3900, referral: 1800 },
  { month: 'May', email: 5600, search: 9100, social: 5200, referral: 2100 },
  { month: 'Jun', email: 5300, search: 9600, social: 6100, referral: 1900 },
  { month: 'Jul', email: 6200, search: 10400, social: 7400, referral: 2400 },
  { month: 'Aug', email: 5900, search: 11200, social: 8900, referral: 2200 },
  { month: 'Sep', email: 6800, search: 11900, social: 10600, referral: 2700 },
  { month: 'Oct', email: 7400, search: 12800, social: 12300, referral: 3100 },
  { month: 'Nov', email: 7100, search: 13500, social: 14800, referral: 2900 },
  { month: 'Dec', email: 8000, search: 14600, social: 17200, referral: 3400 },
]

const series = [
  { key: 'search', name: 'Search', color: chartColors[0] },
  { key: 'social', name: 'Social', color: chartColors[1] },
  { key: 'email', name: 'Email', color: chartColors[2] },
  { key: 'referral', name: 'Referral', color: chartColors[3] },
]

const compact = new Intl.NumberFormat('en-US', { notation: 'compact' })
const count = new Intl.NumberFormat('en-US')

export default function LineChartInteractive() {
  const motion = useChartMotion()
  const [hidden, setHidden] = useState<string[]>([])

  const toggle = (key: string) =>
    setHidden((current) => (current.includes(key) ? current.filter((item) => item !== key) : [...current, key]))

  return (
    <div className="w-full max-w-2xl">
      <Chart className="h-72">
        <LineChart responsive data={data} aria-label="Visits by channel, by month">
          <CartesianGrid vertical={false} />
          <XAxis dataKey="month" tickLine={false} axisLine={false} minTickGap={24} interval="equidistantPreserveEnd" />
          <YAxis
            tickFormatter={(value) => compact.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
            niceTicks="snap125"
          />
          <Tooltip content={<ChartTooltip formatValue={(value) => count.format(value)} />} />
          <Legend itemSorter={null} content={<ChartLegend />} onClick={(entry) => toggle(String(entry.dataKey))} />
          {series.map((item) => (
            <Line
              key={item.key}
              name={item.name}
              dataKey={item.key}
              type="monotone"
              stroke={item.color}
              dot={false}
              activeDot={chartMarks.activeDot}
              hide={hidden.includes(item.key)}
              {...motion.series}
            />
          ))}
        </LineChart>
      </Chart>
    </div>
  )
}
```

## Accessibility [#accessibility]

- Pass `aria-label` to the chart with what it shows (`"Orders per day over 2 weeks"`). It becomes the accessible name of the SVG.
- The chart takes focus, and the left and right arrow keys move the tooltip from point to point.
- A chart with 2 or more lines has a `Legend`, and the tooltip names every series, so color is never the only cue.
- A legend item that toggles a line is a real `<button>` with `aria-pressed`, reachable with Tab and operable with Enter or Space.
- Nothing animates under reduced motion.
