Line chart
Trends over time for one to four series, with gaps for missing data and marked forecasts.
Measures in different units rebased to 100, so growth compares on one honest axis.
<IndexChart />Index chart is free under the MIT license.
$ npx shadcn@latest add https://chartslibraryui.com/r/index-chart.jsonInstalls the chart, its dependencies and the shared core into your components folder.
Then import the theme once: import "@/components/charts/core/charts.css" Installation guide
import { IndexChart } from "@/components/charts/index-chart";
import { headcount } from "./sample-data";
export function Example() {
return (
<IndexChart
title="Revenue is growing faster than headcount"
subtitle="Index, Jan 2025 = 100"
data={data}
x="month"
series={[
{ key: "mrr", label: "MRR" },
{ key: "headcount", label: "Headcount" },
]}
/>
);
}
Rows with an x key and numeric keys; every series needs a non-zero value at the base period.
[{ month: "Jan", revenue: 1.2e6, customers: 840 }, ...]Missing values are null. Invalid values make the chart say what’s wrong instead of drawing a misleading picture. How charts handle bad data
Every chart also accepts the shared frame props: title, subtitle, source, height, mode, toolbar. Common API
export interface IndexChartProps extends Omit<SeriesChartProps, "format" | "zero"> {
/** x value of the base period. Default: the first row. Every series equals 100 there. */
base?: string | number;
}
// from core/series.tsx
export interface SeriesChartProps extends FrameProps {
data: Row[];
/** Key of the x value in each row. */
x: string;
series: SeriesInput[];
/** "category" treats x as labels (default); "time" reads ISO strings, Date or ms and spaces by real time. */
xType?: "category" | "time";
/** Date grain used to label time axes and tooltips. Auto-detected if omitted. */
grain?: DateGrain;
/** Y value format, e.g. "currency", { kind: "percent" }, or a function. */
format?: FormatInput;
curve?: Curve;
/** Series key(s) to emphasise; others are drawn as grey context. */
highlight?: string | string[];
/** First x value that is a forecast. From there on lines are dashed and the region is shaded. */
forecastFrom?: string | number | Date;
/** First x value that is an incomplete period (e.g. month to date). */
partialFrom?: string | number | Date;
/** Include zero in the y axis. Default true; set false to zoom in on a rate (lines only). */
zero?: boolean;
/** Fixed y domain; null keeps the computed bound. */
yDomain?: [number | null, number | null];
references?: Reference[];
/** Dots on every point. Default: only when there are few points. */
markers?: boolean;
/** Series names at the line ends. Default: on when up to 4 series fit. */
endLabels?: boolean;
/** Show the legend. Default: on for 2+ series. */
legend?: boolean;
/** Click legend entries to hide/show series. Default true for 2+ series. */
toggleable?: boolean;
}Installs index-chart.tsx and the shared core/ (11 files).
"use client";
import { useMemo } from "react";
import { SeriesChart, normalizeSeries, type SeriesChartProps } from "./core/series";
import type { Issue, Row } from "./core/data";
import { ChartMessage } from "./core/frame";
export interface IndexChartProps extends Omit<SeriesChartProps, "format" | "zero"> {
/** x value of the base period. Default: the first row. Every series equals 100 there. */
base?: string | number;
}
/**
* Measures in different units, or of very different size, rebased to 100 at a common period. Their growth can then
* be compared on one honest axis instead of a dual axis.
*/
export function IndexChart({ base, data, series, x, references = [], ...props }: IndexChartProps) {
const defs = normalizeSeries(series);
const keyList = defs.map((d) => d.key).join("|");
const { rows, issues } = useMemo(() => {
const issues: Issue[] = [];
if (!Array.isArray(data) || !data.length) return { rows: [] as Row[], issues };
const bi = base === undefined ? 0 : data.findIndex((r) => String(r?.[x]) === String(base));
if (bi < 0) {
issues.push({ level: "error", message: `Base period "${base}" is not in the data.` });
return { rows: [] as Row[], issues };
}
const baseRow = data[bi];
for (const s of defs) {
const b = baseRow?.[s.key];
if (typeof b !== "number" || b === 0 || !Number.isFinite(b))
issues.push({
level: "error",
message: `"${s.label}" has no usable value at the base period (${String(baseRow?.[x])}), so it can't be indexed.`,
});
}
const rows = data.map((r) => {
const out: Row = { [x]: r?.[x] };
for (const s of defs) {
const v = r?.[s.key];
const b = baseRow?.[s.key];
out[s.key] = typeof v === "number" && typeof b === "number" && b !== 0 ? (v / b) * 100 : (v ?? null);
}
return out;
});
return { rows, issues };
}, [data, base, x, keyList]); // eslint-disable-line react-hooks/exhaustive-deps
if (issues.some((i) => i.level === "error"))
return (
<figure className="clui">
{props.title && <h3 className="clui-title">{props.title}</h3>}
<ChartMessage issues={issues} height={props.height ?? 280} />
</figure>
);
return (
<SeriesChart
kind="line"
summaryName="index"
{...props}
data={rows}
x={x}
series={series}
format={{ kind: "number", decimals: 0 }}
zero={false}
references={[{ value: 100, label: "Base = 100" }, ...references]}
/>
);
}
Trends over time for one to four series, with gaps for missing data and marked forecasts.
Two measures in different units on stacked panels sharing one x axis: never a dual axis.
One small chart per group on a shared scale, instead of a ten-colour spaghetti chart.