Chart

Area, bar and line. Hover a chart, or focus it and use the arrow keys.

Revenue

$137,261

5.1%vs previous 30 days

Signups this week

1,466

8.2%vs last week

Customers by stage

678accounts

  • Trial
  • Potential
  • Converted
  • Customers
  • Active
  • Loyal

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/chart.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/chart.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { LineChart as LineChartEngine, BarChart as BarChartEngine } from "@/lib/fabricui/chart-engine";
import { GooSlide } from "@/lib/fabricui/goo-slide";
import "./chart.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;

const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" ");

/** One line of a LineChart. Colours go on the marks only (line, dots, keys), never on text. */
export type ChartSeries = {
  name: string;
  values: number[];
components/fabricui/chart.css
/*
 * Charts — line / area and bar (chart.js), and the card they sit in.
 *
 *   <section class="ch-card">
 *     <header class="ch-head"> title, figure, delta, legend or range </header>
 *     <div class="ch-plot" id="…"></div>       chart.js fills it
 *
 * A plain card; values and dates in small pills; series colours only on
 * marks (lines, bars, dots, keys), never on text. Pills and the range control
 * follow the roundness rule (40% of the height).
 */
.ch-card:where(.fui-chart, .fui-chart *) {
  position: relative;
  display: flex;
  flex-direction: column;
  padding: 20px 20px 16px;
lib/fabricui/chart-engine.js
import { Dither } from "./dither";

/*
 * chart-engine.js — line / area and bar charts (see chart.css).
 *
 *   const c = new LineChart(el, {
 *     labels: [Date, …],                        // one per point
 *     series: [{ name, values, color, gradient: [c1, c2], area: true }],
 *     format: v => "$" + v,                     // values
 *     axis: v => "$" + v,                       // y pills (defaults to format)
 *     ticks: "days" | "months" | "all",         // which x labels get a pill
 *     readout: "callouts" | "tooltip",          // a popup riding above the point, or one tooltip
 *     glow: true,                               // a dithered aura behind the lines
 *   });
 *   c.set({ labels, series });                  // new data: the lines glide to it
 *

Usage

Usage
"use client";

import * as React from "react";
import { ChartCard, ChartHeader, ChartTitle, ChartFigure, ChartDelta, LineChart } from "@/components/fabricui/chart";

export function Revenue({ days, values }: { days: Date[]; values: number[] }) {
  // the same array until the data changes (a new one makes the lines glide again)
  const series = React.useMemo(() => [{ name: "Revenue", values, color: "#338ced", area: true }], [values]);
  return (
    <ChartCard aria-labelledby="rev">
      <ChartHeader>
        <div>
          <ChartTitle id="rev">Revenue</ChartTitle>
          <ChartFigure hero>$137,261</ChartFigure>
          <ChartDelta value="5.1%">vs previous 30 days</ChartDelta>
        </div>
      </ChartHeader>
      <LineChart
        labels={days}
        series={series}
        format={v => "$" + v.toLocaleString("en-US")}
        title="Revenue by day"
      />
    </ChartCard>
  );
}

Props

PropTypeDefault
LineChart labels

One per point. Dates read as "Nov 30" (or "Nov").

(Date | string)[]—
LineChart series

The lines. gradient runs left to right along the line; area adds a dithered fill under it; width is in px (2).

{ name, values, color, gradient?, area?, width? }[]—
LineChart ticks

Which points get a date pill: five spread out, each month's first, or every one.

"days" | "months" | "all""days"
LineChart readout

One tooltip while hovering, or a popup riding above each line's point, always shown.

"tooltip" | "callouts""tooltip"
LineChart glow

A dithered aura behind the lines, round the crosshair.

booleanfalse
BarChart

Bars, one per label. name is what a value is, in the tooltip ("Signups").

{ labels, values, color?, name?, format?, axis?, title? }—
format / axis

How a value reads in readouts and the hidden data table / on the y-axis pills (axis defaults to format).

(v: number) => string—
title

What the chart shows, for screen readers.

string"Chart"
ChartRange

A small segmented control (7D / 30D / 90D) whose pill slides to the chosen option.

{ options, value?, defaultValue?, onValueChange? }—
ChartCard, ChartHeader, ChartTitle, ChartFigure, ChartDelta, ChartLegend

The card round a chart: its name, the big figure (hero for the large one), a change with an arrow, and a legend of { name, color }.

components—