affectUI › Components › Data display

Trend Chart

The chart on its own for any card or layout – line, area, bars or a sparkline from the same data, with a tooltip, this period against the last, and charts that follow the same day together.

Part of affectUI Pro. Install: npx shadcn@latest add https://www.affectui.com/r/trend-chart.json · Import from @/components/ui/trend-chart

When to use

  • Any chart inside your own card, KPI tile, table row or dashboard layout – it brings no card of its own
  • Values over time: a line, an area, bars per day, or a small sparkline next to a number
  • This period against the last – mark a series as previous and it is drawn dashed or faint
  • A row of charts that should read the same day together – share `index` between them

When not to use

  • More than about six series – the chart and the tooltip get hard to read
  • Long time ranges with hundreds of points – use a chart library with zoom and sampling
  • Parts of a whole – use Breakdown Chart or Model Usage

Usage

import { TrendChart } from "@/components/ui/trend-chart";

export function Spend() {
  return (
    <TrendChart
      variant="area"
      label="Spend per day"
      labels={["Mar 3", "Mar 4", "Mar 5", "Mar 6"]}
      format={(n) => `$${Math.round(n)}`}
      series={[
        { label: "This week", values: [412, 455, 398, 470] },
        { label: "Last week", values: [356, 380, 371, 390], previous: true },
      ]}
    />
  );
}

Props

NameTypeDefaultDescription
labelsstring[]One label per point; first, middle and last are shown on the axis
tooltipLabelsstring[]Longer labels for the tooltip header, falls back to labels
seriesTrendSeries[]{ label, values, color?, previous? } – values line up with labels, null leaves a gap; previous draws it dashed (line) or faint (bar)
variant"line" | "area" | "bar" | "sparkline""line"How the same data is drawn – a sparkline has no axes and a small tag instead of the tooltip
format(n: number) => stringcompact, e.g. 1.2kFormats tooltip values – and the axis, unless axisFormat is set
axisFormat(n: number) => stringShorter numbers for the axis, e.g. "$1.6T" while the tooltip shows the full amount
heightnumber152 (sparkline 40)Height of the plot in px
axisboolean | "x"true (sparkline false)Value labels on the left and dates below, only the dates ("x"), or none – without axes a small tag replaces the tooltip
tooltipbooleantrueThe popover or tag with the values – off when the card shows the value itself; the point stays marked
zerobooleantrueStart the value axis at zero; false zooms in around the values for averages and rates (bars always start at zero)
indexnumber | nullThe highlighted point, controlled – share it between charts so they move together
onIndexChange(index: number | null) => voidCalled when the pointer or the keys move to another point, or leave
defaultIndexnumberPoint that stays highlighted while nobody hovers
labelstring"Chart"Name for screen readers
emptyTextstring"No data yet"Quiet text in the plot when there are no values, or only zeros
classNamestringExtra class on the chart

Keyboard

  • Tab: Focus the chart
  • ← / →: Move to the previous or next point
  • Home / End: Jump to the first or last point
  • Esc: Close the tooltip

Accessibility

  • The chart is one focusable stop, readable with the arrow keys
  • The tooltip is a polite live region, so screen readers announce the values of the current point

Responsive

  • Fills the width of its container; the plot scales, line widths stay crisp
  • Below 320px the middle date steps aside; below 240px tooltip values move under their names
  • Touch works too – tap or drag across the chart
  • Dark mode built in: switches when an ancestor (usually <html>) has the class dark or data-theme="dark"

Requirements

  • React 19

Related

  • KPI Chart – Metric card with a multi-line chart and a tooltip that follows the pointer
  • Breakdown Chart – Stacked bar chart with measure, slice and segment pickers, a hover breakdown and a linked table
  • Model Usage – How much each AI company's models were used – one stacked bar per company in its color with a segment per model, the most used darkest and the rest lighter shades; point at a bar for its models and their share. The period's total and change on top with tokens, cost and requests to switch between, and below what it all went into – search, analyze, code, test – across every model, or just the company you point at